android fragment学习5--fragment扩展 TabLayout非自定义

android fragment学习5--fragment扩展 TabLayout非自定义_第1张图片

Activity

/**
 * Created by steven on 2017/10/29 0029.
 */

public class FloatActionBarActivity extends BaseActivity {

    private DrawerLayout mRootDl;
    private LinearLayout mMenuLl;
    private TabLayout mIndicatorTl;
    private ViewPager mContentVp;

    private List tabIndicators;
    private List tabFragments;
    private ContentPagerAdapter contentAdapter;

    private ActionBarDrawerToggle drawerToggle;

    public static void startActivity(Context context ){
        Intent intent = new Intent(context, FloatActionBarActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fab);

        mIndicatorTl = (TabLayout) findViewById(R.id.tl_indicator);
        mContentVp = (ViewPager) findViewById(R.id.vp_content);

        initDrawer();
        initContent();
        initTab();
    }

    private void initDrawer() {
        mRootDl = (DrawerLayout) findViewById(R.id.dl_root);
        drawerToggle = new ActionBarDrawerToggle(this, mRootDl, R.string.app_name, R.string.app_name);
        mRootDl.setDrawerListener(drawerToggle);

        mMenuLl = (LinearLayout) findViewById(R.id.ll_menu);
        DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) mMenuLl.getLayoutParams();
        layoutParams.width = getScreenSize()[0]/4*3;

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public int[] getScreenSize(){
        int screenSize[] = new int[2];
        DisplayMetrics displayMetrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screenSize[0] = displayMetrics.widthPixels;
        screenSize[1] = displayMetrics.heightPixels;
        return screenSize;
    }

    public void onClickFab(View v){
        Snackbar.make(findViewById(R.id.fab_add), "Show The Snackbar", Snackbar.LENGTH_SHORT).show();
    }

    private void initTab(){
        mIndicatorTl.setTabMode(TabLayout.MODE_FIXED);
        mIndicatorTl.setTabTextColors(ContextCompat.getColor(this, R.color.gray), ContextCompat.getColor(this, R.color.white));
        mIndicatorTl.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.white));
        ViewCompat.setElevation(mIndicatorTl, 10);
        mIndicatorTl.setupWithViewPager(mContentVp);
    }

    private void initContent(){
        tabIndicators = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            tabIndicators.add("Tab " + i);
        }
        tabFragments = new ArrayList<>();
        for (String s : tabIndicators) {
            tabFragments.add(TabListFragment.newInstance(s));
        }
        contentAdapter = new ContentPagerAdapter(getSupportFragmentManager());
        mContentVp.setAdapter(contentAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_samples, menu);
        return true;
    }

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

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class ContentPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

        @Override
        public int getCount() {
            return tabIndicators.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }

    }

}

布局文件

"1.0" encoding="utf-8"?>
.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    .support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        .support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            "@layout/include_toolbar" />

            .support.design.widget.TabLayout
                android:id="@+id/tl_indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        .support.design.widget.AppBarLayout>

        .support.v4.view.ViewPager
            android:id="@+id/vp_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


        "bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        

    .support.design.widget.CoordinatorLayout>

    "@+id/ll_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@color/white">

        "wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu Content"
            android:textColor="@color/black"
            android:textSize="@dimen/sp_16"/>

    

.support.v4.widget.DrawerLayout>

子fragment

public class TabListFragment extends Fragment implements AdapterView.OnItemClickListener{

    private static final String EXTRA_CONTENT = "content";

    private ListView mContentLv;

    public static TabListFragment newInstance(String content){
        Bundle arguments = new Bundle();
        arguments.putString(EXTRA_CONTENT, content);
        TabListFragment tabContentFragment = new TabListFragment();
        tabContentFragment.setArguments(arguments);
        return tabContentFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_tab_list, null);

        mContentLv = (ListView) contentView.findViewById(R.id.lv_content);
        mContentLv.setOnItemClickListener(this);
        ViewCompat.setNestedScrollingEnabled(mContentLv, true);
        mContentLv.setAdapter(new ContentAdapter());

        return contentView;
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        // TODO
    }

    private class ContentAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.item_simple_list_2, null);
            ImageView coverIv = (ImageView) contentView.findViewById(R.id.iv_cover);
            coverIv.setImageResource(getResources().getIdentifier("ic_palette_0"+position%4, "mipmap", getActivity().getPackageName()));
            contentView.findViewById(R.id.cv_content).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent detailIntent = new Intent(getActivity(), PaletteDetailActivity.class);
                    detailIntent.putExtra(PaletteDetailActivity.EXTRA_INDEX, position);
                    startActivity(detailIntent);
                }
            });
            return contentView;
        }
    }

}

小结

这个demo跟fragment4一样的,都是tabLayout和viewpager,因为它使用到了 android.support.design.widget.CoordinatorLayout,可伸缩扩展的布局,所以拿出来,方便以后用到。这样的布局现新的app是经常用到的。用法,代码的含义在fragment4中有提到。

android fragment学习6–其他种类的tab
https://blog.csdn.net/qq_26296197/article/details/81147007

你可能感兴趣的:(Android)