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

倖福魔咒の 提交于 2019-12-20 10:15:25

问题


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


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/9637759/is-it-possible-to-extend-abslistview-to-make-new-listview-implementations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!