我有一个包含ListView和RelativeLayout中的几个按钮的布局。我正在尝试让用户使用自定义绘制视图布局在页面上绘制。好消息是,我几乎所有的东西都在工作,并查看它应该如何工作,然而最后一个也是最令人沮丧的问题是ListView根本无法点击。我想在ListView的顶部绘制并使其可点击。
有趣的是,页面上的按钮仍然可以点击,只是ListView中的项目不可以。我认为现在发生的情况是,ListView是由适配器加载的,并首先加载(因此落后于所有内容(。我需要做些什么才能使ListView可点击?
我试过添加android:focusableInTouchMode="true"android:fcusable="true(,但没有什么区别。
我的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<ListView android:id="@+id/lv"
android:layout_height="match_parent"
android:layout_width="match_parent"
></ListView>
<com.my.app.PaintView
android:id="@+id/paintView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
Buttons Here.../>
</RelativeLayout>
对于您不想接收触摸事件的元素,focusable
属性应为false
。要覆盖PaintView
而不让它拦截触摸事件,可以添加一些XML属性:
<com.my.app.PaintView
...
android:focusable="false"
android:clickable="false" />
只要您没有将OnClickListener
或OnTouchListener
设置为PaintView,触摸事件就应该简单地通过它传递到它后面的视图。
设法找到了一个完美工作的解决方案(至少对我来说(。我想如果没有listview,这会容易得多,但我希望listview能像在画布上画画一样工作。我最终所要做的就是在具有ListView的主活动中设置一个onTouchListener,然后将其与MotionEvent一起发送到PaintView。简单且完美:
listview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
paintView.onTouchEvent(motionEvent);
return false;
}
});
在本例中,paintView.onTouchEvent实际上可以是自定义视图中的任何方法,只是为了简单起见,将其保持在"onTouchEvent"。