美文网首页高级UI
TabLayout + ViewPager 使用,禁止ViewP

TabLayout + ViewPager 使用,禁止ViewP

作者: 穿越平行宇宙 | 来源:发表于2019-07-19 17:45 被阅读45次

    1. ViewPager 取消左右滑动,自定义一个ViewPager,代码如下:

    NoScrollViewPager .java

    public class NoScrollViewPager extends ViewPager {
    
        // 定义一个是否可以滑动的boolean 值
        private boolean isCanScroll = false;
    
        public NoScrollViewPager(@NonNull Context context) {
            super(context);
        }
    
        public NoScrollViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
    
        // 滑动到指定位置
        @Override
        public void scrollTo(int x, int y) {
            super.scrollTo(x, y);
        }
    
        // 触摸事件
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            if (isCanScroll) {
                return super.onTouchEvent(ev);
            } else {
                return false;
            }
        }
    
        // 设置当前显示的布局
        @Override
        public void setCurrentItem(int item) {
            super.setCurrentItem(item);
        }
    
        // 设置当前显示的布局,并定义滑动方式
        @Override
        public void setCurrentItem(int item, boolean smoothScroll) {
            super.setCurrentItem(item, smoothScroll);
        }
    
    
        // 拦截触摸事件
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (isCanScroll) {
                return super.onInterceptTouchEvent(ev);
            } else {
                return false;
            }
        }
    }
    

    2. UI布局

    activitu_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="2dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorPrimary" />
    
    
        <com.example.administrator.tablayoutdemo.NoScrollViewPager
            android:id="@+id/noScrollViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    
    </LinearLayout>
    
    1. UI修改
      MainActivity.java
    public class MainActivity extends AppCompatActivity {
    
        private TabLayout mTablayout;
        private NoScrollViewPager mNoScrollViewPager;
        private List<String> titles = new ArrayList<>();
        private List<Fragment> fragments = new ArrayList<>();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            initSet();
    
        }
    
        private void initSet() {
    
            titles.add("aaaa");
            titles.add("bbbb");
            titles.add("cccc");
            titles.add("dddd");
            titles.add("eeee");
            titles.add("ffff");
    
            for (int i = 0; i < titles.size(); i++) {
                fragments.add(new BlankFragment());
            }
    
            MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
            mNoScrollViewPager.setAdapter(adapter);
            mTablayout.setupWithViewPager(mNoScrollViewPager);
    
    
        }
    
        private void initView() {
            mTablayout = (TabLayout) findViewById(R.id.tablayout);
            mNoScrollViewPager = (NoScrollViewPager) findViewById(R.id.noScrollViewPager);
        }
    
        class MyAdapter extends FragmentPagerAdapter {
    
            public MyAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
    
            @Override
            public int getCount() {
                return fragments.size();
            }
    
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        }
    
    
    }
    

    相关文章

      网友评论

        本文标题:TabLayout + ViewPager 使用,禁止ViewP

        本文链接:https://www.haomeiwen.com/subject/fhvklctx.html