Browse Source

add disallowInterceptTouchEvent method in BannerViewPager.java

zhpanvip 4 years ago
parent
commit
88861d2bc3

+ 1 - 0
app/build.gradle

@@ -64,4 +64,5 @@ dependencies {
     implementation "androidx.navigation:navigation-fragment:2.2.2"
     implementation "androidx.navigation:navigation-fragment:2.2.2"
     implementation "androidx.navigation:navigation-ui:2.2.2"
     implementation "androidx.navigation:navigation-ui:2.2.2"
     implementation 'com.tencent.bugly:crashreport:3.2.1'
     implementation 'com.tencent.bugly:crashreport:3.2.1'
+    implementation 'com.google.android.material:material:1.0.0'
 }
 }

+ 11 - 3
app/src/main/java/com/example/zhpan/circleviewpager/activity/MainActivity.kt

@@ -45,10 +45,11 @@ class MainActivity : AppCompatActivity() {
     private fun initData() {
     private fun initData() {
         with(vp_fragment) {
         with(vp_fragment) {
             adapter = AdapterFragmentPager(this@MainActivity)
             adapter = AdapterFragmentPager(this@MainActivity)
-            isUserInputEnabled = true
+            isUserInputEnabled = false
             registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
             registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                 override fun onPageSelected(position: Int) {
                 override fun onPageSelected(position: Int) {
                     super.onPageSelected(position)
                     super.onPageSelected(position)
+                    vp_fragment.isUserInputEnabled = true
                     rg_tab?.check(getCheckedId(position))
                     rg_tab?.check(getCheckedId(position))
                 }
                 }
             })
             })
@@ -57,7 +58,10 @@ class MainActivity : AppCompatActivity() {
 
 
     private fun getCheckedId(position: Int): Int {
     private fun getCheckedId(position: Int): Int {
         return when (position) {
         return when (position) {
-            0 -> R.id.rb_home
+            0 -> {
+                vp_fragment.isUserInputEnabled = false
+                R.id.rb_home
+            }
             1 -> R.id.rb_add
             1 -> R.id.rb_add
             2 -> R.id.rb_find
             2 -> R.id.rb_find
             3 -> R.id.rb_others
             3 -> R.id.rb_others
@@ -67,8 +71,12 @@ class MainActivity : AppCompatActivity() {
 
 
     private fun setListener() {
     private fun setListener() {
         rg_tab?.setOnCheckedChangeListener { _, checkedId ->
         rg_tab?.setOnCheckedChangeListener { _, checkedId ->
+            vp_fragment.isUserInputEnabled = true
             when (checkedId) {
             when (checkedId) {
-                R.id.rb_home -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_HOME, true)
+                R.id.rb_home -> {
+                    vp_fragment.isUserInputEnabled = false
+                    vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_HOME, true)
+                }
                 R.id.rb_add -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_FIND, true)
                 R.id.rb_add -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_FIND, true)
                 R.id.rb_find -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_INDICATOR, true)
                 R.id.rb_find -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_INDICATOR, true)
                 R.id.rb_others -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_OTHERS, true)
                 R.id.rb_others -> vp_fragment.setCurrentItem(AdapterFragmentPager.PAGE_OTHERS, true)

+ 5 - 5
app/src/main/java/com/example/zhpan/circleviewpager/fragment/HomeFragment.java

@@ -92,6 +92,10 @@ public class HomeFragment extends BaseFragment {
     }
     }
 
 
     private void initRecyclerView(View view) {
     private void initRecyclerView(View view) {
+        mViewPagerHorizontal = view.findViewById(R.id.banner_view);
+        mRlIndicator = view.findViewById(R.id.layout_indicator);
+        mTvTitle = view.findViewById(R.id.tv_title);
+        mIndicatorView = view.findViewById(R.id.indicator_view);
         recyclerView = view.findViewById(R.id.recycler_view);
         recyclerView = view.findViewById(R.id.recycler_view);
         recyclerView.setLayoutManager(new LinearLayoutManager(getMContext()));
         recyclerView.setLayoutManager(new LinearLayoutManager(getMContext()));
         recyclerView.addHeadView(getHeaderView(), true);
         recyclerView.addHeadView(getHeaderView(), true);
@@ -173,7 +177,7 @@ public class HomeFragment extends BaseFragment {
                     }
                     }
                 })
                 })
                 .setOnPageClickListener(this::onPageClicked).create();
                 .setOnPageClickListener(this::onPageClicked).create();
-
+        mViewPagerHorizontal.disallowInterceptTouchEvent(true);
         mViewPagerVertical
         mViewPagerVertical
                 .setAutoPlay(true)
                 .setAutoPlay(true)
                 .setScrollDuration(500)
                 .setScrollDuration(500)
@@ -201,11 +205,7 @@ public class HomeFragment extends BaseFragment {
 
 
     private View getHeaderView() {
     private View getHeaderView() {
         headerView = LayoutInflater.from(getMContext()).inflate(R.layout.item_header_view, recyclerView, false);
         headerView = LayoutInflater.from(getMContext()).inflate(R.layout.item_header_view, recyclerView, false);
-        mRlIndicator = headerView.findViewById(R.id.layout_indicator);
-        mViewPagerHorizontal = headerView.findViewById(R.id.banner_view);
         mViewPagerVertical = headerView.findViewById(R.id.banner_view2);
         mViewPagerVertical = headerView.findViewById(R.id.banner_view2);
-        mTvTitle = headerView.findViewById(R.id.tv_title);
-        mIndicatorView = headerView.findViewById(R.id.indicator_view);
         return headerView;
         return headerView;
     }
     }
 }
 }

+ 61 - 3
app/src/main/res/layout/fragment_home.xml

@@ -1,18 +1,76 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.coordinatorlayout.widget.CoordinatorLayout 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_width="match_parent"
     android:layout_height="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
     android:orientation="vertical">
 
 
+    <com.google.android.material.appbar.AppBarLayout
+        android:id="@+id/app_bar"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <com.google.android.material.appbar.CollapsingToolbarLayout
+            android:id="@+id/toolbar_layout"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            app:contentScrim="?attr/colorPrimary"
+            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
+            app:statusBarScrim="@android:color/transparent">
+
+            <RelativeLayout
+                android:id="@+id/rl_banner1"
+                android:layout_width="match_parent"
+                android:layout_height="@dimen/dp_200">
+
+                <com.zhpan.bannerview.BannerViewPager
+                    android:id="@+id/banner_view"
+                    android:layout_width="match_parent"
+                    android:layout_height="@dimen/dp_200" />
+
+                <!--这里为了设置标题故用了自定义Indicator,如果无需标题则没必要将Indicator定义在此处-->
+                <RelativeLayout
+                    android:id="@+id/layout_indicator"
+                    android:layout_width="match_parent"
+                    android:layout_height="@dimen/dp_35"
+                    android:layout_alignParentBottom="true"
+                    android:background="#77000000"
+                    android:visibility="gone">
+
+                    <TextView
+                        android:id="@+id/tv_title"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_centerVertical="true"
+                        android:layout_marginStart="@dimen/dp_15"
+                        android:textColor="@color/white"
+                        android:textSize="@dimen/sp_14"
+                        tools:text="This is Title" />
+
+                    <com.zhpan.indicator.IndicatorView
+                        android:id="@+id/indicator_view"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_alignParentEnd="true"
+                        android:layout_centerVertical="true"
+                        android:layout_marginEnd="@dimen/dp_15" />
+                </RelativeLayout>
+            </RelativeLayout>
+
+        </com.google.android.material.appbar.CollapsingToolbarLayout>
+    </com.google.android.material.appbar.AppBarLayout>
+
     <com.scwang.smartrefresh.layout.SmartRefreshLayout
     <com.scwang.smartrefresh.layout.SmartRefreshLayout
         android:id="@+id/refresh_layout"
         android:id="@+id/refresh_layout"
         android:layout_width="match_parent"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_height="match_parent"
-        android:overScrollMode="never">
+        android:overScrollMode="never"
+        app:layout_behavior="@string/appbar_scrolling_view_behavior">
 
 
         <com.example.zhpan.circleviewpager.recyclerview.ui.CustomRecyclerView
         <com.example.zhpan.circleviewpager.recyclerview.ui.CustomRecyclerView
             android:id="@+id/recycler_view"
             android:id="@+id/recycler_view"
             android:layout_width="match_parent"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />
             android:layout_height="match_parent" />
     </com.scwang.smartrefresh.layout.SmartRefreshLayout>
     </com.scwang.smartrefresh.layout.SmartRefreshLayout>
-</LinearLayout>
+</androidx.coordinatorlayout.widget.CoordinatorLayout>

+ 1 - 43
app/src/main/res/layout/item_header_view.xml

@@ -3,51 +3,9 @@
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_height="match_parent"
-    android:visibility="gone"
     android:orientation="vertical">
     android:orientation="vertical">
-
-    <RelativeLayout
-        android:id="@+id/rl_banner1"
-        android:layout_width="match_parent"
-        android:layout_height="@dimen/dp_200">
-
-        <com.zhpan.bannerview.BannerViewPager
-            android:id="@+id/banner_view"
-            android:layout_width="match_parent"
-            android:layout_height="@dimen/dp_200" />
-
-        <!--这里为了设置标题故用了自定义Indicator,如果无需标题则没必要将Indicator定义在此处-->
-        <RelativeLayout
-            android:id="@+id/layout_indicator"
-            android:layout_width="match_parent"
-            android:layout_height="@dimen/dp_35"
-            android:layout_alignParentBottom="true"
-            android:background="#77000000"
-            android:visibility="gone">
-
-            <TextView
-                android:id="@+id/tv_title"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_centerVertical="true"
-                android:layout_marginStart="@dimen/dp_15"
-                android:textColor="@color/white"
-                android:textSize="@dimen/sp_14"
-                tools:text="This is Title" />
-
-            <com.zhpan.indicator.IndicatorView
-                android:id="@+id/indicator_view"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_alignParentEnd="true"
-                android:layout_centerVertical="true"
-                android:layout_marginEnd="@dimen/dp_15" />
-        </RelativeLayout>
-    </RelativeLayout>
-
     <com.zhpan.bannerview.BannerViewPager
     <com.zhpan.bannerview.BannerViewPager
         android:id="@+id/banner_view2"
         android:id="@+id/banner_view2"
         android:layout_width="match_parent"
         android:layout_width="match_parent"
-        android:layout_height="@dimen/dp_120"
-        android:layout_below="@id/rl_banner1"/>
+        android:layout_height="@dimen/dp_120" />
 </RelativeLayout>
 </RelativeLayout>

+ 19 - 1
bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java

@@ -194,13 +194,14 @@ public class BannerViewPager<T, VH extends BaseViewHolder<T>> extends RelativeLa
     @Override
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         boolean canIntercept = mViewPager.isUserInputEnabled() || mBannerPagerAdapter != null && mBannerPagerAdapter.getData().size() <= 1;
         boolean canIntercept = mViewPager.isUserInputEnabled() || mBannerPagerAdapter != null && mBannerPagerAdapter.getData().size() <= 1;
-        if (!canIntercept) {
+        if (!canIntercept || disallowIntercept) {
             return super.onInterceptTouchEvent(ev);
             return super.onInterceptTouchEvent(ev);
         }
         }
         switch (ev.getAction()) {
         switch (ev.getAction()) {
             case MotionEvent.ACTION_DOWN:
             case MotionEvent.ACTION_DOWN:
                 startX = (int) ev.getX();
                 startX = (int) ev.getX();
                 startY = (int) ev.getY();
                 startY = (int) ev.getY();
+                getParent().requestDisallowInterceptTouchEvent(true);
                 break;
                 break;
             case MotionEvent.ACTION_MOVE:
             case MotionEvent.ACTION_MOVE:
                 int endX = (int) ev.getX();
                 int endX = (int) ev.getX();
@@ -225,6 +226,9 @@ public class BannerViewPager<T, VH extends BaseViewHolder<T>> extends RelativeLa
         return super.onInterceptTouchEvent(ev);
         return super.onInterceptTouchEvent(ev);
     }
     }
 
 
+    private boolean disallowIntercept;
+
+
     private void onVerticalActionMove(int endY, int disX, int disY) {
     private void onVerticalActionMove(int endY, int disX, int disY) {
         if (disY > disX) {
         if (disY > disX) {
             if (!isCanLoop()) {
             if (!isCanLoop()) {
@@ -943,4 +947,18 @@ public class BannerViewPager<T, VH extends BaseViewHolder<T>> extends RelativeLa
         startLoop();
         startLoop();
     }
     }
 
 
+    /**
+     * 设置是否允许BVP对事件进行拦截,用于解决CoordinatorLayout+CollapsingToolbarLayout
+     * 在嵌套BVP时引起的滑动冲突问题。
+     * BVP在处理ViewPager2嵌套滑动冲突时,在{@link #onInterceptTouchEvent(MotionEvent)}方法中
+     * 对事件进行了处理导致CoordinatorLayout+CollapsingToolbarLayout的布局中滑动BVP的事件无效。
+     * 对于这种情况可以调用该方法来禁止BVP对事件进行拦截。
+     *
+     * @param disallowIntercept 是否允许BVP拦截触摸事件
+     */
+    public BannerViewPager<T, VH> disallowInterceptTouchEvent(boolean disallowIntercept) {
+        this.disallowIntercept = disallowIntercept;
+        return this;
+    }
+
 }
 }