layout:
<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.shopcart.view.OrderActivity"> <RelativeLayout android:id="@+id/relat" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:text="订单列表" android:textSize="30sp" android:textColor="#000" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <TextView android:id="@+id/t1" android:layout_below="@+id/relat" android:background="#F3F4F5" android:layout_width="match_parent" android:layout_height="4dp" /> <android.support.design.widget.TabLayout android:layout_below="@+id/t1" android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="55dp" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary"></android.support.design.widget.TabLayout> <TextView android:id="@+id/t2" android:layout_below="@+id/tab_layout" android:background="#F3F4F5" android:layout_width="match_parent" android:layout_height="4dp" /> <android.support.v4.view.ViewPager android:layout_below="@+id/t2" android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </RelativeLayout>
2.代码:
public class OrderActivity extends AppCompatActivity { private TabLayout tab_layout; private ViewPager view_pager; private List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_order); //https://www.zhaoapi.cn/product/getOrders?uid=71 tab_layout = (TabLayout) findViewById(R.id.tab_layout); view_pager = (ViewPager) findViewById(R.id.view_pager); list = new ArrayList<>(); list.add("全部"); list.add("待支付"); list.add("已支付"); list.add("已取消"); view_pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) { DanFragment danFragment = new DanFragment(); Bundle bundle = new Bundle(); bundle.putString("title",list.get(position)); danFragment.setArguments(bundle); return danFragment; } @Override public int getCount() { return list.size(); } }); tab_layout.setupWithViewPager(view_pager); view_pager.setOffscreenPageLimit(list.size()-1); } }.//跳转订单fragment:http://blog.csdn.net/wuchengyayo/article/details/78861861