--------------TabLayout布局---------------
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.com.tablayout.MainActivity"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tab" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pager" android:layout_below="@+id/tab"></android.support.v4.view.ViewPager> </RelativeLayout>
-------------MainActivity代码-------------
public class MainActivity extends AppCompatActivity { private TabLayout tab; private ViewPager pager; private List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控件 tab = findViewById(R.id.tab); pager = findViewById(R.id.pager); getdata(); //设置绑定 tab.setupWithViewPager(pager); //设置适配器 pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Nullable @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) { ContentFragment fragment = new ContentFragment(); Bundle bundle=new Bundle(); bundle.putString("list",list.get(position)); fragment.setArguments(bundle); return fragment; } @Override public int getCount() { return list.size(); } }); } public void getdata() { list = new ArrayList<>(); //加载数据 list.add("相识"); list.add("遇见"); list.add("相知"); list.add("相恋"); list.add("相爱"); list.add("相守"); list.add("白头"); list.add("来生"); } }
-------------content布局------------
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.com.tablayout.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_content" android:layout_centerInParent="true"/> </RelativeLayout> ------------ContentFragment代码----------- public class ContentFragment extends Fragment{ @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.content,container,false); //扎到控件 TextView text=view.findViewById(R.id.text_content); Bundle bundle = getArguments(); String list = bundle.getString("list"); text.setText(list); return view; } }