所以,当我点击ListView
时,我有一个包含CheckBox
和TextView
的自定义CheckBox
,我想让它跨越TextView
我的代码适用于列表中的顶级项目,但如果我单击列表底部的CheckBox
它仍然会越过顶部TextView
我在xml中使用onClick方法我知道它不是ListView
的标准我只是不知道如何使用onListItemClick
来处理两个不同的xml元素
public void verify(View view)
{
CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
TextView t= (TextView) findViewById(R.id.label);
Toast.makeText(this, t.getText(), Toast.LENGTH_SHORT).show();
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="verify"
/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tasks"
android:textSize="30px" >
</TextView>
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values ) {
super(context, R.layout.activity_task_list, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_task_list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
}
答案 0 :(得分:0)
在onClick上使用XML功能时,只会为该特定View调用onClick方法。也就是说,编译时生成onClick回调的代码将无法知道您在运行时生成更多与您希望处理点击回调的View相同的View。这或多或少是为什么只有第一个TextView是虚线的原因,因为它与第一个生成的CheckBox绑定,唯一的CheckBox接收回调。
如果您将代码更改为:
会发生什么public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values ) {
super(context, R.layout.activity_task_list, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_task_list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
CheckBox cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
}
});
return rowView;
}
如果这不起作用,那么可能在你应用适配器的地方:
MobileArrayAdapter mobileArrayAdapter = new MobileArrayAdapter(this,
R.layout.activity_task_list,yourvalues);
list = (ListView) findViewById(R.id.yourlist);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// fetch TextView from the view
textView.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
});
以上似乎适用于:Custom ArrayAdapter onClick position & row highlight