【发布时间】:2018-10-14 20:33:46
【问题描述】:
我阅读了很多线程并进行了很多测试,但没有成功。
在一个活动中,我有一个列表视图连接到 ResourceCursorAdapter 类型的适配器。
在适配器的方法(bindView)中,我定义了listview每一行的自定义布局。
每行有2个ImageButton、2个TextView和1个Checkbox:布局如下。
<?xml version="1.0" encoding="utf-8"?>
<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="50dp"
android:orientation="horizontal"
android:weightSum="100"
tools:context=".Main"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/chebag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8" />
<TextView
android:id="@+id/texbag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="68"
android:gravity="left|center_vertical"
android:lines="3"
android:maxLines="3"
android:text="Item"
android:textSize="21sp" />
<ImageButton
android:id="@+id/butdec"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="@null"
android:src="@drawable/meno"
android:clickable="false"/>
<TextView
android:id="@+id/texnum"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="12"
android:gravity="center_vertical|center_horizontal"
android:text="100"
android:textSize="20sp" />
<ImageButton
android:id="@+id/butinc"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="@null"
android:src="@drawable/piu"
android:clickable="false" />
</LinearLayout>
在Activity中我准备了name(lismain)的listview的监听器;使用布局的语法每次点击 在列表项中触发方法(onItemClick)。
AdapterView.OnItemClickListener mainlistener= new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,final int position, long id) {
}
};
lismain.setOnItemClickListener(mainlistener);
我需要这两件事: a) 当在 (onItemClick) 我无法确定用户点击了哪个子视图 (行本身或里面的ImageButtons:有不同的处理)
b) 我更喜欢在 Activity 中而不是在适配器中处理点击事件:如果 ImageButtons 触发例如 (onClick) 方法也可以,但我也尝试不使用成功。
感谢支持
【问题讨论】: