###横向滑动布局
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width=“0dp”
app:layout_constraintTop_toTopOf=“parent”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
android:layout_height=“wrap_content”></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/Pager"
android:layout_width=“0dp”
android:layout_height=“0dp”
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tab"></android.support.v4.view.ViewPager>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
###适配器
public class Oneadapter extends FragmentPagerAdapter {
public String[] s = new String[]{
“关注”, “推荐”, “热点”, “科技”, “视频”, “数码”, “汽车”
};
public Oneadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return new FragmentTwo();
}
@Override
public int getCount() {
return s.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return s[position];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
###核心代码
public class FragmentOne extends Fragment {
private ViewPager Pager;
private TabLayout tab;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmentone,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Pager = view.findViewById(R.id.Pager);
tab = view.findViewById(R.id.tab);
Pager.setAdapter(new Oneadapter(getChildFragmentManager()));
tab.setupWithViewPager(Pager);
}
}
作者:weixin_44022413
来源:CSDN
原文:https://blog.csdn.net/weixin_44022413/article/details/84955773
版权声明:本文为博主原创文章,转载请附上博文链接!