Can I make a ListView item not selectable?
Asked Answered
P

3

37

I am implementing an endless ListView (like in the Twitter app). I want to make the last item not selecteble. So that if the penultimate item is selected and I scroll down with my trackball, nothing happens. I tried setting android:focusable="false" and android:cickable="false" but I didn't notice any chnage.

Passepartout answered 26/2, 2011 at 11:22 Comment(1)
please add the only answer as the correct!Subaqueous
C
97

It's pretty easy, in your adapter you can override the method isEnabled(int position) and return false for this item.

Cambrai answered 26/2, 2011 at 11:23 Comment(5)
Documentation of the method signature: developer.android.com/reference/android/widget/…Toplevel
shockingly, BaseExpandableListAdapter doesn't have this method :(Delamare
Actually it wont help much (you could also do convertView.setEnabled(false) in getView() for such items). But it could have its own item selector like in my case, for normal item it has orange bg selector and for disabled one - gray colored bg. So it stays clickable visually. However if you additionally add some checkbox or something making it invisible so it would intercept focus then it would be like unclickable.Cordelier
Thanks for the help, in my case I didn't wanted one item to triger the actionmode so everytime that item was selected i just called mode.finish() that was saved from the onCreateActionModeCruse
Override ArrayAdapter's isEnabled() method sets the whole View as DISABLED, so if any ColorStateList is used in the ListView (or its children/items) the Disabled color will be used instead of the Enabled one.Illegal
P
3

if you're using custom array adapter just override this method.

@Override
public boolean isEnabled(int position) {
    return false;
}
Playboy answered 17/12, 2017 at 19:5 Comment(0)
I
0

If you want to get the same effect without having to have a custom adapter, you make the OnClickListener ignore that item when tapped and then set a solid background color for the item's view, so it doesn't highlight when tapped.

Infestation answered 26/9, 2014 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.