読者です 読者をやめる 読者になる 読者になる

さまよえる、Android

Androidのプログラミングで便利なことや残しておきたいことを残しておく。もしオススメのライブラリがあったら教えてくださいね。

AndroidのUIライブラリ、PagerSlidingTabStripを使ってみた。

github.com

https://raw.githubusercontent.com/jpardogo/PagerSlidingTabStrip/master/art/material_tabs.gif

    compile 'com.astuetz:pagerslidingtabstrip:1.0.1'

MainActivity.java

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.astuetz.PagerSlidingTabStrip;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout vDrawerLayout;
    private ActionBarDrawerToggle vDrawerToggle;
    private ListView vListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new TestAdapter(getSupportFragmentManager()));
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tab_strip);
        tabs.setViewPager(pager);

        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        toolbar.setPopupTheme(R.style.PopupMenu);

        vDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        initDrawer();
        vListView = (ListView) findViewById(R.id.listview);
        vListView.setAdapter(new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, new String[]{"menu 1", "menu 2", "menu 3", "menu 4", "menu 5"}));

    }

    protected void onPostCreate(Bundle savedInstanceState) {
        super .onPostCreate(savedInstanceState);
        vDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super .onConfigurationChanged(newConfig);
        vDrawerToggle.onConfigurationChanged(newConfig);
    }

    private class TestAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"スポーツ", "芸能", "食べ物","ニュース","IT"};

        public TestAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment1();
                case 2:
                    return new Fragment1();
                case 3:
                    return new Fragment1();
                case 4:
                    return new Fragment1();
                case 5:
                    return new Fragment1();
            }
            return null;
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void initDrawer() {
        vDrawerToggle = new ActionBarDrawerToggle( this , vDrawerLayout, R.string.app_name, R.string.app_name);
        vDrawerToggle.setDrawerIndicatorEnabled( true );
        vDrawerLayout.setDrawerListener(vDrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled( true );
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        return vDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }

    public static class Fragment1 extends Fragment {
        @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment, container, false);
        }
    }

}

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:elevation="5dp"
            android:layout_alignParentLeft = "true"
            android:layout_alignParentStart = "true"
            android:layout_alignParentTop = "true"
            android:theme="@style/ToolbarTheme"
            app:popupTheme="@style/PopupMenu"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary">
        </android.support.v7.widget.Toolbar>

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tab_strip"
            android:layout_below = "@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="?attr/colorPrimary"
            android:textColor="#ffffff"
            android:textSize="14sp"
            app:pstsDividerColor="@android:color/transparent"
            app:pstsIndicatorColor="@color/white"
            app:pstsIndicatorHeight="2dp"
            app:pstsShouldExpand="true"
            app:pstsUnderlineColor="@android:color/transparent" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_below = "@+id/tab_strip"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:nestedScrollingEnabled="false"
            android:layout_width="match_parent" />

    </RelativeLayout>
    <LinearLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff">
        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</FrameLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimaryDark">#212121</item>
        <item name="colorPrimary">#212121</item>
        <item name="android:textColorPrimary">#000000</item>
        <item name="android:textColorSecondary">#ffffff</item>


    </style>

    <style name="ToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Toolbar Title Color-->
        <item name="android:textColorPrimary">#ffffff</item>
        <item name="colorPrimaryDark">#212121</item>
        <item name="colorPrimary">#212121</item>
        <!-- Toolbar menu overflow icon color -->
        <item name="android:textColorSecondary">#ffffff</item>
    </style>

    <style name="PopupMenu" parent="Base.ThemeOverlay.AppCompat">
        <item name="android:background">#ffffff</item>
        <item name="android:textColorPrimary">#999999</item>

        <!-- <color name="base_actionbar">#e51c23</color> -->
    </style>
    <style name="toolbarSpinnerStyle" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
        <item name="android:background">?android:selectableItemBackground</item>
        <item name="android:dropDownSelector">?android:selectableItemBackground</item>
        <item name="android:divider">@null</item>
        <item name="android:dividerHeight">0dp</item>
        <item name="overlapAnchor">true</item>
    </style>
</resources>