[英]ListView OnItemClickListener not working on second adapter
I have a fragment with a ListView and two buttons above this ListView. 我有一个带有ListView的片段,该ListView上方有两个按钮。 The two buttons call the following methods which reload the ListView with a different adapter for the data source. 这两个按钮调用以下方法,这些方法为数据源使用不同的适配器重新加载ListView。 The problem is with the second one. 问题出在第二个。 When calling the showWhenWhere(), the onItemClick() is never fired. 调用showWhenWhere()时,永远不会触发onItemClick()。
private void showAtoZ() {
lineupAdapter = new LineupAdapter(parentView.getContext());
listView.setAdapter(lineupAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
System.out.println("A to Z tapped");
int itemPosition = position;
if(parentActivity != null) {
parentActivity.changeFragment(new BandFragment());
}
}
});
loadData();
}
private void showWhenWhere() {
whenWhereAdapter = new LineupWhenWhereAdapter(parentView.getContext());
listView.setAdapter(whenWhereAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
System.out.println("When/Where tapped");
int itemPosition = position;
if(parentActivity != null) {
parentActivity.changeFragment(new BandFragment());
}
}
});
loadData();
}
UPDATE - I determined it's caused by the XML for the whenWhereAdapter, which I'm pasting below: 更新-我确定是由whenWhereAdapter的XML引起的,我在下面粘贴:
<?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="150dp" android:id="@+id/lineupWhenWhereView">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bandImageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:background="@android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textView"
android:background="@drawable/list_row_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Elijah & The Moon"
android:id="@+id/bandTitle"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="30dp"
android:textSize="23sp"
android:maxLines="2"
android:textColor="@android:color/white" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@android:color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:00 PM"
android:id="@+id/whenLabel"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="@android:color/holo_orange_light"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:id="@+id/divider"
android:layout_toRightOf="@+id/whenLabel"
android:layout_toEndOf="@+id/whenLabel"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="6dp"
android:layout_alignParentBottom="true"
android:textIsSelectable="true"
android:textSize="13sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="West Stage"
android:id="@+id/whereLabel"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/divider"
android:layout_toEndOf="@+id/divider"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="12sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO SCHEDULE"
android:id="@+id/addButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@android:color/transparent"
android:drawableLeft="@drawable/add_to_schedule"
android:drawablePadding="5dp"
android:textColor="@android:color/holo_orange_light"
android:layout_marginRight="10dp" />
</RelativeLayout>
</RelativeLayout>
Any idea why this would cause the OnItemClickListener to not work? 知道为什么这会导致OnItemClickListener无法正常工作吗?
ListView
only maintains one OnItemClickListener
at time. ListView
仅维护一个OnItemClickListener
。 When you set the second listener, only that listener will be called. 设置第二个侦听器时,将仅调用该侦听器。
-------EDIT ------- -------编辑-------
After seeing your row layout, I think you should set android:descendantFocusability="blocksDescendants"
on your very first RelativeLayout. 看到行布局后,我认为您应该在第一个RelativeLayout上设置android:descendantFocusability="blocksDescendants"
。
That happen because TextViews && Buttons inside layout are blocking click event. 发生这种情况是因为布局内的TextViews && Button阻止了click事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.