Force a ListView item to stay “pressed” after bein

2019-01-21 14:10发布

I have a ListView that opens another activity when an item row is clicked via a onItemClick listener.

I would like that row to stay in its pressed state from the time it is clicked to the time the screen switches to a new activity. I think this would be a clearer experience for the user and you see this kind of thing with most buttons that open/close dialogs or switch activities.

I tried setting view.setPressed(true) in the onItemClick() listener but it seems to get called just a moment after the press state changed back to normal because it flickers slightly.

For example:

mListView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       view.setPressed(true);
       //start an activity
   }
});

That code almost works except for the flicker (User presses the list item and it turns to its pressed state, then user lets go (completing the click) and it turns back to its normal state for a split second before turning back to the pressed state from the setPressed(true) call)

Any ideas?

Thanks

Edit: I should mention that I am using an xml drawable selector to define the normal, pressed, selected, etc states for the background of the list.

7条回答
Ridiculous、
2楼-- · 2019-01-21 14:55

The best way you can do this before Android 3.0 is to use a custom state in a custom view or to change your view's background color from the adapter. Starting with Android 3.0 you can use the new state_activated to keep a list item selected.

查看更多
登录 后发表回答