7

I'm working on creating a swipe-to-dismiss list view adapter. My basic methodology is to wrap the list item's view as the second view in a ViewPager and provide the necessary callbacks in the item change listener of the ViewPager. Through much pain I've got the View recycler working as intended, as well as ViewHolder and ViewBinder patterns implemented. I even managed to keep the ListView from taking over the touch events while the ViewPager is being scrolled without having to make a custom subclass of ListView (I can do it all from the Adapter).

Where I'm running into trouble is getting the selector and the OnItemClickListener to work. After looking at ListView's source it seemed that by overriding the ViewPager's hasFocusable() method to always return false (later on I'll pull this value from the child view) these things should have been reenabled. Unfortunately this is not the case. I've tried the setDecendantFocusability() workaround and I'm still stuck.

I'd like to avoid having to extend ListView if possible to provide the greatest amount of modularity. For similar reasons I don't want to add the selector to the ViewPager's background (if the dev changes the ListView's selector this wouldn't be reflected). Essentially I'm looking to make the ViewPager code transparent between the ListView and child View. Any ideas?

cpx
  • 17,009
  • 20
  • 87
  • 142
keyboardr
  • 841
  • 1
  • 8
  • 20
  • It should be noted I'm not opposed to "tricking" the ListView into thinking the ViewPager isn't clickable. That was essentially what I was trying to do with the hasFocusable() method. – keyboardr Nov 17 '11 at 09:09
  • 1
    The [`TouchListView`](https://github.com/commonsguy/cwac-touchlist) that I extracted from an older edition of the AOSP Music app offers the ability to drag items to the side to remove them. Starting with something like this will be a better option than trying to hack `ViewPager` for this role. – CommonsWare Dec 17 '11 at 23:52

1 Answers1

7

You are saying that you are making each list item a view pager, so that you can implement swiping to delete? If so... no no, this is not what view pager is for. First sorry it is just not intended to be used as an item in a list. Second it is for switching between views, not swiping to delete.

Unfortunately we don't have a sample code to show how to do this, but you can look at the platform's implementation of the notification pane or recent apps to get some ideas.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • I recognize that this wasn't its intended purpose, and for that matter didn't expect it to work as well as it did. With the exception of the selector/OnItemClickListener issue, it's working beautifully. The physics are perfect and I'm not running into any performance issues even with complex layouts on the aging Nexus One (and it's fantastic on the Galaxy Nexus). A large part of that is due to making good use of the view recycler (including within the child view) and using a ViewHolder pattern. (part 1/2) – keyboardr Dec 19 '11 at 03:08
  • (cont'd) That said, this is more of a specific instance of a more general question: how does one force ListView to show/hide the selector? This is especially important in wrapper adapters like this that may have touchable components, but want to pass the focusability of their children up. (part 2/2) – keyboardr Dec 19 '11 at 03:08