Android-RecyclerView + CardView + Palette
A few days ago, ListView was a little annoying. I heard that a new control was officially released to replace it, so I wrote a Demo along with the other two new versions of 5.0.
First:
(1) RecyclerView
RecyclerView is officially used to replace ListView. In fact, it can also replace the GridView. The Code Implementation of the second figure above only shows a line of code different from the first one.
RecyclerView generally requires two things to be used together. LayoutManager and Adapter are one more LayoutManager than ListView and GridView (later referred to as the LG combination. In fact, the layout function is extracted, and different layoutmanagers can be used to generate different layoutmanagers. That is, the layout task is handed over to LayoutManager, which is more flexible. Currently, the official LinearLayoutManager and GridLayoutManager are provided to implement the layout style of the LG combination respectively. However, horizontal layout is supported, which is better than the LG combination. If there is a new layout idea in the future, you only need to implement the new LayoutManager.
Summary: Like its name, RecyclerView only focuses on recycling and reusing the View. As for the layout of the View, it will find LayoutManager to help it solve the problem. View Details, it will go to the Adapter to help it deal.
Next let's take a look at the general usage of RecyclerView (I changed it to an official example ):
LayoutManager = new LinearLayoutManager (this); // layoutManager = new GridLayoutManager (this, 2); initData (); // recyclerView the initialization data. setLayoutManager (layoutManager); // set the layout manager mAdapter = new MyRecycleAdapter (datas); recyclerView. setAdapter (mAdapter); // set the adapter
There is nothing to talk about setting the layout manager. Now there are only two of them. Let's take a look at the constructor's options and assign them to RecyclerView.
The following describes the general implementation of the Adapter:
public class MyRecycleAdapter extends RecyclerView.Adapter
{private List
list;public MyRecycleAdapter(List
list) {this.list = list;}@Overridepublic int getItemCount() {return list.size();}@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {holder.mImageView.setImageResource(list.get(position).drawableId);holder.mTextView.setText(list.get(position).text);}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);ViewHolder holder = new ViewHolder(view);return holder;}public class ViewHolder extends RecyclerView.ViewHolder {public TextView mTextView;public ImageView mImageView;public ViewHolder(View itemView) {super(itemView);mTextView = (TextView) itemView.findViewById(R.id.txt);mImageView = (ImageView) itemView.findViewById(R.id.img);}}}
Let's take a look at ViewHolder. Here we have a custom ViewHolder, RecyclerView. viewHolder is defined as an abstract class, but it does not have any method to rewrite to implement the function. The reason why it is defined as an abstract class is to emphasize that custom ViewHolder can improve the performance, you should do this, and force you to do this.
So how does custom ViewHolder improve the performance? In fact, like ViewHolder we previously wrote in BaseAdapter, it reduces the number of calls to findViewById.
First, let's take a look at the previous AbsListView cache itemView, And now RecyclerView caches ViewHolder, and itemView becomes a member variable of ViewHolder:
The following describes how to obtain the control reference in the above three methods:
(1) AbsListView
// In getView, ViewHolder holder = (ViewHolder) itemView. getTag (); holder. textView. setText (Hello World !);
(2) RecyclerView. ViewHolder by default
// In onBindViewHolder, View itemView = holder. itemView; TextView textView = (TextView) itemView.findViewById(R.id.txt); textView. setText (Hello World !);
(3) custom ViewHolder
// Also in onBindViewHolder, holder. textView. setText (Hello World !);
We can see that both 1 and 3 use a TextView reference to save the result of the first findViewById. You can use this reference directly next time, instead of using findViewById, this reduces the number of calls to findViewById.
The following describes some key events that occur when RecyclerView is to display the item at position:
(1)RecyclerView: find out whether there is a ViewHolder suitable for the position from the cached ViewHolder.
(2)If no suitable one is found, a new one will be generated through the Adapter (find the suitable one and then discuss it later)
(3)Call the onCreateViewHolder method of the Adapter to generate a new one:
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);ViewHolder holder = new ViewHolder(view);return holder;}
First, an itemView is generated. A new ViewHolder must have an itemView, which is essential for ViewHolder.
Then, a new ViewHolder will be created. Let's take a look at the ViewHolder constructor:
public ViewHolder(View itemView) {super(itemView);mTextView = (TextView) itemView.findViewById(R.id.txt);mImageView = (ImageView) itemView.findViewById(R.id.img);}First, the constructor of the parent class is called. The constructor of the parent class is very simple. It is just to make a null judgment on the itemView and assign a value to the member variable:
public ViewHolder(View itemView) { if (itemView == null) { throw new IllegalArgumentException(itemView may not be null); } this.itemView = itemView; }
Then we save the reference of the control (reduce the call to findViewById, as mentioned above ).
In this way, a new ViewHolder is born. This ViewHolder has itemView and references of those controls in itemView.
(4)Call onBindViewHolder to modify the attributes of some controls in itemView:
public void onBindViewHolder(ViewHolder holder, int position) {holder.mImageView.setImageResource(list.get(position).drawableId);holder.mTextView.setText(list.get(position).text);}
So far, the building process of ViewHolder and itemView is complete, and the general painting process begins.
(5)The parent View RecyclerView measures holder. itemView to obtain the size.
(6)RecyclerView: Submit the layout task to LayoutManager and LayoutManager layout holder. itemView to obtain the location.
(7)The system calls the draw method of holder. itemView to draw the itemView on the screen.
One note:
If a suitable ViewHolder is obtained from the cache, the method will jump directly to step 1, and the onCreateViewHolder method will not be called. Note that the holder has passed through onBindViewHolder at least once. the value of the control attribute in itemView may already have a value. If you do not modify it, the previously set attribute value will be displayed. This is the problem of display disorder caused by the reuse of views. This problem can be avoided by following the principle below:
To display different states for items with different positions, a List-like container should save the status, in addition, the status information is retrieved from the container in the onBindViewHolder method to reset the control.
(2) CardView
CardView has a simple function, that is, adding a rounded corner for the layout with a shadow effect.
It can be used directly in xml. below is the layout file of the item above:
CardView has the following custom attributes that can be set:
The important and common attributes are:
CardBackgroundColor: Set the background color of the card.
CardCornerRadius: sets the degree of rounded corner.
CardElevation: sets the shadow height. The larger the value, the more obvious the shadow.
(3) Palette
Translating Palette into Chinese is a "Palette ". The official explanation of this function is that it can obtain a highlighted color from a Bitmap. It can be set to controls such as ActionBar to unify the tone of the entire interface.
Palette can only extract color from Bitmap, but generally we operate on the View, so we need to convert the View to Bitmap first. The following describes the method:
(1)
view.buildDrawingCache(autoScale);Bitmap bitmap = view.getDrawingCache(autoScale);// doSomething by bitmapview.destroyDrawingCache();
(2)
Bitmap bitmap=Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);Canvas canvas=new Canvas(bitmap);view.draw(canvas);//doSomething by bitmap
In fact, the second method is the internal implementation of the first method, which can be used by yourself.
After obtaining bitmap, you can start using it:
Palette palette = Palette.generate(bitmap);titleLayout.getChildAt(0).setBackgroundColor(palette.getDarkMutedColor(0XFFFFFFFF));titleLayout.getChildAt(1).setBackgroundColor(palette.getDarkVibrantColor(0XFFFFFFFF));titleLayout.getChildAt(2).setBackgroundColor(palette.getLightMutedColor(0XFFFFFFFF));titleLayout.getChildAt(3).setBackgroundColor(palette.getLightVibrantColor(0XFFFFFFFF));titleLayout.getChildAt(4).setBackgroundColor(palette.getMutedColor(0XFFFFFFFF));titleLayout.getChildAt(5).setBackgroundColor(palette.getVibrantColor(0XFFFFFFFF));
Call the generate method to extract the color from the bitmap and save the result to the palette. You can use the above six get methods to obtain different types of colors. If the bitmap does not have a color that matches this type, the default color is returned.
Meaning:
Dark: dim
Light: Brilliant
Muted: soft
Vibrant: dynamic, bright
The six color blocks at the top of a RecyclerView are the six colors extracted from the current display of the RecyclerView. The color changes when the color is dragged.
Note: generate is the synchronous method, and Palette also provides the Asynchronous Method generateAsync. We recommend that you use the asynchronous method. I am lazy here.