Powerful scrolling control RecyclerView

RecyclerView basic usage:

RecyclerView belong to the new controls, in order to make RecyclerView can all Android can use version, we need to RecyclerView defined in the support libraries inside.

Was added in the layout RecyclerView step control is very simple: we first need to define an ID , then the width and height are set to match-parent, this could make RecyclerView controls occupy the entire space layout. However, because RecyclerView is not built into the system SDk in, so we need to write out the full package path.

Next we need to RecyclerView prepare an adapter, a new category, and let this class inherits RecyclerView.Adapter , then the generic designated as "New class name .ViewHolder form". Which ViewHolder is an internal class that we defined in the new class inside. This inner class ViewHolder to inherit RecyclerView.Holder . Then we want ViewHolder passed a constructor parameter View , and this parameter is usually RecyclerView child outermost layout, then we can findViewById () f acquired layout method ImageView and TextView instance a.

We set up a new class constructor is mainly used to show the source of the data passed in and assigned to a global variable. Because the class has inherited RecyclerView.Adapter , we need to override onCreateViewHolder () , onBindViewHolder () , getItemCount () method. onCreateHolder () method is used to create ViewHolder instance in which this method, we will RecyclerView child layout loaded in, and then create another ViewHolder instance, to load out the layout of passed to the constructor, and returns ViewHolder instance . onBindViewHolder () method is used to RecyclerView data sub-items of the assignment, it will be scrolled on each child to the screen when executed by our position to obtain examples of the current item parameters, and then set the data to ViewHolder of ImageView and TextView them. getItemCount () method is used to tellRecyclerView a plenty of how many items, the data source returns directly to the line length.

After the adapter is ready, we need to modify the code inside the main activities. First, we need a initFruits () method to initialize all data. Next, in onCreat () Gets inside method RecyclerView instance, then create a LinearLayoutManager object and adds it to RecyclerView them, finally call RecyclerView is of the setAdapter () method to complete the adapter set, to achieve Recycler associated with the data.

Published 37 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/OneLinee/article/details/77841815