我制作了自己的自定义ListView
,每行都有 2 TextView
。
我打算在textview
可点击的每一行中生成listview
个。因此,每当用户点击其中任何一个时,它都会将他重定向到其他地方。
实际上,当用户点击ListView
项时,会点击所有内容,textviews
被忽略。
总之,当我点击ListView
项目时,我不希望Listview项目做出响应,我希望Textview
内的Listview
能够做出响应。
编辑:
以下是我如何自定义视图:
String[] from = {"value1", "value2"};
int[] to = {R.id.label, R.id.label2};
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.rowlayout, from, to);
答案 0 :(得分:3)
在Adapter类中,在getview()方法中,您可以像这样执行onclick操作
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
if (row == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.listitem, null);
holder.txtTitle1 = (TextView) row.findViewById(R.id.heading1);
holder.txtTitle2 = (TextView) row.findViewById(R.id.heading2);
holder.txtTitle1 .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
holder.txtTitle2 .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
return row;
}
/////
///////////
static class Holder {
TextView txtTitle1;
TextView txtTitle2;
}
答案 1 :(得分:2)
set your TextView in your layot.xml
<TextView
android:id="@+id/YourID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
/>
,然后在自定义适配器上
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
if (row == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.youLayout, null);
holder.txt1 = (TextView) row.findViewById(R.id.tx1);
holder.txt2 = (TextView) row.findViewById(R.id.tx2);
holder.txt1 .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your statements
}
});
holder.txt2 .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your statements
}
});
}
return row;
}