| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
BethanyAcun
8年前发布

一个适用于ListView/GridView/RecyclerView的Adapter库

   <ul>     <li>简化大量重复代码</li>     <li>支持多布局</li>     <li>自定义图片加载</li>     <li>常用数据操作</li>     <li>view复用</li>     <li>RecyclerView item 点击和长按事件</li>    </ul>    <p><a href="/misc/goto?guid=4959671487567792792"><img alt="一个适用于ListView/GridView/RecyclerView的Adapter库" src="https://simg.open-open.com/show/c15a156f1cbad144f1cdc7032ee0d66d.png" width="240" height="426"></a> <a href="/misc/goto?guid=4959671487650475889"><img alt="一个适用于ListView/GridView/RecyclerView的Adapter库" src="https://simg.open-open.com/show/a293d46d01177f37b8942be59fd4adc7.png" width="240" height="426"></a> <a href="/misc/goto?guid=4959671487741552993"><img alt="一个适用于ListView/GridView/RecyclerView的Adapter库" src="https://simg.open-open.com/show/1aa331dd7ffa4b9c0698edd90d56e5e4.png" width="240" height="426"></a></p>    <h2>gradle依赖</h2>    <pre>  <code class="language-java">dependencies {      compile 'com.classic.adapter:commonadapter:1.1'      //项目中使用到RecyclerView,需要添加依赖      compile 'com.android.support:recyclerview-v7:23.2.1'  }</code></pre>    <h2>开始使用</h2>    <p>ListView/GridView 使用示例</p>    <pre>  <code class="language-java">List<News> newsList = ...;  //单布局文件  listView = (ListView) findViewById(R.id.listview);  listView.setAdapter(new CommonAdapter<News>(context,      //item布局文件      R.layout.item_none_picture, newsList ) {      @Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {          //BaseAdapterHelper详细用法,见下方            helper.setText(R.id.xxx, item.getTitle())                 //可实现ImageLoad接口,进行图片自定义加载方式,demo里面使用的Glide                .setImageLoad(new GlideImageLoad())                .setImageUrl(R.id.xxx,item.getCoverUrl());      }  });    //多布局文件  private final class MultipleLayoutAdapter extends CommonAdapter<News>{        public MultipleLayoutAdapter(Context context, int layoutResId, List<News> data) {          super(context, layoutResId, data);      }      //多种布局重写此方法即可      @Override public int getLayoutResId(News item) {          int layoutResId = -1;          switch (item.getNewsType()){              case News.TYPE_NONE_PICTURE: //布局样式一                  layoutResId = R.layout.item_none_picture;                  break;              case News.TYPE_SINGLE_PICTURE: //布局样式二                  layoutResId = R.layout.item_single_picture;                  break;              case News.TYPE_MULTIPLE_PICTURE: //布局样式三                  layoutResId = R.layout.item_multiple_picture;                  break;                更多的布局样式 ...          }          return layoutResId;      }        @Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {          helper.setImageLoad(new GlideImageLoad());          switch (item.getNewsType()){              case News.TYPE_NONE_PICTURE: //布局样式一                  helper.setText(R.id.xxx, item.getTitle())                        .setImageUrl(R.id.xxx,item.getCoverUrl());                  break;              case News.TYPE_SINGLE_PICTURE: //布局样式二                  helper.setText(R.id.xxx, item.getTitle())                        .setImageUrl(R.id.xxx,item.getCoverUrl());                  break;              case News.TYPE_MULTIPLE_PICTURE: //布局样式三                  helper.setText(R.id.xxx, item.getTitle())                        .setImageUrl(R.id.xxx,item.getCoverUrl());                  break;                更多的布局样式 ...          }      }  }</code></pre>    <p>RecyclerView 使用示例</p>    <pre>  <code class="language-java">List<News> newsList = ...;  //单布局文件  recyclerView = (RecyclerView) findViewById(R.id.recyclerview);  recyclerView.setLayoutManager(new LinearLayoutManager(this));  recyclerView.setHasFixedSize(true);  recyclerView.setItemAnimator(new DefaultItemAnimator());  newsAdapter = new NewsAdapter(this, R.layout.item_none_picture, newsList);  recyclerView.setAdapter(newsAdapter);    private class NewsAdapter extends CommonRecyclerAdapter<News>{        public NewsAdapter(Context context, int layoutResId, List<News> data) {          super(context, layoutResId, data);      }        @Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {          helper.setText(R.id.xxx, item.getTitle())                .setImageLoad(new GlideImageLoad())                .setImageUrl(R.id.xxx,item.getCoverUrl());      }  }    //多布局文件  private final class MultipleLayoutAdapter extends CommonRecyclerAdapter<News>{        public MultipleLayoutAdapter(Context context, int layoutResId, List<News> data) {          super(context, layoutResId, data);      }      //多种布局重写此方法即可      @Override public int getLayoutResId(News item) {          int layoutResId = -1;          switch (item.getNewsType()){              case News.TYPE_NONE_PICTURE: //布局样式一                  layoutResId = R.layout.item_none_picture;                  break;              case News.TYPE_SINGLE_PICTURE: //布局样式二                  layoutResId = R.layout.item_single_picture;                  break;              case News.TYPE_MULTIPLE_PICTURE: //布局样式三                  layoutResId = R.layout.item_multiple_picture;                  break;                更多的布局样式 ...          }          return layoutResId;      }        @Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {          helper.setImageLoad(new GlideImageLoad());          switch (item.getNewsType()){              case News.TYPE_NONE_PICTURE: //布局样式一                  helper.setText(R.id.xxx, item.getTitle())                        .setImageUrl(R.id.xxx,item.getCoverUrl());                  break;              case News.TYPE_SINGLE_PICTURE: //布局样式二                  helper.setText(R.id.xxx, item.getTitle())                        .setImageUrl(R.id.xxx,item.getCoverUrl());                  break;              case News.TYPE_MULTIPLE_PICTURE: //布局样式三                  helper.setText(R.id.xxx, item.getTitle())                        .setImageUrl(R.id.xxx,item.getCoverUrl());                  break;                更多的布局样式 ...          }      }  }</code></pre>    <p>RecyclerView item点击事件和长按事件</p>    <pre>  <code class="language-java">commonRecyclerAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {      @Override      public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {          // TODO ...      }  });  commonRecyclerAdapter.setOnItemLongClickListener(new CommonRecyclerAdapter.OnItemLongClickListener() {      @Override public void onItemLongClick(RecyclerView.ViewHolder viewHolder, View view,          int position) {          // TODO ...      }  });</code></pre>    <p>CommonAdapter、CommonRecyclerAdapter区别</p>    <pre>  <code class="language-java">CommonAdapter适用于:ListView/GridView  CommonRecyclerAdapter适用于:RecyclerView  使用方式都一样</code></pre>    <p>BaseAdapterHelper 使用示例</p>    <pre>  <code class="language-java">helper.setText(R.id.viewId, text)        .setTag(R.id.viewId, objectTag)        .setAlpha(R.id.viewId, 0.6f)        .setBackgroundColor(R.id.viewId, R.color.colorResId)        .setBackgroundRes(R.id.viewId, R.drawable.drawableResId)        .setChecked(R.id.viewId, true)        .setImageBitmap(R.id.viewId,bitmap)        .setImageDrawable(R.id.viewId,drawable)        .setImageResource(R.id.viewId, R.drawable.drawableResId)        .setImageUrl(R.id.viewId, url)        .setProgress(R.id.viewId,progress)        .setProgress(R.id.viewId,progress,max)        .setRating(R.id.viewId, rating)        .setRating(R.id.viewId, rating, max)        .setTextColor(R.id.viewId, R.color.colorResId)        .setTextColorRes(R.id.viewId, R.color.colorResId)        //单个TextView设置Typeface        .setTypeface(R.id.viewId, typeface)        //多个TextView设置Typeface        .setTypeface(typeface, R.id.xxx1, R.id.xxx2,R.id.xxx3, ...)        .setVisible(R.id.viewId, View.VISIBLE)        //ProgressBar设置Max        .setMax(R.id.viewId, max)        //ListView设置adapter        .setAdapter(R.id.viewId, adapter)        //下面为View常用点击事件设置        .setOnTouchListener(R.id.viewId, View.OnTouchListener)        .setOnClickListener(R.id.viewId, View.OnClickListener)        .setOnLongClickListener(R.id.viewId, View.OnLongClickListener)        .setOnItemClickListener(R.id.viewId, AdapterView.OnItemClickListener)        .setOnItemLongClickListener(R.id.viewId, AdapterView.OnItemLongClickListener)        .setOnItemSelectedClickListener(R.id.viewId, AdapterView.OnItemSelectedListener);    //获取item的convertView  View convertView = helper.getView();    //如果上面的属性不够用,可以通过getView(viewId)拿到View,然后进行属性设置  View childView = helper.getView(R.id.viewId);  </code></pre>    <p>自定义图片加载</p>    <pre>  <code class="language-java">public class YourXXX implements ImageLoad {        @Override public void load(Context context, ImageView imageView, String imageUrl) {            //使用Glide加载图片          Glide.with(context).load(imageUrl).into(imageView);            or            //使用Picasso加载图片          Picasso.with(context).load(url).into(imageView);            or            fresco          Android-Universal-Image-Loader          其它自定义          ...      }  }</code></pre>    <p>注意事项</p>    <pre>  <code class="language-java">//加载网络图片之前,请调用setImageLoad方法,设置网络图片加载的实现类  helper.setImageLoad(new GlideImageLoad());  helper.setImageUrl(R.id.xxx,url);</code></pre>    <p>常用的数据操作</p>    <pre>  <code class="language-java">//CommonAdapter、CommonRecyclerAdapter都实现了IData接口,里面包含了一些常用的数据操作    void add(T elem);    void addAll(List<T> elem);    void set(T oldElem, T newElem);    void set(int index, T elem);    void remove(T elem);    void remove(int index);    void replaceAll(List<T> elem);    boolean contains(T elem);    void clear();  </code></pre>    <h2>感谢</h2>    <p><a href="/misc/goto?guid=4959644427404037414">base-adapter-helper</a></p>    
 本文由用户 BethanyAcun 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1461507710151.html
ListView Android Android开发 移动开发 RecyclerView gridview