Skip to content

Commit

Permalink
修复隐患的事件冲突问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen2018 committed Jun 27, 2019
1 parent 07c1a2f commit 8f0326d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.ellen.tableview.supertableview.adapter.superadapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

/**
* 解决与viewpager的滑动冲突问题
*
* @author: WEI
* @date: 2018/6/25
*/
public class TableHorizontalScrollView extends HorizontalScrollView {
private final static String TAG = "CusHorizontalScrollView";

public TableHorizontalScrollView(Context context) {
super(context);
}

public TableHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public TableHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public TableHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev);
}

/**
* 可以在此处理冲突
* @param ev
* @return
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// 还没滑到右边,请求父控件不要拦截我的事件,事件自己处理 true ;已经滑到右边,则事件交由父控件处理 false。
// getParent().requestDisallowInterceptTouchEvent(!isScrollToRight());
return super.dispatchTouchEvent(ev);
}

/**
* 也可以在此处理冲突
* @param ev
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent ev)
{
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
break;

case MotionEvent.ACTION_MOVE:
if (isScrollToLeft() || isScrollToRight())
{
// 把事件交给父控件处理,例如:viewpager滑动
getParent().requestDisallowInterceptTouchEvent(false);
}
break;

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// 请求父控件可以拦截事件
getParent().requestDisallowInterceptTouchEvent(false);
break;

default:
}
return super.onTouchEvent(ev);
}

/**
* 是否已经滑到了最右边
*
* @return
*/
private boolean isScrollToRight() {
return getChildAt(getChildCount() - 1).getRight() == getScrollX() + getWidth();
}

/**
* 是否已经滑到了最左边
* @return
*/
private boolean isScrollToLeft() {
return getScrollX() == 0;
}
}
8 changes: 4 additions & 4 deletions tableview/src/main/res/layout/layout_view_table.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<HorizontalScrollView
<com.ellen.tableview.supertableview.adapter.superadapter.TableHorizontalScrollView
android:id="@+id/horizontalScrollView_x"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -14,7 +14,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</HorizontalScrollView>
</com.ellen.tableview.supertableview.adapter.superadapter.TableHorizontalScrollView>

<GridLayout
android:id="@+id/grid_layout_xy"
Expand Down Expand Up @@ -49,7 +49,7 @@
android:background="#CFCFCF"
android:visibility="gone" />

<HorizontalScrollView
<com.ellen.tableview.supertableview.adapter.superadapter.TableHorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -60,7 +60,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

</HorizontalScrollView>
</com.ellen.tableview.supertableview.adapter.superadapter.TableHorizontalScrollView>

</LinearLayout>

Expand Down

0 comments on commit 8f0326d

Please sign in to comment.