Super shorthand for RecyclerView adapter

Super shorthand for RecyclerView adapter

[[144456]]

RecycleView is a new control. It standardizes the writing of Viewholder.

But I always feel that the writing of the adapter is too lengthy. What should I do?

Let's simplify it.

The implementation of ViewHolder is the same as ViewHolder in the super-saving writing of ListView adapter

ViewHolder.class

  1. public   class ViewHolder {
  2. private SparseArray<View> viewHolder;
  3. private View view;
  4.  
  5. public   static ViewHolder getViewHolder(View view){
  6. ViewHolder viewHolder = (ViewHolder) view.getTag();
  7. if (viewHolder == null ) {
  8. viewHolder = new ViewHolder(view);
  9. view.setTag(viewHolder);
  10. }
  11. return viewHolder;
  12. }
  13. private ViewHolder(View view) {
  14. this .view = view;
  15. viewHolder = new SparseArray<View>();
  16. view.setTag(viewHolder);
  17. }
  18. public <T extends View> T get( int id) {
  19. View childView = viewHolder.get(id);
  20. if (childView == null ) {
  21. childView = view.findViewById(id);
  22. viewHolder.put(id, childView);
  23. }
  24. return (T) childView;
  25. }
  26.  
  27. public View getConvertView() {
  28. return view;
  29. }
  30.  
  31. public TextView getTextView( int id) {
  32.  
  33. return get(id);
  34. }
  35. public Button getButton( int id) {
  36.  
  37. return get(id);
  38. }
  39.  
  40. public ImageView getImageView( int id) {
  41. return get(id);
  42. }
  43.  
  44. public   void setTextView( int id,CharSequence charSequence){
  45. getTextView(id).setText(charSequence);
  46. }
  47.  
  48. }

We inherit RecyclerView.Adapter<RVHolder>

AutoRVAdapter.class

  1. public   abstract   class AutoRVAdapter extends RecyclerView.Adapter<RVHolder> {
  2.  
  3.  
  4. public List<?> list;
  5.  
  6. private Context context;
  7.  
  8. public AutoRVAdapter(Context context, List<?> list) {
  9. this .list = list;
  10. this .context = context;
  11. }
  12.  
  13. @Override  
  14. public RVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  15. View view = LayoutInflater.from(context).inflate(onCreateViewLayoutID(viewType), null );
  16.  
  17. return   new RVHolder(view);
  18. }
  19.  
  20. public   abstract   int onCreateViewLayoutID( int viewType);
  21.  
  22.  
  23. @Override  
  24. public   void onViewRecycled( final RVHolder holder) {
  25. super .onViewRecycled(holder);
  26. }
  27.  
  28. @Override  
  29. public   void onBindViewHolder( final RVHolder holder, final   int position) {
  30.  
  31. onBindViewHolder(holder.getViewHolder(), position);
  32. if (onItemClickListener != null ) {
  33. holder.itemView.setOnClickListener( new View.OnClickListener() {
  34. @Override  
  35. public   void onClick(View v) {
  36. onItemClickListener.onItemClick( null , v, holder.getPosition(), holder.getItemId());
  37. }
  38. });
  39. }
  40.  
  41. }
  42.  
  43. public   abstract   void onBindViewHolder(ViewHolder holder, int position);
  44.  
  45. @Override  
  46. public   int getItemCount() {
  47. return list.size();
  48. }
  49.  
  50. private AdapterView.OnItemClickListener onItemClickListener;
  51.  
  52. public AdapterView.OnItemClickListener getOnItemClickListener() {
  53. return onItemClickListener;
  54. }
  55.  
  56. public   void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
  57. this .onItemClickListener = onItemClickListener;
  58. }
  59. }

RVHolder.class inherits RecyclerView.ViewHolder

  1. public   class RVHolder extends RecyclerView.ViewHolder {
  2.  
  3.  
  4. private ViewHolder viewHolder;
  5.  
  6. public RVHolder(View itemView) {
  7. super (itemView);
  8. viewHolder=ViewHolder.getViewHolder(itemView);
  9. }
  10.  
  11.  
  12. public ViewHolder getViewHolder() {
  13. return viewHolder;
  14. }
  15.  
  16. }

That's it, it's over

Our newly written adapter inherits AutoRVAdapter and implements onCreateViewLayoutID and onBindViewHolder methods.

onCreateViewLayoutID->Returns the layout of the item.

onBindViewHolder->Bind data source.

  1. public   class DemoRVAdapter extends AutoRVAdapter {
  2. public RecyclerAdapter(Context context, List<?> list) {
  3. super (context, list);
  4. }
  5.  
  6. @Override  
  7. public   int onCreateViewLayoutID( int viewType) {
  8. return R.layout.item;
  9. }
  10.  
  11. @Override  
  12. public   void onBindViewHolder(ViewHolder holder, int position) {
  13.  
  14. Entity item=(Entity) list.get(position);
  15. vh.getTextView(R.id.name).setText(item.getName());
  16. vh.getTextView(R.id.age).setText(item.getAge());
  17. vh.setText(R.id.height,item.getHeight());
  18. }
  19. }

<<:  Windows open-sources iOS to Windows porting technology

>>:  Ten days after the Android vulnerability broke out, it changed Google and Samsung

Recommend

#千万IP创科普# AI face-changing: I decide how he looks

Sichuan opera is a treasure of traditional Chines...

What exactly is "Rakshasa Sea City" about?

Although "Rakshasa Sea City" is bizarre...

5 major trends in brand marketing in 2021

Among the top ten popular slogans, Yuanfudao (A t...

Don't know what to do with your kids? Why not look for spring bugs?

Audit expert: Li Weiyang Well-known science write...

E-commerce operations: How to price products?

Let’s start by answering the first question: Why ...

Wuhan Red Square Recommendation

Wuhan high-end tea drinking is unique and very un...

Short video operation matrix gameplay!

Account matrix has always been the most common op...

Ad creative writing secrets, take them!

In the process of bidding promotion , I believe t...

Why doesn’t China have a super IP yet?

01 What are we talking about when we talk about IP...

Bidding Promotion | How to reduce costs and increase clicks?

The competition environment is intensifying. Alth...

5 tricks and classic cases of leveraging marketing during National Day!

The National Day is coming, and operators are alr...