[英]Listview click listener not responding
我有一个列表视图,并希望对其执行clickable事件。 我以前做过 但现在它不起作用。 我也添加了XML。 在列表视图中单击时,我只需要从一种活动移到另一种活动。
CustomFinalSubmit_ItemDetail item = new CustomFinalSubmit_ItemDetail(Final_Submit.this , R.layout.customview_finalsubmit_itemdetails, itemInfo);
itemList.setAdapter(item);
itemList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
FinalSubmitItem pos = itemInfo.get(position);
String itemType = pos.getItemType();
String itemCountry = pos.getItemCountry();
String itemSerial = pos.getItemNo();
pos.setChecked(true);
Intent inn = new Intent(getApplicationContext(), FinalSubmitDetails.class);
inn.putExtra("itemType", itemType);
inn.putExtra("itemCountry", itemCountry);
inn.putExtra("itemSerial", itemSerial);
startActivity(inn);
}
});
这是我的主要Xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/green">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_marginTop="10dip"
android:orientation="vertical"
android:background="#0B3B0B">
<ListView
android:id="@+id/CustomerDetailList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellow"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="@+id/Itemtype"
android:paddingTop="5dp"
android:background="@color/green"
android:textColor="@color/yellow"
android:layout_marginLeft="5dip"
android:text="Item Type"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:id="@+id/txtcountry"
android:background="@color/green"
android:textColor="@color/yellow"
android:text="Country"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:id="@+id/txtItemNumber"
android:background="@color/green"
android:textColor="@color/yellow"
android:text="Item No."/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:id="@+id/itemChck"
android:button="@drawable/custom_checkbox"
android:paddingTop="5dp"
android:padding="5dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_margin="5dp"
android:layout_height="1dp"
android:background="@color/white"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/itemList"
android:background="#088A08"
android:divider="@color/white"
android:dividerHeight="1dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我这样做是这样的:
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
your code
}});
只需将这么多代码添加到我的customview中
最近,在将ToggleButton添加到列表项后,我遇到了同样的问题。 对我来说,解决方案是将android:descendantFocusability="blocksDescendants"
到项目的布局。
这是整个XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:paddingLeft="15dp"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="@+id/watch_roadName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Addresse"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<ToggleButton
android:id="@+id/watch_button_arrived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/watch_roadName"
android:layout_alignParentRight="true"
android:textOn="@string/arrived"
android:textOff="@string/arrived"
android:text="@string/arrived" />
</RelativeLayout>
可以在以下位置找到针对同一问题的类似答案:
如何使用列表中的按钮在Listactivity中触发onListItemClick?
和这里
在充气机布局的小部件(Textview或按钮)中,只需添加:
android:focusable="false"
android:focusableInTouchMode="false"
在充气机的父级布局中添加:
android:descendantFocusability="blocksDescendants"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.