顶部标题TabLayout点击切换Fragment

Posted 沈晖然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顶部标题TabLayout点击切换Fragment相关的知识,希望对你有一定的参考价值。

顶部标题TabLayout点击切换Fragment

加依赖:

 

在开始之前先调用工具插件:右键工程》Open Module Settings》Dependencies》+》Library Dependency》导人(com.android.support:design:27.0.2)》ok

 

 

implementation ‘com.android.support:design:27.0.2‘
===================================================

 

布局:

<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/tabLayout"/>
    <android.support.v4.view.ViewPager
android:layout_height="0dp"
android:layout_width="fill_parent"
android:id="@+id/viewPager"
android:layout_weight="1"/>
</LinearLayout>
==============================================
主要方法:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.shenhuiran.R;
import java.util.ArrayList;
public class Main2Fragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    ArrayList<Fragment> fragmentList;  //放所有的fragment
ArrayList<String> titleList ;// tablayout的标题
private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.fragment_2, null);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        Log.e("test", "---------------------onCreateView");
        return view;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        Log.e("test", "---------------------test log");
        viewPager.setAdapter(new MAdapter(getFragmentManager()));
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //让viewpager和顶部标题关联
tabLayout.setupWithViewPager(viewPager);
    }
    private void initData() {
        fragmentList = new ArrayList<Fragment>();
        titleList = new ArrayList<String>();
        if (fragmentList.size()==0){
            fragmentList.add(new Fragment1());
            fragmentList.add(new Fragment2());
            fragmentList.add(new Fragment3());
        }
        if (titleList.size()==0){
            titleList.add("Fragment1");
            titleList.add("Fragment2");
            titleList.add("Fragment3");
        }
    }
    private class MAdapter extends FragmentPagerAdapter {
        public MAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }
        @Override
        public int getCount() {
            return fragmentList.size();
        }
        //一定要重写这个返回标题的方法;
@Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }
    }
}
=======================================

Fragment1 方法:

importandroid.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.shenhuiran.R;
public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.f1, null);
        return view;
    }
}

以上是关于顶部标题TabLayout点击切换Fragment的主要内容,如果未能解决你的问题,请参考以下文章

简单Fragment+Tablayout+ViewPage实现底部和顶部滑动(菜单栏)

Kotlin 实现可点击可滑动顶部导航栏(AppBarLayout+TabLayout)和左右切换可滑动页面(ViewPager)的功能

TabLayout 中的选项卡不可点击

关于tablayout+viewpager+fragment配合使用的一点记录

自适应 Tab 宽度可以滑动文字逐渐变色的 TabLayout(仿今日头条顶部导航)

TabLayoutViewPagerDemoTabLayout+ViewPager可滑动