Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extend AbsListView to make new ListView implementations?

I've been looking at creating a stylistically different list view, like many other people, and I started by looking to extend AbsListView. Per the Android doc, AbsListView is:

Base class that can be used to implement virtualized lists of items. A list does not have a spatial definition here. For instance, subclases of this class can display the content of the list in a grid, in a carousel, as stack, etc.

After some effort and review of the ListView implementation, it looks like it may not be possible to extend AbsListView the way ListView does because of the coupling of the two classes and the inability to access certain package members. I stumbled on this form Romain Guy:

AbsListView and AbsSpinner are designed to be extended within the framework. They could also be extended in 3rd party apps but we did not expose all the necessary protected fields and methods on purpose. We want to be very careful in how we expose such APIs so as to not get stuff for future extensions and internal changes. Our current recommendation is you simply copy/paste the code you need inside your app.

I started trying to do this but copying AbsListView becomes a rabbit hole of copying in a bunch of other stuff and it really just seems like a losing proposition. I wanted to see if anyone has had success extending AbsListView who could share a methodology.

[1] http://developer.android.com/reference/android/widget/AbsListView.html

[2] https://groups.google.com/forum/?fromgroups#!topic/android-developers/UhbR1tpVvF0

like image 608
Nick Campion Avatar asked Mar 09 '12 16:03

Nick Campion


2 Answers

The post is circa 2010, but his assessment still holds true today. Certain necessary methods & fields are protected, in order to prevent future incompatibilities that could result from changing the internals of AbsListView.

The best solution is still to copy the code for AbsListView into your project, along with necessary dependencies (the rabbit hole you mentioned).

In the meantime, if you want to describe what kind of View you're trying to build, we might be able to point you in a direction that's slightly easier than creating your own AbsListView?

like image 73
Alexander Lucas Avatar answered Sep 23 '22 10:09

Alexander Lucas


I've had the same experience trying to subclass ListView and AbsListView to develop a 2D scrollable spreadsheet. I ended up with my own subclass of ViewGroup which handles layout and a subclass of TwoDScrollView to handle the scrolling. The hardest parts are getting flings to work correctly (ie. create and place new views before they scroll into view) and synchronizing view positions between scrollable components. With enough time though, it is possible to create a robust component that pulls data from a Cursor and scrolls it around quite smoothly and quickly.

like image 35
Scott Leslie Avatar answered Sep 23 '22 10:09

Scott Leslie