Application of component RecyclerView (1), component recyclerview
First, we know that the RecyclerView component is an upgraded version of ListView. Today we will introduce the basic layout of the basic RecyclerView, RecyclerView. Adapter, and LayoutManager.
First:
LayoutManager
We know that ListView can only scroll in the vertical direction, and Android API does not support scrolling in the horizontal direction of ListView. There may be multiple ways to achieve horizontal sliding. ListView is not designed to do this. However, compared with ListView, RecyclerView provides many more functions on scrolling. It supports the presentation requirements of multiple types of lists, mainly as follows:
LinearLayoutManager supports horizontal and vertical scrolling lists.
StaggeredGridLayoutManager supports cross-grid lists, similar to Waterfall streams or Pinterest.
GridLayoutManager supports grid display and horizontal or vertical scrolling, such as the gallery for displaying images.
Therefore, RecyclerView has a lot of custom extensions compared with ListView.
Second:
The difference between RecyclerView. Adapter and the BaseAdapter of ListView is that BaseAdapter is implemented in the getView method, including View instantiation and component binding. In the RecyclerView. Adapter, The RecyclerView. ViewHolder mechanism is provided. This class must be defined by ourselves. It is mainly used to save the View, and is mainly used to save the View in onCreateViewHolder.
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView itemText;
public ViewHolder(View itemView) {
super(itemView);
itemText = (TextView)findViewById(R.id.item_text);
}
}
@ Override
Public RecyclerView. ViewHolder onCreateViewHolder (ViewGroup parent, int viewType ){
View view = layoutInflater. inflate (R. layout. item_recyclerview, parent, false );
Return new ViewHolder (view );
}
In addition, you also need to implement the getCount () method, that is, the number of items returned. In addition, the onBindViewHolder () method implements the corresponding components and data binding.
@ Override
Public void onBindViewHolder (RecyclerView. ViewHolder holder, int position ){
(ViewHolder) holder). itemText. setText (titleList. get (position ));
}
The following is a small piece of code that I wrote to basically implement the RecyclerView function.
/**
Adapter class
**/
public class RecyclerAdapter extends RecyclerView.Adapter { private List<String> titleList; private LayoutInflater layoutInflater; public RecyclerAdapter(Context context) { this.titleList = new ArrayList<>(); for(int i=0;i<30;i++) { titleList.add("item"+i); } layoutInflater = LayoutInflater.from(context); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = layoutInflater.inflate(R.layout.item_recyclerview,parent,false); return new ViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((ViewHolder)holder).itemText.setText(titleList.get(position)); } @Override public int getItemCount() { return titleList.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { //public TextView itemText; public ViewHolder(View itemView) { super(itemView); } }}
/**
Main Interface code
**/
mRecyclerView = (RecyclerView)pageList.get(SPORT).findViewById(R.id.recyclerview); mRecyclerView.setHasFixedSize(true); mLinearLayoutManager = new LinearLayoutManager(this); mLinearLayoutManager.setOrientation(OrientationHelper.VERTICAL); RecyclerAdapter mRecyclerAdapter = new RecyclerAdapter(this); mRecyclerView.setLayoutManager(mLinearLayoutManager); mRecyclerView.setAdapter(mRecyclerAdapter);
Note: pageList. get (SPORT) is an instantiated view defined by you. You can customize different xml files as needed and instantiate them as views.