快速搭建MD風格的界面

MD出來之后再也不用羨慕iOS的優雅界面了

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="新聞"
            app:titleTextColor="#ffffff"/>

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

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="體育"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="娛樂"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="音樂"/>
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

以上有幾個坑:
1.toolbar 的title屬性不能用android:title,要用app:title,跟命名空間有關
2.toolbar的background屬性最好自己設置,否則顏色會和activity主題有關。

邏輯代碼

這里對viewpager進行處理。
這兒的坑是用getFragmentManager().beginTransaction().add(fragment1,fragment1.getTag());的話會報錯,不能夠在這里設置fragment的tag,因為在構造的時候已經設置了他的Tag。

public class Fragment_main extends BaseFragment {
    public ViewPager viewPager;
    public TabLayout tabLayout;
    public List<Fragment> fragments;
    public static String[] title=new String[]{"體育","娛樂","音樂"};
    public static Fragment_main newInstance() {
        
        Bundle args = new Bundle();
        
        Fragment_main fragment = new Fragment_main();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void initView() {
        viewPager= (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout= (TabLayout) view.findViewById(R.id.tablayout);
        tabLayout.addTab(tabLayout.newTab().setText(title[0]));
        tabLayout.addTab(tabLayout.newTab().setText(title[1]));
        tabLayout.addTab(tabLayout.newTab().setText(title[2]));
        ZhihuNewsListFragment fragment1=ZhihuNewsListFragment.newInstance();
        NewsScanFragment fragment2=NewsScanFragment.newInstance();
        ZhihuNewsListFragment fragment3=ZhihuNewsListFragment.newInstance();
        fragments=new ArrayList<>();
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        //不能用是因為tag已經被設置了
//        getFragmentManager().beginTransaction().add(fragment1,fragment1.getTag());
//        getFragmentManager().beginTransaction().add(fragment2,fragment2.getTag());
//        getFragmentManager().beginTransaction().add(fragment1,fragment1.getTag());
        getFragmentManager().beginTransaction().commit();
        viewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

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

            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        });
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setupWithViewPager(viewPager);
        // startfragment(R.id.fg_container, ZhihuNewsListFragment.newInstance());
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void showContentView() {

    }

    @Override
    public void hideContentView() {

    }

    @Override
    public void showLoadingContentView() {

    }

    @Override
    public void removeLoadingContentView() {

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public int getRootViewId() {
        return R.layout.fragment_main;
    }
}
Paste_Image.png
Paste_Image.png
Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容