![](/img/trans.png)
[英]Does View.OnTouchListener run on other thread than the UI thread?
[英]View.OnTouchListener() does not work on parent layout
我在父布局上設置了View.OnTouchListener,但它似乎不起作用。
以下是UI的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:id="@+id/mainActivityLinearLayout"
android:background="@drawable/listviewborder"
tools:context=".MainActivity" >
<View
android:id="@+id/lineView"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/gray" />
<ListView
android:id="@+id/gameListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:dividerHeight="2dp" />
</LinearLayout>
這是在父布局上設置的OnTouchListener:
mainActivityView = this.findViewById(R.id.mainActivityLinearLayout);
mainActivityView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch test activity");
return true;
}
});
永遠不會調用onTouch()
並且永遠不會打印字符串。
並不是說我已經為ListView實現了OnItemClick,我只是希望能夠檢測整個布局上的觸摸。
我也試過了
gameListView = (ListView) findViewById(R.id.gameListView);
gameListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch test list");
return true;
}
});
雖然這也不起作用。
為什么父布局沒有觸摸事件? 我怎樣才能讓它運轉起來?
使此類檢測手勢監聽器
class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; // right to left swipe if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Log.d("asdsa", "left"); // Toast.makeText(SelectFilterActivity.this, "Left Swipe", // Toast.LENGTH_SHORT).show(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Log.d("asdsa", "right"); Fragment fr = getActivity().getSupportFragmentManager() .findFragmentByTag("list_items"); if (fr != null) { Log.d("asdsa", "not null"); if (fr.isVisible()) { Log.d("asdsa", "visible"); getActivity().getSupportFragmentManager() .beginTransaction() .setCustomAnimations(0, R.anim.right_out) .remove(fr).commit(); } } // Toast.makeText(SelectFilterActivity.this, "Right Swipe", // Toast.LENGTH_SHORT).show(); } } catch (Exception e) { // nothing } return false; }
在活動中聲明此變量:
private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; private GestureDetector gestureDetector; View.OnTouchListener gestureListener;
現在你的活動onCreate里面這樣做:
gestureDetector = new GestureDetector(getActivity(), new MyGestureDetector()); gestureListener = new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }; listView.setOnTouchListener(gestureListener);
最后檢查手勢是否發生,對我來說它完美無缺。
我剛剛遇到同樣的問題。 我認為這是因為觸摸事件永遠不會到達LinearLayout。 我為解決這個問題所做的是攔截cutom視圖的觸摸事件
public class MyPager extends ViewPager {
public RecentsPager(Context context) {
super(context);
}
public RecentsPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Do not allow swiping to switch between child pager pages
return false;
}
}
當你想要觸摸時,返回true。 如果返回false,則觸摸事件將傳遞給子視圖。
希望這有助於尋找它的人。
編輯:你也可以嘗試為父布局添加屬性android:clickable="true"
。 這可能會影響點擊子視圖(我在某些情況下使用它但不確定它適用於所有人)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.