.xml 中布局
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabIndicatorColor="@color/cheng"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/find_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
对应Fragment中代码
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
public class FindFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewpager;
ArrayList fragmentList = new ArrayList<Fragment>();
String[] title = {"推荐","b","c","d","e","f","g","h","i"};
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_find, container, false);
tabLayout = root.findViewById(R.id.tablayout);
viewpager = root.findViewById(R.id.find_viewpager);
return root;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// fragment中嵌套fragment, Manager需要用(getChildFragmentManager())
MPagerAdapter mPagerAdapter = new MPagerAdapter(getChildFragmentManager());
initFragment();
tabLayout.setupWithViewPager(viewpager);
viewpager.setAdapter(mPagerAdapter);
}
private void initFragment() {
fragmentList.add(new find_tuijian());
fragmentList.add(new find_b());
fragmentList.add(new find_c());
fragmentList.add(new find_d());
fragmentList.add(new find_e());
fragmentList.add(new find_f());
fragmentList.add(new find_g());
fragmentList.add(new find_h());
fragmentList.add(new find_i());
}
class MPagerAdapter extends FragmentPagerAdapter {
public MPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return (Fragment) fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
//返回tablayout的标题文字;
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
}
各个页面中布局用LinearLayout,创建几个Fragment
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".find_tuijian">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1111111111" />
</LinearLayout>
对应Fragment中代码
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class find_a extends Fragment {
public find_tuijian() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_find_tuijian, container, false);
}
}