横向菜单滑动选择viewpager+tablayout+fragment

点击与滑动时,菜单下的横线自带滑动动画;代码如下:

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    >


   

    <android.support.design.widget.TabLayout
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabIndicatorColor="@color/y"
        app:tabIndicatorHeight="5dp"
        app:tabSelectedTextColor="@color/j"
        app:tabTextColor="@color/b2">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/v1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>

代码:

public class EEAActivity extends AppCompatActivity {

    private Context mContext;
   
    TabLayout mTabLayout;
    ViewPager mViewPager;
    OneFragment mOneFragment = new OneFragment();
    TwoFragment mTwoFragment = new TwoFragment();
    List<Fragment> mFragmentList = new ArrayList<>();
    List<String> mList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.e_ea);
        mContext = this;
        mFragmentList.add(mOneFragment);
        mFragmentList.add(mTwoFragment);
        mList.add("one");
        mList.add("two");
       
        mTabLayout = (TabLayout) findViewById(R.id.t1);
        mViewPager = (ViewPager) findViewById(R.id.v1);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

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

            @Override
            public int getCount() {
                return 2;
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
    }

}

fragment代码:

public class OneFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.e_ea_f1,null);
    }
}

public class TwoFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.e_ea_f2,null);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值