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

Konka HD player HAN330 review: Genuine content brings excellent user experience

Smart TVs and TV box products that provide online...

Why are fireworks colorful? Fireworks and firecrackers are actually different

Expert of this article: Chu Yuhao, PhD of Beijing...

Android uses JobScheduler to perform background tasks

JobScheduler Introduction JobScheduler is used in...

Samsung Empire: The Crisis of the Authoritarian Model

Samsung accounts for one-fifth of South Korea'...

Dr. Mo reviews iPhone 6S: The best smartphone ever

Apple has just released its new smartphone, iPhon...

The trend of Douyin e-commerce bringing goods to monetize!

The short video industry will reach 1 billion dai...

Is it a loss if you don’t buy anything on 618?

Part 1 618 has arrived Dear, has the hand that wa...

Girls' live game show, you deserve it

At present, domestic online live broadcast platfo...

The nails are the "barometer" of health, check yourself now!

Do you usually pay attention to your nails? Every...

A rough man enjoys exercising and losing weight

A brief introduction to Yijieren Yuelian Yueshou ...

Advertising tips for education, tourism and gaming industries!

If June is a carnival for e-commerce advertisers,...