Android中一个很赞的RecyclerView Adapter辅助类

Tor6721 8年前
   <h2>是什么</h2>    <p>BaseRecyclerViewAdapterHelper是一个强大并且灵活的RecyclerViewAdapter。</p>    <h2>它能做什么</h2>    <ul>     <li>优化Adapter代码</li>     <li>添加点击事件</li>     <li>添加列表加载动画</li>     <li>添加添加头部、添加尾部</li>     <li>添加下拉刷新、上拉加载</li>     <li>添加分组</li>     <li>自定义不同item类型</li>     <li>设置setEmptyView</li>     <li>添加子布局控件的点击事件</li>    </ul>    <h2>效果图</h2>    <p><img src="https://simg.open-open.com/show/07068209c71aea43459f2889e9804e01.gif"></p>    <h2>如何使用它?</h2>    <p>先在 build.gradle 的 repositories 添加:</p>    <pre>  <code class="language-java">    allprojects {          repositories {              ...              maven { url "https://jitpack.io" }          }      }  </code></pre>    <p>然后在dependencies添加:</p>    <pre>  <code class="language-java">    dependencies {              compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.6.3'      }  </code></pre>    <h2>如何使用它来创建Adapter?</h2>    <pre>  <code class="language-java">public class QuickAdapter extends BaseQuickAdapter<Status> {      public QuickAdapter(Context context) {          super(context, R.layout.tweet, DataServer.getSampleData());      }        @Override      protected void convert(BaseViewHolder helper, Status item) {          helper.setText(R.id.tweetName, item.getUserName())                  .setText(R.id.tweetText, item.getText())                  .setText(R.id.tweetDate, item.getCreatedAt())                  .setImageUrl(R.id.tweetAvatar, item.getUserAvatar())                  .setVisible(R.id.tweetRT, item.isRetweet())                  .linkify(R.id.tweetText);      }  }  </code></pre>    <p>这么复杂的布局只需要15行代码即可!</p>    <h2>如何添加item点击事件</h2>    <pre>  <code class="language-java">mQuickAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {              @Override              public void onItemClick(View view, int position) {                  //..              }          });  </code></pre>    <h2>如何使用它添加动画?</h2>    <pre>  <code class="language-java">// 一行代码搞定(默认为渐显效果)  quickAdapter.openLoadAnimation();  </code></pre>    <p>不喜欢渐显动画可以这样更换</p>    <pre>  <code class="language-java">// 默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)  quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);  </code></pre>    <p>还是没你喜欢的,你可以自定义</p>    <pre>  <code class="language-java">// 自定义动画如此轻松  quickAdapter.openLoadAnimation(new BaseAnimation() {                              @Override                              public Animator[] getAnimators(View view) {                                  return new Animator[]{                                          ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),                                          ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)                                  };                              }                          });  </code></pre>    <h2>如何添加多种类型item?</h2>    <pre>  <code class="language-java">public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> {        public MultipleItemQuickAdapter(Context context, List data) {          super(context, data);          addItmeType(MultipleItem.TEXT, R.layout.text_view);          addItmeType(MultipleItem.IMG, R.layout.image_view);      }        @Override      protected void convert(BaseViewHolder helper, MultipleItem item) {          switch (helper.getItemViewType()) {              case MultipleItem.TEXT:                  helper.setImageUrl(R.id.tv, item.getContent());                  break;              case MultipleItem.IMG:                  helper.setImageUrl(R.id.iv, item.getContent());                  break;          }      }    }  </code></pre>    <h2>使用它添加头部添加尾部</h2>    <pre>  <code class="language-java">mQuickAdapter.addHeaderView(getView());  mQuickAdapter.addFooterView(getView());  </code></pre>    <h2>使用它加载更多</h2>    <pre>  <code class="language-java">mQuickAdapter.setOnLoadMoreListener(PAGE_SIZE, new BaseQuickAdapter.RequestLoadMoreListener() {              @Override              public void onLoadMoreRequested() {                  if (mCurrentCounter >= TOTAL_COUNTER) {                      mRecyclerView.post(new Runnable() {                          @Override                          public void run() {                              mQuickAdapter.isNextLoad(false);                          }                      });                  } else {                      // reqData                      mCurrentCounter = mQuickAdapter.getItemCount();                      mQuickAdapter.isNextLoad(true);                  }              }          });  </code></pre>    <h2>使用分组</h2>    <pre>  <code class="language-java">public class SectionAdapter extends BaseSectionQuickAdapter<MySection> {       public SectionAdapter(Context context, int layoutResId, int sectionHeadResId, List data) {          super(context, layoutResId, sectionHeadResId, data);      }      @Override      protected void convert(BaseViewHolder helper, MySection item) {          helper.setImageUrl(R.id.iv, (String) item.t);      }      @Override      protected void convertHead(BaseViewHolder helper,final MySection item) {          helper.setText(R.id.header, item.header);          if(!item.isMroe)helper.setVisible(R.id.more,false);          else          helper.setOnClickListener(R.id.more, new View.OnClickListener() {              @Override              public void onClick(View v) {                  Toast.makeText(context,item.header+"more..",Toast.LENGTH_LONG).show();              }          });      }  </code></pre>    <h2>使用setEmptyView</h2>    <pre>  <code class="language-java">mQuickAdapter.setEmptyView(getView());  </code></pre>    <h2>新增添加子布局多个控件的点击事件</h2>    <p>Adapter</p>    <pre>  <code class="language-java"> protected void convert(BaseViewHolder helper, Status item) {      helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener())        .setOnClickListener(R.id.tweetName, new OnItemChildClickListener());        }  </code></pre>    <p>Activity</p>    <pre>  <code class="language-java">mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {              @Override              public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {                  String content = null;                  Status status = (Status) adapter.getItem(position);                  switch (view.getId()) {                      case R.id.tweetAvatar:                          content = "img:" + status.getUserAvatar();                          break;                      case R.id.tweetName:                          content = "name:" + status.getUserName();                          break;                  }                  Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).show();              }          });</code></pre>    <p>Adapter</p>    <pre>  <code class="language-java">protected void convert(BaseViewHolder helper, Status item) {  helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener())  .setOnClickListener(R.id.tweetName, new OnItemChildClickListener()); } </code></pre>    <p>Activity</p>    <pre>  <code class="language-java">mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {      @Override      public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {          String content = null;          Status status = (Status) adapter.getItem(position);          switch (view.getId()) {              case R.id.tweetAvatar:                  content = "img:" + status.getUserAvatar();                  break;              case R.id.tweetName:                  content = "name:" + status.getUserName();                  break;          }          Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).show();      }  });  </code></pre>    <h2> </h2>