.fragment_a
<?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=".fragment.AFragment" android:orientation="vertical"> <!-- TODO: Update blank fragment layout --> <android.support.design.widget.TabLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/vp2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"></android.support.v4.view.ViewPager> </LinearLayout>.AFragment
import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.lll.yuekaomoni.R; import com.example.lll.yuekaomoni.adapter.MyPageAdapter; import com.example.lll.yuekaomoni.tab_fragment.FFragment; import com.example.lll.yuekaomoni.tab_fragment.GFragment; import java.util.ArrayList; public class AFragment extends Fragment { private View inflate; private TabLayout tab; private ViewPager vp2; private ArrayList<Fragment> fragments; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { inflate = inflater.inflate(R.layout.fragment_a, container, false); tab = inflate.findViewById(R.id.tab); vp2 = inflate.findViewById(R.id.vp2); initData(); return inflate; } private void initData() { fragments = new ArrayList<>(); fragments.add(new FFragment()); fragments.add(new GFragment()); MyPageAdapter myPageAdapter = new MyPageAdapter(getChildFragmentManager()); myPageAdapter.setFragments(fragments); vp2.setAdapter(myPageAdapter); tab.addTab(tab.newTab()); tab.addTab(tab.newTab()); tab.setupWithViewPager(vp2); tab.getTabAt(0).setText("推荐"); tab.getTabAt(1).setText("视频"); } }