android custom AdapterView

    I believe that many Android developers have used ListView and GridView. It feels like setting some properties, writing an adapter, and then it's OK. But have you studied their internal implementation? If they only stay at the level of calling API, they are not good technicians. Brother, I will introduce their internal implementation here, hoping to help some friends.

    ListView, GridView inherit from AbsListView, and AbsListView inherits from AdapterView. Views using adapters in android need to inherit from AdapterView. I will start with AdapterView here, and I will mention AbsListView in future articles.

    The addView and removeView functions cannot be used in AdapterView because the source code of AdapterView is as follows:

    public void addView(View child) {

        throw new UnsupportedOperationException("addView(View) is not supported in AdapterView");

    }

     public void removeView(View child) {

        throw new UnsupportedOperationException("removeView(View) is not supported in AdapterView");

    }

     Adding and removing child views in AdapterView must pass the addViewInLayout, and removeViewInLayout functions.

     If you customize the View, you need to override the onMeasure function; if you customize the ViewGroup, you also need to override the onLayout function. Because AdapterView inherits from ViewGroup, theoretically you need to override the onMeasure and onLayout functions. But you can probably guess from the names of the two functions addViewInLayout and removeViewInLayout, they both need to be called in the onLayout function. And in order to avoid looping the subviews multiple times, the measurement and placement of the subviews in my demo are written in the onLayout function.

    To implement a ListView-like control through AdapterView, there are two functions that must be considered: the deletion of sub-views beyond the scope of the screen, and the caching of sub-views. To delete a child view that exceeds the screen range, you must do the corresponding calculation according to the position and coordinate offset of the child view. If a child view is found to exceed the screen, you should call the removeViewInLayout function to delete it, and cache the deleted child view in a collection. middle. Each time the adapter.getView function is called, get the first element in the cached collection and remove it from the list, avoiding unnecessary memory leaks. Pass this cached element from the list to the second parameter of adapter.getView.

    Brother, it is my first time to write a technical blog. The writing is poor. There may be many places that are not clear. If you have any doubts, you can contact me through my email: [email protected]

    In addition, I put the source code of my Demo, and friends who need it can view the source code. It needs to be said that the function of touch screen inertial scrolling is not added in the source code. Please understand this. After all, time is limited. I will add it when there is time in the future.

     Download address: http://download.csdn.net/detail/liuyunprogramer/9463633


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443095&siteId=291194637