作家
登录

Android 自定义 BaseAdapter 最佳实践

作者: 来源: 2017-05-19 13:06:51 阅读 我要评论

from(getApplicationContext()).inflate(R.layout.item_text, parent, false); 
  •             holder = new MyTextHolder(view); 
  •             break; 
  •         case TYPE_IMAGE: 
  •             view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_image, parent, false); 
  •             holder = new MyImageHolder(view); 
  •             break; 
  •         } 
  •         return holder; 
  •     } 
  •  
  • private class MyTextHolder extends RecyclerView.ViewHolder { 
  •     private TextView textView; 
  •  
  •     public MyTextHolder(View itemView) { 
  •         super(itemView); 
  •         textView = (TextView) itemView.findViewById(R.id.textView); 
  •     } 
  •  
  • private class MyImageHolder extends ViewHolder { 
  •     private ImageView imageView; 
  •  
  •     public MyImageHolder(View itemView) { 
  •         super(itemView); 
  •         imageView = (ImageView) itemView.findViewById(R.id.imageView); 
  •     } 
  • RecyclerView中Adapter的应用

    自定义BaseAdapter

    在自定义基类之前,先简单分析一下,我们须要自定义一个支撑单种视图的Adapter,还要自定义一个支撑多种视图类型的Adapter,两个类都要持续BaseAdapter,先将两个类都公用的部分采掏出来定义为MyAdapter。

    1. public abstract class MyAdapter<T> extends BaseAdapter { 
    2.  
    3.     protected List<T> dataList = new ArrayList<>(); 
    4.     protected Context context; 
    5.     protected LayoutInflater inflater; 
    6.  
    7.     public MyAdapter(Context context) { 
    8.         this.context = context; 
    9.         inflater = LayoutInflater.from(context); 
    10.     } 
    11.  
    12.     public void setDataList(List<T> dataList) { 
    13.         this.dataList = dataList; 

        推荐阅读

        【海外风向标】阿斯利康公司CIO:想数字化转型成功?先给高层们“讲故事”吧

      Dave Smoley比来停止了一个为期三年的数字化改革过程,固然大年夜多半CIO们可能会在此时打开一瓶喷鼻槟庆贺,但这位阿斯利康集团的IT引导人已经把计谋重点缀向了成长和立异。Smoley给所有>>>详细阅读


      本文标题:Android 自定义 BaseAdapter 最佳实践

      地址:http://www.17bianji.com/lsqh/35318.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)