当前位置: 代码迷 >> 综合 >> TabLayout + ViewPager + Fragment实现类似新闻栏目动态切换
  详细解决方案

TabLayout + ViewPager + Fragment实现类似新闻栏目动态切换

热度:53   发布时间:2023-11-18 04:16:49.0

1,此次文章讲述如何利用TabLayout + ViewPager + Fragment结构做一个内容切换;

  • 主要类文件和布局文件:
  • 1个adapter适配器文件,如:scanFragmentPagerAdapter;
  • 2个class类文件+1个主class类文件,如:ScanFragment1,ScanFragment2,MyActivityFragment;
  • 2个子布局文件+1个TabLayout + ViewPager布局界面;如:conent_fargment,scanfragment1,scanfragment2;

2,其次需要注意的是需要导入包文件,放置在app-bulide下,26.1.0是根据你当前android版本号来引入:

 compile 'com.android.support:appcompat-v7:26.1.0'compile 'com.android.support:design:26.1.0'

3,好,具体的思路/类文件,配置文件都弄清楚了,接下来看:

  • conent_fargment布局界面:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.noonecode.tablayoutdemo.MainActivity"><android.support.design.widget.TabLayoutandroid:id="@+id/tablayout2"android:layout_width="match_parent"android:layout_height="wrap_content" /><android.support.v4.view.ViewPagerandroid:id="@+id/viewpager2"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></android.support.v4.view.ViewPager></LinearLayout>
</android.support.v4.widget.DrawerLayout>
  • scanfragment1,scanfragment2布局,我这儿就建立一个了。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent">
<LinearLayout  android:layout_width="match_parent"android:layout_height="wrap_content"><TextView  android:layout_width="match_parent"android:layout_height="wrap_content"android:text="11111"/></LinearLayout></android.support.constraint.ConstraintLayout>
  • scanFragmentPagerAdapter适配器界面:
public class scanFragmentPagerAdapter extends FragmentPagerAdapter {
    private int num;private HashMap<Integer, Fragment> mFragmentHashMap = new HashMap<>();public scanFragmentPagerAdapter(FragmentManager fm, int num) {super(fm);this.num = num;}@Overridepublic Fragment getItem(int position) {return createFragment(position);}@Overridepublic int getCount() {return num;}private Fragment createFragment(int pos) {Fragment fragment1 = mFragmentHashMap.get(pos);if (fragment1 == null) {switch (pos) {case 0:fragment1 = new ScanFragment1();Log.i("fragment", "fragment1");break;case 1:fragment1 = new ScanFragment2();Log.i("fragment", "fragment2");break;}mFragmentHashMap.put(pos, fragment1);}return fragment1;}
}
  • ScanFragment1,ScanFragment2类文件,也同上,只创建一个,剩余的自己需要几个就建立几个类文件;
public class ScanFragment1 extends Fragment{
    @Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {return inflater.inflate( R.layout.scanfragment1, container, false);}
}
  • MyActivityFragment主文件:
public class MyActivityFragment extends AppCompatActivity implements View.OnClickListener {
    
// private LinearLayout back;private TabLayout tabLayout2;private ViewPager viewPager2;private Button tiaozhuan1;private List<Fragment> list2;private MyAdapter adapter2;private String[] titles = {
   "用户", "标签"};protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView( R.layout.conent_fargment);viewPager2 = (ViewPager) findViewById(R.id.viewpager2);tabLayout2 = (TabLayout) findViewById(R.id.tablayout2);
// back = findViewById(R.id.back);
// back.setOnClickListener(this);list2 = new ArrayList<>();list2.add(new ScanFragment1());list2.add(new ScanFragment2());//ViewPager的适配器adapter2 = new MyAdapter(getSupportFragmentManager());viewPager2.setAdapter(adapter2);//绑定tabLayout2.setupWithViewPager(viewPager2);//实例化}@Overridepublic void onClick(View v) {
// switch (v.getId()){
    
// case R.id.back:
// finish();
// break;
// }}class MyAdapter extends FragmentPagerAdapter {public MyAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {return list2.get(position);}@Overridepublic int getCount() {return list2.size();}//重写这个方法,将设置每个Tab的标题@Overridepublic CharSequence getPageTitle(int position) {return titles[position];}}
}

讲述结束完毕,主要归纳就这7就文件,若你们需求listview,只需要在当前类文件引入listview和适配器就ok,若有疑问或不懂者可以私信,评论!谢谢!!

  相关解决方案