简体   繁体   中英

Disable click or touch for items in ListActivity Xamarin.Android

I have an Activity inherited from ListActivity. Inside I have a SimpleListItemChecked layout. I have an Ilist called codes. I have made items checked if they contain certain word which works fine. I now want to make the items not-editable/clickable.

public class ScanHistoryActivity : ListActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        var codes = Intent.Extras.GetStringArrayList("Codes");

        base.OnCreate(bundle);
        codes.ToList();
        ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, codes);
        ListView lv = FindViewById<ListView>(Android.Resource.Id.List);
        lv.ChoiceMode = ChoiceMode.Multiple;

        foreach (var c in codes)
        {
            if (c.Contains("Success"))
            {
                int position = codes.IndexOf(c);
                lv.SetItemChecked(position, true); 
                // Here i tried: 
                // lv.clickable = false; but didn't work
            }
        }       
    }
}

Tried this but not working unfortunatley..

       protected override void OnCreate(Bundle bundle)
    {
        var codes = Intent.Extras.GetStringArrayList("Codes");

        base.OnCreate(bundle);
        codes.ToList();
        ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, codes);
        ListView lv = FindViewById<ListView>(Android.Resource.Id.List);
        lv.ChoiceMode = ChoiceMode.Multiple;
        lv.Clickable = false;
        lv.Focusable = false;
        foreach (var c in codes)
        {
            long id = lv.GetItemIdAtPosition(codes.IndexOf(c));
            if (c.Contains("Success"))
            {
                int position = codes.IndexOf(c);
                lv.SetItemChecked(position, true);
                OnListItemClick(lv,lv, position, id);
            }
        }
    }

           protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        l.Clickable = false;
        l.Focusable = false;
    }

Tried this as well: Made my own adapter to try get it working better: protected override void OnCreate(Bundle bundle) { var codes = Intent.Extras.GetStringArrayList("Codes");

        base.OnCreate(bundle);
        codes.ToList();
                    ListAdapter = new ScanHistoryAdapter(this, codes.ToArray());
        ListView lv = FindViewById<ListView>(Android.Resource.Id.List);
        lv.ChoiceMode = ChoiceMode.Multiple;
        lv.Clickable = false;
        lv.Focusable = false;
        foreach (var c in codes)
        {
            long id = lv.GetItemIdAtPosition(codes.IndexOf(c));
            if (c.Contains("Success"))
            {
                int position = codes.IndexOf(c);
                lv.SetItemChecked(position, true);
                View v = FindViewById(ListAdapter.GetItemId();
                OnListItemClick(lv,lv, position, id);
            }
        }
    }

    protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        l.Clickable = false;
        l.Focusable = false;
    }

simply We use to pass false value to setClickable method to disable clicking in java like this way.

ListView myList = (ListView) findViewById(R.id.list_view_id);
myList.setClickable(false);

Try once !!! There may be some methods like same...

We have some method to override and make specific item unclickable like this way

@Override
public boolean isEnabled(int position) {
if(position == your_item_pos) {
    return false;
}
return true;
}

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