[英]Checkbox click event not triggered in android listview with checkbox
我正在嘗試用復選框實現一個android listview。 但是,我無法獲取復選框的點擊事件。
如果我將復選框監聽器放在適配器類上,我可以使代碼工作。 但是我需要主類的信息。
使用主類,我可以在單擊時獲取listview的事件,但不能使用lv.onItemClick獲取復選框。 但是,我需要單擊列表視圖一次,然后再次單擊復選框,以便觸發復選框事件。 我需要單獨的復選框事件和listview點擊事件。 有幫助嗎? 謝謝
XML FOR LIST在Relative布局之外仍然存在線性布局。
<RelativeLayout
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:descendantFocusability="blocksDescendants"
android:layout_weight="1">
<TextView android:id="@+id/list_item_entry_title_paper"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toLeftOf="@+id/cbPaper"
android:gravity="left"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:fadingEdge="horizontal" />
<TextView android:id="@+id/list_item_entry_summary_paper"
android:layout_alignParentLeft="true"
android:gravity="left"
android:layout_toLeftOf="@+id/cbPaper"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/list_item_entry_title_paper"
android:layout_alignLeft="@id/list_item_entry_title_paper"
android:textAppearance="?android:attr/textAppearanceSmall"
android:singleLine="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
/>
<CheckBox
android:button="@null"
android:id="@+id/cbPaper"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="@drawable/customcbpaper"
android:layout_centerVertical="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:choiceMode="multipleChoice"
android:gravity="right" />
</RelativeLayout>
主要事件處理器
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
checkbox = (CheckBox) view.findViewById(R.id.cbPaper);
checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.w("do something", "do something" );
} });
}
});
}
公共類CustAdapHeaderWithCbPaper擴展ArrayAdapter {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public CustAdapHeaderWithCbPaper(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
protected TextView a;
protected TextView b;
protected CheckBox cb;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder = null;
final int newPosition = position;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}
else //Not a section
{
EntryItemCheckBox ei = (EntryItemCheckBox)i;
v = vi.inflate(R.layout.list_item_entry_cb_paper, null);
holder = new ViewHolder();
holder.a = (TextView)v.findViewById(R.id.list_item_entry_title_paper);
holder.b = (TextView)v.findViewById(R.id.list_item_entry_summary_paper);
holder.cb = (CheckBox)v.findViewById(R.id.cbPaper);
final String titleCheck = ei.title.toString();
holder.cb.setTag(position);
holder.a.setText(ei.title);
holder.cb.setChecked(ei.selected);
v.setTag(holder);
v.setTag(R.id.list_item_entry_title_paper, holder.a);
v.setTag(R.id.list_item_entry_summary_paper, holder.b);
v.setTag(R.id.cbPaper, holder.cb);
}
}
return v;
}
}
您提的android:clickable="false"
的checkBox
中的XML ...所以復選框可以不聽點擊event..and最好把聽者的適配器類..
你setOnCheckedChangeListener
而不是setOnClickListener
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
您可以使用該物業
android:descendantFocusability="afterDescendants"
在你的listview xml中。 我認為這會有所幫助。 讓我知道結果。
在第一個if(i!=null)
條件之外找到復選框並執行您需要的相同工作
例如:
holder.cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.w("do something", "do something" );
} });
要獲取整個RelativeLayout
click事件,請在xml中提供id並執行相同操作
首先從Xml文件中刪除此行
android:button=null
並將此行添加到您的復選框
android:focusable="false"
然后,使用此代碼它將適合您
checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(checkBox.isChecked())
{
// do what You want to perform on when check box is checked
}
else
{
}
}
});
並為列表視圖提供單獨的事件,如下所示:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.