顶部标题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; } }