android BottomNavigationView
ViewPager
实施Fragment
,fragment
内tablayout
附加ViewPager
Fragment
RecyclerView
Scroll Toolbar scroll
如果RecyclerView
Scroll toolbar scroll
请帮帮我
与此图像一样
答案 0 :(得分:0)
您可以按照以下步骤添加TabLayout
PagerView
:
第1步: 将布局添加到主xml中,如
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:tabStripEnabled="true"
app:tabBackground="@drawable/tab_color_selector"// make a xml selector
app:tabPaddingEnd="0dp"
app:tabIndicatorColor="#ff727272"
app:tabPaddingStart="0dp"
app:tabSelectedTextColor="#ffffff"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</RelativeLayout>
第2步:
在您的Activity
中找到这些并将FragmentPagerAdapter设置为PagerView并设置其他属性
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Artcles"));
tabLayout.addTab(tabLayout.newTab().setText("Dicussion"));
tabLayout.addTab(tabLayout.newTab().setText("News"));
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(new ArtclesFragment());// create an ArtclesFragment
fragments.add(new DicussionFragment());// Create a DicussionFragment
fragments.add(new NewsFragment()); //Create a NewsFragment
mViewPager.setAdapter(new FragPagerAdaptor(getSupportFragmentManager(), fragments););
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
FragPagerAdaptor如下所示:
public class FragPagerAdaptor extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
public FragPagerAdaptor(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments == null ? 0 : fragments.size();
}
}
注意:您必须为每个标签创建单独的fragemet
。
希望它能帮助您了解标签和PagerView
TabLayout
的基本实现。