开发者

Android: disable ListView selection

I have a listView that I re-display with the correct answer highlighted when the user selects an item. However, at that point I would like to disable selection of list view items; the user can only get to the next question by pressing a button. However, even if I disable the items like so:

   private void disableItemSelection() {
        ListView lv = getListView();
        for (int i = 0; i < lv.getChildCount(); i++){
            View v = lv.getChildAt(i);
            v.setEnabled(false);
        }
    }

...it still highlights t开发者_开发技巧he selection when the user selects it. Any thoughts?


The reason the scrolling is locked is because you are setting up at

ListView

I found a way to disable highlighting without locking the scroll. You need to set it up at ListAdapter as shown below:

ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText) {
           public boolean isEnabled(int position) 
            { 
                    return false; 
            } 
        };


You can add attribute to Listview XML layout to disable multiple high light rows as

android:listSelector="@android:color/transparent"               
android:cacheColorHint="@android:color/transparent"


I just hid the selected item with this:

listView.setSelector(new ColorDrawable(0));

And then you show it again by restoring to whatever drawable it was using previously:

wifiSsid.setSelector(R.drawable.listview_selector);


I used android:choiceMode="none" as documented here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜