ListView related
What is a ListView? (Ancient artifacts) No, what is RecyclerView? (Also ancient artifact? Better not to call it that until its replacement comes out.) In the meantime, I thought I’d clean up this mess, too.
OK, no, ListView and RecyclerView are used to display list data. Compared to ListView, RecyclerView is much more powerful.
The ListView adapter
A quick look at the ListView adapter code:
public class ListViewAdapter extends BaseAdapter {
// Used to load item
private LayoutInflater mInflater;
// Pass the Context in via the constructor
public ListViewAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
The data obtained by /** * is stored in this collection, which generally stores the item */ of its children
private List mList = new ArrayList();
/ * * *@returnReturns the size of the collection */
@Override
public int getCount(a) {
return mList.size();
}
/ * * *@param position
* @returnReturns a specific item */
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/** * loads the layout of the child item **@paramPosition Position of the item *@paramConvertView item is reused to avoid multiple loads of the same item *@paramParent Parent container, depending on the situation@return item
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = mInflater.inflate(R.layout.activity_detail,parent,false);
}
// Reuse item, return convertView
returnconvertView; }}Copy the code
The optimization of ListView
As you can see, almost every item needs to be loaded in getView(), and too many items can cause performance problems and affect the user experience. This is where we need to use the ViewHolder (developer custom class), which is the inner class of the ListViewAdapter. The code is as follows:
/** * loads the layout of the child item **@paramPosition Position of the item *@paramConvertView item is reused to avoid multiple loads of the same item *@paramParent Parent container, depending on the situation@return item
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.activity_detail,parent,false);
convertView.setTag(holder);
} else{if convertView is not null, reuse holder = (ViewHolder) convertView.getTag(); }// Reuse item, return convertView
return convertView;
}
// This is an inner class
private class ViewHolder{
// The controls that get item are defined here
}
Copy the code
Well, that’s it for ListView, and two key points to summarize:
- Custom ListViewAdapter, inheriting BaseAdapter.
- Custom ViewHolder and convertView are used together for reuse optimization.
RecyclerView related
The following began to write RecyclerView, the key points of RecyclerView summarized here. ListView and ListView are similar, there are two main:
- RecyclerViewAdapter inherits recyclerView. Adapter, its inner class ViewHolder inherits recyclerView. ViewHolder.
- Set the layout manager to control the layout effect
RecyclerView adapter
RecyclerView adapter code
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
/** * This method produces a View per Item inflater, but returns a ViewHolder. This method encapsulates the View directly into a ViewHolder, which is then an instance of the ViewHolder that we need to write ourselves. * *@param parent
* @param viewType
* @return viewHolder
*/
@NonNull
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
/** * This method is mainly used to adapt render data to the View. The * method provides you with a viewHolder instead of a convertView@param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
// Update the UI here
}
/ * * *@returnReturns the number of items */
@Override
public int getItemCount(a) {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView); }}}Copy the code
From the basis of use, we can obviously see that the differences between RecyclerView and ListView in the basis of use are mainly as follows:
- The writing of the ViewHolder is normalized.
- You don’t need to use setTag() as ListView does.
- RecyclerView needs to set KayoutManager
How to use RecyclerView in your Activity
Now look at how to use RecyclerView in your Activity :(ListView is roughly the same)
// set the layout manager
LinearLayoutManager manager = new LinearLayoutManager(this);
RecyclerView recyclerView = findViewById(R.id.detail_rv);
recyclerView.setLayoutManager(manager);
//2. Set the adapter
RecyclerViewAdapter adapter = new RecyclerViewAdapter();
recyclerView.setAdapter(adapter);
Copy the code
RecyclerView layout manager
RecyclerView also needs to talk about its layout manager. RecyclerView provides three layout managers to choose from:
- LinerLayoutManager is presented as a vertical or horizontal list
- The GridLayoutManager presents items as a grid
- StaggeredGridLayoutManager waterfalls flow way to show the Item
RecyclerView is more powerful because RecyclerView provides a variety of layout managers that can be set up in your Activity. In addition, some API for layout manager are as follows:
canScrollHorizontally();// Can scroll horizontally
canScrollVertically();// Can scroll vertically
scrollToPosition(int position);// Scroll to the specified position
setOrientation(int orientation);// Set the scrolling direction
getOrientation();// Get the scroll direction
findViewByPosition(int position);// Get the Item View at the specified location
findFirstCompletelyVisibleItemPosition();// Get the first fully visible Item position
findFirstVisibleItemPosition();// Get the position of the first visible Item
findLastCompletelyVisibleItemPosition();// Get the last fully visible Item position
findLastVisibleItemPosition();// Get the position of the last visible Item
Copy the code
RecyclerView sectionals
We can draw a dividing line for REcyclerview items, using the dividing line defined by the system:
/ / add divider myRecyclerView. AddItemDecoration (new DividerItemDecoration (this, DividerItemDecoration. VERTICAL));Copy the code
Similarly, we can define our own dividing line to achieve different effects:
Custom interval style. We need to inherit RecyclerView ItemDecoration class, the class is an abstract class, the official did not provide a default implementation classes, there are three main methods.
- OnDraw (Canvas C, RecyclerView parent, State State), called before Item is drawn. This method is used to draw interval styles.
- OnDrawOver (Canvas C, RecyclerView parent, State State), called before the Item is drawn. This method is used to draw interval styles.
- GetItemOffsets (Rect outRect, View View, RecyclerView parent, State State), set the offset of the item, the offset part is used to fill the interval style, that is, set the width and height of the dividing line; This method is called in RecyclerView’s onMesure()