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

Zhai Shanying's "Financial Class CEO Class" 43 episodes video

Zhai Shanying's "Financial Class for CEO...

2019 Mayu APP product analysis report!

1. Product structure analysis Product Structure D...

Five strategic keywords outline the 2015 blueprint of Youpengpule

If 2014 was a dormant year for the Internet TV in...

Careful analysis of mmap: what it is, why it is used, and how to use it

Mmap basic concept Mmap is a method of memory map...

How to attract more WeChat friends in private domains

Whether you open a Taobao store, do Douyin, write...

How can an APP application product retain users?

More and more companies can develop their own app...

A universal formula for user growth

I have always believed that no matter what you do...

Cooling spray has a bad temper and may be flammable and explosive!

Even though the beginning of autumn has passed, t...

Marketing campaign self-checklist!

Events have become one of the most effective ways...

B station semi-private traffic pool marketing

In the eyes of the public, Bilibili is a video pl...