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