Use of RecyclerView in Android

Source: Internet
Author: User

Use of RecyclerView in Android

Use of RecyclerView in Android

The release of the RecyclerView on Android is not too short. Some time ago, the company used code optimization. After learning more about it, I found that it is really powerful. The following describes the RecyclerView control.

What is RecyclerView used?

It can be understood as a more efficient ListView and GridView with more powerful functions. The most important thing is that it seems to be the items that have been generated before the Adapter is reused. You can check the memory side to see them clearly.

When using RecyclerView, we need to understand three elements.

1. RecyclerView. Adapter

2. LayoutManager

3. ItemAnimator

 

1. RecyclerView. Adapter

As a pretty cool-looking control, we naturally use a variety of amazing design patterns (which I don't know ). Interface-oriented programming is affirmative, so let's take a look at its Adapter

 

Public class MyAdapter extends RecyclerView. Adapter
 
  
{Public class ViewHolder extends RecyclerView. viewHolder {public VideoListItem txt1; public VideoListItem txt2; public VideoListItem txt3; public VideoListItem txt4; public ViewHolder (View itemView) {super (itemView);} public VideoListItem getTxt1 () {return txt1;} public void setTxt1 (VideoListItem txt1) implements this.txt 1 = txt1;} public VideoListItem getTxt2 () {return txt2;} public void setTxt2 (VideoListItem txt2) export this.txt 2 = txt2;} public VideoListItem getTxt3 () {return txt3;} public void setTxt3 (VideoListItem txt3) Export this.txt 3 = txt3;} public VideoListItem getTxt4 () {return txt4 ;} public void setTxt4 (VideoListItem txt4) Export this.txt 4 = txt4 ;}} private LayoutInflater inflater; public MyAdapter () {inflater = LayoutInflater. from (VideoListActivity. this) ;}@ Overridepublic int getItemCount () {int size = mVideoList. size (); if (size = 0) {return 0;} int count = mVideoList. size ()/COLUMN; if (size % COLUMN = 0) {return count + 1 ;}else {return count + 2 ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic void onBindViewHolder (ViewHolder viewHolder, int position) {System. out. println ("wangzx **" + "onCreateViewHolder"); int firstIndex = position * COLUMN; int size = mVideoList. size (); bindItem (firstIndex + 0, size, position, viewHolder.txt 1); bindItem (firstIndex + 1, size, position, viewHolder.txt 2); bindItem (firstIndex + 2, size, position, viewHolder.txt 3); bindItem (firstIndex + 3, size, position, viewHolder.txt 4);} private void bindItem (int index, int size, int position, VideoListItem item) {VideoInfo historyData0 = null; if (size> index) {item. setVisibility (View. VISIBLE); historyData0 = mVideoList. get (index); item. setCurrentLine (position); item. setIndex (index); item. setView_id (historyData0.getVideo _ id (); item. getmTextView (). setText (historyData0.getVideo _ name (); item. getmRoundedImageView (). setCornerRadiusDimen (R. dimen. video_list_corner_radius); item. getmRoundedImageView (). setImageResource (R. drawable. history_default); imageLoader. displayImage (historyData0.getVideo _ img_url (), item. getmRoundedImageView ();} else {item. setVisibility (View. INVISIBLE) ;}@ Overridepublic MyAdapter. viewHolder onCreateViewHolder (ViewGroup parent, int viewType) {System. out. println ("wangzx **" + "onCreateViewHolder"); View convertView = inflater. inflate (R. layout. video_list_itemlist, parent, false); ViewHolder viewHolder = new lens 1 = (VideoListItem) lens 2 = (VideoListItem) lens 3 = (VideoListItem) lens 4 = (VideoListItem) convertView. findViewById (R. id. item4); View itemBackground_focus1 = viewHolder.txt 1. findViewById (R. id. background_focus); View itemBackground_focus2 = viewHolder.txt 2. findViewById (R. id. background_focus); View itemBackground_focus3 = viewHolder.txt 3. findViewById (R. id. background_focus); View itemBackground_focus4 = viewHolder.txt 4. findViewById (R. id. background_focus); Round (mVideoListItemOnFocus); Round (Focus); Round (mVideoListItemOnFocus); // the shift of focus is not taken into consideration for the moment (Focus acquisition synchronously updates videoCount data) values (mOnClick); values (mWheelKeyListener); values (mWheelKeyListener2); values (mWheelKeyListener2); values (mWheelKeyListener2 ); return viewHolder ;}}
 

 

Figure RecyclerView

 

public class Byhistory__Adapater extends BaseAdapter {private ArrayList
 
   byhistorylist;private LayoutInflater mInflater;public Byhistory__Adapater(Context context,ArrayList
  
    byhistory_list){this.byhistorylist = byhistory_list;this.mInflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return this.byhistorylist.size();}@Overridepublic Object getItem(int arg0) {return null;}@Overridepublic long getItemId(int arg0) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup arg2) {Byhistory_Item holder;if(convertView == null){convertView = mInflater.inflate(R.layout.byhistory_item, null);holder = new Byhistory_Item();holder.setLayout_Image((ImageView) convertView.findViewById(R.id.history_item_image));holder.setLayout_ID((TextView) convertView.findViewById(R.id.history_item_name));//holder.setLayout_price((TextView) convertView.findViewById(R.id.history_item_price));holder.setLayout_count((TextView) convertView.findViewById(R.id.history_item_count));holder.setLayout_business((ImageView) convertView.findViewById(R.id.history_item_more));convertView.setTag(holder);}else{holder = (Byhistory_Item) convertView.getTag();}Byhistory_Item dd = byhistorylist.get(position);//holder.getLayout_Image().setBackgroundResource(Integer.valueOf(dd.getItem_Tile_Image()));holder.getLayout_ID().setText(dd.getItem_Title_ID());//holder.getLayout_price().setText(dd.getItem_price());holder.getLayout_count().setText(dd.getItem_count());//holder.getLayout_business().setBackgroundResource(Integer.valueOf(dd.getItem_business()));return convertView;}}
  
 

Figure ListView

 

We can immediately compare the adapater of listview. In this example, a ViewHolder is created separately. This ViewHolder can be a combination of multiple layout, which is more flexible, making the layout more variable (you can think about it carefully ).

The ListView Adapter usually imports data directly and slides down. However, the Adapter of RecyclerView is not as simple as slide up or down, and both the left and right slides are possible. The key is the credit of LayoutManager. With the diversity of ViewHolder, the function is really powerful.

 

2. LayoutManager

I know that android does have more than one LayoutManager, but I have never touched on it, so I will not post comments for the time being. I may add it later.

LinearLayoutManager

 

Public class MyLinearLayoutManager extends LinearLayoutManager {public MyLinearLayoutManager (Context context) {super (context);} public MyLinearLayoutManager (Context context, int orientation, boolean reverseLayout) {super (context, orientation, reverseLayout) ;}@ Overridepublic boolean requestChildRectangleOnScreen (RecyclerView parent, View child, Rect rect, boolean immediate) {final int parentLeft = getP AddingLeft (); final int parentTop = getPaddingTop (); final int parentRight = getWidth ()-getPaddingRight (); final int parentBottom = getHeight ()-getPaddingBottom (); final int childLeft = child. getLeft () + rect. left; final int childTop = child. getTop () + rect. top; final int childRight = childLeft + rect. right; final int childBottom = childTop + rect. bottom; final int offScreenLeft = Math. min (0, childLef T-parentLeft); final int offScreenTop = Math. min (0, childTop-parentTop); final int offScreenRight = Math. max (0, childRight-parentRight); final int offScreenBottom = Math. max (0, childBottom-parentBottom); // Favor the "start" layout ction over the end when bringing one // side or the other // of a large rect into view. final int dx; if (ViewCompat. getLayoutDirection (parent) = ViewCompat. LAYOUT _ Dirertl _rtl) {dx = offScreenRight! = 0? OffScreenRight: offScreenLeft;} else {dx = offScreenLeft! = 0? OffScreenLeft: offScreenRight;} // Favor bringing the top into view over the bottomint dy = offScreenTop! = 0? OffScreenTop: offScreenBottom; if (dy> 0) {// The offset is 40, and the focus calculation height is 360, which is 40 less than the overall height, the reason is that the focus view is the sub-View of the item, and the height of the item is 400dy + = 40; // dy + = 400;} if (dy <0) {// dy-= 400;} if (dx! = 0 | dy! = 0) {if (immediate) {parent. scrollBy (dx, dy);} else {parent. smoothScrollBy (dx, dy) ;}return true ;}return false ;}@ Overridepublic int scrollHorizontallyBy (int dx, Recycler recycler, State state) {return dx ;}}
Public boolean requestChildRectangleOnScreen (RecyclerView parent,

 

View child, Rect rect, boolean immediate) function is very important, it can basically determine the position between the child Item and the parent when scrolling or sliding in the layout. By carefully checking the source code of the parent class of this function, we can know that the actual significance of dy and dx is the sliding distance between the scroll and the left and right. And this value will definitely seriously affect the smoothness of the slide (I have been debugging these two values ......)

Public int scrollHorizontallyBy (int dx, Recycler recycler, State state) is the callback function in the slide. There must be a lot of people to see, not surprising.

 

3. ItemAnimator

It's a slide animation. It's amazing to hear other people say, but you don't seem to have implemented it yourself. You know there is such a thing.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.