简体   繁体   中英

How to make only one widget focusable in a custom listview?

I have a custom listview with one checkbox and two textviews. I'm using a custom adapter to present the list view. The problem is that the onItemclick function doesn't work for the checkbox if I focus on the checkbox directly, it works only when the list is focused first and then checkbox.I want my listview to work similar to checkedtextview where the checkbox gains focus by default.Can anybody suggest me a workaround for this problem? Thank you.

Add this code to list view activity and change according to your specification.

View v;
LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.listview, null);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.check_box);
checkBox.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View view)
    {
        //Action  
    }
});

Try to add a click listener to your view returned with the getView() method as follows:

view.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
/// checkboxview.clearFocus()
/// checkboxview.requestFocus()
     }....

EDIT. onclicklistener for checkbox:

 CheckBox chckKill = new CheckBox(context);
            chckKill.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {
                //is chkIos checked?
        if (((CheckBox) v).isChecked()) {
        ///// your code here
        }    
      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM