MD出來(lái)之后再也不用羨慕iOS的優(yōu)雅界面了
布局文件
<?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="娛樂(lè)"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音樂(lè)"/>
</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>
以上有幾個(gè)坑:
1.toolbar 的title屬性不能用android:title,要用app:title,跟命名空間有關(guān)
2.toolbar的background屬性最好自己設(shè)置,否則顏色會(huì)和activity主題有關(guān)。
邏輯代碼
這里對(duì)viewpager進(jìn)行處理货裹。
這兒的坑是用getFragmentManager().beginTransaction().add(fragment1,fragment1.getTag());
的話會(huì)報(bào)錯(cuò)卿闹,不能夠在這里設(shè)置fragment的tag横殴,因?yàn)樵跇?gòu)造的時(shí)候已經(jīng)設(shè)置了他的Tag昆禽。
public class Fragment_main extends BaseFragment {
public ViewPager viewPager;
public TabLayout tabLayout;
public List<Fragment> fragments;
public static String[] title=new String[]{"體育","娛樂(lè)","音樂(lè)"};
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);
//不能用是因?yàn)閠ag已經(jīng)被設(shè)置了
// 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;
}
}