Method 2: RecyclerView

Source: Internet
Author: User

Method 2: RecyclerView

In the previous article (RecyclerView usage (I), I briefly introduced the basic usage of RecyclerView. Next I will introduce more methods of RecyclerView to achieve different functional effects, most of them are still in the Adapter Writing Method of RecyclerView, so let's take a look at several different functions of the Adapter writing method.

1. MultipleItem layout implementation)

If you have used ListView to implement this function before, you must be familiar with the following two methods:

@Overridepublic int getItemViewType(int position) {return super.getItemViewType(position);}@Overridepublic int getViewTypeCount() {return super.getViewTypeCount();}

The getItemViewType method is used to obtain the layout of the current Item (position parameter), and The getViewTypeCount method is used to obtain the total layout of the current listview.

If you use RecyclerView, you will find that the getViewTypeCount method does not exist. There is only one getItemViewType method, and its usage is no different from that of listview. Note that this function is onCreateViewHolder (ViewGroup parent, int viewType) The second parameter here is the View type. You can create ViewHolder for different items based on this type.

public class MultipleItemAdapter extends RecyclerView.Adapter
 
   {public static enum ITEM_TYPE {ITEM_TYPE_IMAGE,ITEM_TYPE_TEXT}private final LayoutInflater mLayoutInflater;private final Context mContext;private String[] mTitles;public MultipleItemAdapter(Context context) {mTitles = context.getResources().getStringArray(R.array.titles);mContext = context;mLayoutInflater = LayoutInflater.from(context);}@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {if (viewType == ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal()) {return new ImageViewHolder(mLayoutInflater.inflate(R.layout.item_image, parent, false));} else {return new TextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));}}@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {if (holder instanceof TextViewHolder) {((TextViewHolder) holder).mTextView.setText(mTitles[position]);} else if (holder instanceof ImageViewHolder) {((ImageViewHolder) holder).mTextView.setText(mTitles[position]);}}@Overridepublic int getItemViewType(int position) {return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal() : ITEM_TYPE.ITEM_TYPE_TEXT.ordinal();}@Overridepublic int getItemCount() {return mTitles == null ? 0 : mTitles.length;}public static class TextViewHolder extends RecyclerView.ViewHolder {@InjectView(R.id.text_view)TextView mTextView;TextViewHolder(View view) {super(view);ButterKnife.inject(this, view);view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.d("TextViewHolder", "onClick--> position = " + getPosition());}});}}public static class ImageViewHolder extends RecyclerView.ViewHolder {@InjectView(R.id.text_view)TextView mTextView;@InjectView(R.id.image_view)ImageView mImageView;ImageViewHolder(View view) {super(view);ButterKnife.inject(this, view);view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.d("ImageViewHolder", "onClick--> position = " + getPosition());}});}}}
 

How about it? Is it very simple? Yes, it is so simple, but as long as there are more items, you can make a lot of results, for example, HeaderView and BottomView can also be implemented in this way. (HeaderView Adapter)

Ii. as follows:

 

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.