Android Studio ViewPager和TabLayout

    xiaoxiao2022-07-07  177

    ViewPager和TabLayout

    ViewPage 实现 滑动效果控件TabLayout代码片段

    ViewPage 实现 滑动效果控件

    实现 滑动效果控件 使用 可以添加多个Fragment ViewPagerAdapter 适配器 作用:可以添加Fragment和TabLayout 用法: 1、自定义类继承ViewPagerAdapter 2、重写4个方法

    TabLayout

    实现选项卡效果配合ViewPager来搭配使用 用法: 1、导依赖 2、加标签 3、常用属性

    代码片段

    主类

    public class MainActivity extends AppCompatActivity { ViewPager viewPage; RadioButton btn1; RadioButton btn2; RadioButton btn3; RadioButton btn4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPage = findViewById(R.id.viewPage); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); btn3 = findViewById(R.id.btn3); btn4 = findViewById(R.id.btn4); btn1.setChecked(true); ArrayList<Fragment> list = new ArrayList<>(); FragmentA fragmentA = new FragmentA(); FragmentB fragmentB = new FragmentB(); FragmentC fragmentC = new FragmentC(); FragmentD fragmentD = new FragmentD(); list.add(fragmentA); list.add(fragmentB); list.add(fragmentC); list.add(fragmentD); MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), list); viewPage.setAdapter(myAdapter); viewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { switch (i){ case 0: btn1.setChecked(true); break; case 1: btn2.setChecked(true); break; case 2: btn3.setChecked(true); break; case 3: btn4.setChecked(true); break; } } @Override public void onPageScrollStateChanged(int i) { } }); } }

    适配器

    public class MyAdapter extends FragmentPagerAdapter { ArrayList<Fragment> list; public MyAdapter(FragmentManager fm,ArrayList<Fragment> list) { super(fm); this.list = list; } @Override public Fragment getItem(int i) { return list.get(i); } @Override public int getCount() { return list.size(); } }

    其中一个Fragment

    public class FragmentA extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_a, container, false); return view; } }

    布局文件

    <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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPage" android:layout_above="@id/bottom" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fragment_a" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <FrameLayout android:id="@+id/fragment_b" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <FrameLayout android:id="@+id/fragment_c" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <FrameLayout android:id="@+id/fragment_d" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <RadioButton android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:text="按钮1" android:gravity="center" android:drawableTop="@drawable/select"/> <RadioButton android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:text="按钮2" android:gravity="center" android:drawableTop="@drawable/select"/> <RadioButton android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:text="按钮3" android:gravity="center" android:drawableTop="@drawable/select"/> <RadioButton android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:text="按钮4" android:gravity="center" android:drawableTop="@drawable/select"/> </RadioGroup> </RelativeLayout>
    最新回复(0)