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

来源:互联网 发布:lol域名注册 编辑:程序博客网 时间:2024/06/06 01:23

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

布局:

<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);    }}




阅读全文
0 0