ListView 与 HorizontalScrollView OnItemClick 不起作用
所以很快我就有了一个带有自定义适配器的listView,它会膨胀一个包含horizontalScrollView以及textview等的视图。我遇到的问题是,当我尝试将侦听器附加到此listView时,它没有收到任何回调。
我相信问题与以下事实有关:我的列表项包含一个滚动视图,该视图正在拦截单击事件(尽管我认为它应该只拦截其他手势)。
代码...(我的列表项 xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey" >
<TextView
android:id="@+id/headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="4dp"
android:textColor="@color/text_header"
android:textStyle="bold"
android:text="TextView" />
</RelativeLayout>
<View
android:background="@color/border"
android:layout_width="fill_parent"
android:layout_height="1px" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
</LinearLayout>
</HorizontalScrollView>
<View
android:background="@color/border"
android:layout_width="fill_parent"
android:layout_height="1px" />
</LinearLayout>
,然后在我的 onCreate...
lv.setAdapter(myobj.adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.w("dsdsds", "sdsdsds");
}});
任何帮助或建议将不胜感激
So really quickly i have a listView with a custom adapter and it inflates a view that contains a horizontalScrollView as well as a textview etc. The problem I am having is that when i try to attach a listener to this listView it is not receiving any callbacks.
I believe the problem has to do with the fact that my list item contains a scroll view which is intercepting the click events (although i thought it should only intercept other gestures).
code... (my list item xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey" >
<TextView
android:id="@+id/headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="4dp"
android:textColor="@color/text_header"
android:textStyle="bold"
android:text="TextView" />
</RelativeLayout>
<View
android:background="@color/border"
android:layout_width="fill_parent"
android:layout_height="1px" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
</LinearLayout>
</HorizontalScrollView>
<View
android:background="@color/border"
android:layout_width="fill_parent"
android:layout_height="1px" />
</LinearLayout>
and then in my onCreate...
lv.setAdapter(myobj.adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.w("dsdsds", "sdsdsds");
}});
Any help or suggestions would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其添加到最顶层的布局:
android:descendantFocusability="blocksDescendants"
它对我有用,现在 onItemClickListener 被触发。
Add this to the topmost Layout:
android:descendantFocusability="blocksDescendants"
It did the trick for me, now onItemClickListener is being fired.
您可以在适配器的 getView() 方法中调用 LinearMain 的 setOnClickListener 。
You can call setOnClickListener for linearMain in the getView() method of your adapter.
对于ScrollView,您应该使用OnTouchListener,例如:
并在适配器的getView方法中:
for ScrollView you should use OnTouchListener, like:
and in getView method of adapter:
在 list_item.xml 中使用一些 id 标记您的布局:
打开自定义适配器,在
getView(...)
中写入:适配器下面写入:
并在适配器的构造函数中分配一个回调:
在 包含 ListView 的活动/片段创建一个适配器,如下所示:
Inside list_item.xml mark your layout with some id:
Open your custom adapter, in
getView(...)
write:Below in the adapter write:
And in the constructor of the adapter assign a callback:
In your activity/fragment that contains a ListView create an adapter like so: