TabLayout 横向滑动菜单

布局---------------------------------------------------------------------
<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="maxinawei.bw.com.zhonghelianxi.Fragment1"
    android:background="#09f"
    android:orientation="vertical">
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/myTab"
            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="match_parent"
            android:id="@+id/pager"
            android:layout_below="@+id/myTab"
            ></android.support.v4.view.ViewPager>




</RelativeLayout>


具体的操作-----------------------------------------
public class Fragment1 extends Fragment {
private ViewPager pager;
  private TabLayout mytab;
  private List<DateType> meuns;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment1, container, false);
    pager = v.findViewById(R.id.pager);
    mytab = v.findViewById(R.id.myTab);
    return v;
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  //初始化
    initmeuns();

    MyAdapter adapter=new MyAdapter(getChildFragmentManager());
    pager.setAdapter(adapter);
    mytab.setupWithViewPager(pager);

  }
class MyAdapter extends FragmentPagerAdapter{

  public MyAdapter(FragmentManager fm) {
    super(fm);
  }

  @Override
  public CharSequence getPageTitle(int position) {
    return meuns.get(position).getTitle();
  }

  @Override
  public Fragment getItem(int position) {
    Bundle bundle=new Bundle();
    bundle.putString("type",meuns.get(position).getTitle());
    ContentFrag frag=new ContentFrag();
  frag.setArguments(bundle);
    return frag;
  }

  @Override
  public int getCount() {
    return meuns.size();
  }
}



  private void initmeuns() {
    meuns=new ArrayList<>();
    meuns.add(new DateType("数据新闻","xbsjxw"));
    meuns.add(new DateType("快讯","txs"));
    meuns.add(new DateType("头条","toutiao"));
    meuns.add(new DateType("精编公告","news/mobile/jbgg"));
    meuns.add(new DateType("美股","news/mobile/mgxw"));
    meuns.add(new DateType("港股","news/mobile/ggxw"));
    meuns.add(new DateType("基金","news/mobile/jjxw"));
    meuns.add(new DateType("理财","news/mobile/lcxw"));

  }
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值