可以添加多个Fragment
viewpagerAdapter
实现选项卡效果配合viewpragre配使用 布局
public class Main2Activity extends AppCompatActivity { ViewPager viewPager; TabLayout tabLayout; List<Fragment> list= new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); viewPager = findViewById(R.id.view1); tabLayout = findViewById(R.id.tab1); Frag1 frag1 = new Frag1(); Frag2 frag2 = new Frag2(); Frag3 frag3 = new Frag3(); Frag4 frag4 = new Frag4(); Frag5 frag5 = new Frag5(); list.add(frag1); list.add(frag2); list.add(frag3); list.add(frag4); list.add(frag5); String[] strings={"推荐","手机","智能","电视5周年","笔记本"}; MyAdapter1 myAdapter1 = new MyAdapter1(getSupportFragmentManager(), list, strings); viewPager.setAdapter(myAdapter1); tabLayout.setupWithViewPager(viewPager); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { switch (i){ case 0: RadioButton radioButton = findViewById(R.id.ra1); radioButton.setChecked(true); break; case 1: RadioButton radioButton2 = findViewById(R.id.ra2); radioButton2.setChecked(true); break; case 2: RadioButton radioButton3 = findViewById(R.id.ra3); radioButton3.setChecked(true); break; case 3: RadioButton radioButton4 = findViewById(R.id.ra4); radioButton4.setChecked(true); break; case 4: RadioButton radioButton5 = findViewById(R.id.ra5); radioButton5.setChecked(true); break; } } @Override public void onPageScrollStateChanged(int i) { } }); } } public class Frag1 extends Fragment { ViewPager viewPager; List<Fragment> list=new ArrayList<>(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.name1, container, false); viewPager =view.findViewById(R.id.view3); list.add(new Fr1()); list.add(new Fr2()); list.add(new Fr3()); list.add(new Fr4()); list.add(new Fr5()); Mybase mybase = new Mybase(getChildFragmentManager(), list); viewPager.setAdapter(mybase); return view; } }适配器
public class Mybase extends FragmentPagerAdapter { List<Fragment> list; public Mybase(FragmentManager fm, List<Fragment> list) { super(fm); this.list=list; } @Override public Fragment getItem(int i) { return list.get(i); } @Override public int getCount() { return list.size(); } } public class Frag2 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view =inflater.inflate(R.layout.names1,container,false); return view; } } public class Frag3 extends Fragment { ViewPager viewPager; List<Fragment> list = new ArrayList<>(); RadioButton radioButton; RadioButton radioButton1; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final View view =inflater.inflate(R.layout.name2,container,false); viewPager= view.findViewById(R.id.view2); Fra1 fra1 = new Fra1(); Fra2 fra2 = new Fra2(); list.add(fra1); list.add(fra2); MyBaseAdapter2 myBaseAdapter2 = new MyBaseAdapter2(getChildFragmentManager(), list); viewPager.setAdapter(myBaseAdapter2); radioButton = view.findViewById(R.id.rd1); radioButton1=view.findViewById(R.id.rd2); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { switch (i){ case 0: radioButton.setChecked(true); radioButton1.setChecked(false); break; case 1: radioButton1.setChecked(true); radioButton.setChecked(false); break; } } @Override public void onPageScrollStateChanged(int i) { } }); return view; }}
public class Frag4 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.name4,container,false); } } public class Frag5 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.name5,container,false); } }布局
<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=".Main2Activity"> <android.support.design.widget.TabLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/view1" android:layout_above="@id/rg1" android:layout_below="@id/tab1" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v4.view.ViewPager> <RadioGroup android:layout_alignParentBottom="true" android:id="@+id/rg1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_weight="1" android:id="@+id/ra1" android:button="@null" android:drawableTop="@drawable/shouye1" android:text="首页" android:textAlignment="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:layout_weight="1" android:id="@+id/ra2" android:button="@null" android:drawableTop="@drawable/shouye2" android:text="分类" android:textAlignment="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:layout_weight="1" android:id="@+id/ra3" android:button="@null" android:drawableTop="@drawable/shouye3" android:text="发现" android:textAlignment="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:layout_weight="1" android:id="@+id/ra4" android:button="@null" android:drawableTop="@drawable/shouye4" android:text="购物车" android:textAlignment="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/ra5" android:button="@null" android:drawableTop="@drawable/shouye5" android:text="我" android:textAlignment="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/view3" android:layout_width="match_parent" android:layout_height="200dp"> </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/tu1" /> <LinearLayout android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/tu2" /> <ImageView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/tu3" /> </LinearLayout> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/view2" android:layout_width="match_parent" android:layout_height="300dp"> </android.support.v4.view.ViewPager> <RadioGroup android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:layout_marginLeft="100dp" android:id="@+id/rd1" android:layout_weight="1" android:button="@null" android:layout_width="100dp" android:layout_height="wrap_content" android:drawableTop="@drawable/fragmentx" /> <RadioButton android:layout_marginLeft="20dp" android:layout_toRightOf="@id/rd1" android:id="@+id/rd2" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/fragmentx1" android:layout_width="100dp" android:layout_height="wrap_content" /> </RelativeLayout> </RadioGroup> </LinearLayout>