Android开源 - 简单易用的 RecyclerView.Adapter 封装库

剑皇萧剑 5年前
   <h2>RecyclerAdapter</h2>    <p>在想着尽可能保留原味,又想简化代码工作量的情况下封装了RecyclerView.Adapter。</p>    <h2>Installation</h2>    <p>add the dependency to your build.gradle:</p>    <pre>  <code class="language-java">compile 'com.github.nukc:recycleradapter:0.1'</code></pre>    <h2>Usage</h2>    <p>继承 RecyclerHolder 。 (class RecyclerHolder extends RecyclerView.ViewHolder )</p>    <p>在原有的 ViewHolder 上增加了一个抽象方法 onBindView(T t)。</p>    <pre>  <code class="language-java">//NumberItem 是 sample 中的 Model      static class ItemHolder extends RecyclerHolder<NumberItem> {            private TextView mTextView;          private OnItemListener mItemListener;            public ItemHolder(View itemView, OnItemListener listener) {              super(itemView);              mTextView = (TextView) itemView.findViewById(R.id.text);              mItemListener = listener;                itemView.setOnClickListener(new View.OnClickListener() {                  @Override                  public void onClick(View view) {                      if (mItemListener != null) {                          mItemListener.onItemClick(view, getAdapterPosition());                      }                  }              });          }            @Override          public void onBindView(NumberItem item) {              mTextView.setText("+" + item.getNumber());          }      }</code></pre>    <p>监听点击事件</p>    <pre>  <code class="language-java">interface OnItemListener {          void onItemClick(View view, int position);      }        private OnItemListener mItemListener = new OnItemListener() {          @Override          public void onItemClick(View view, int position) {              NumberItem numberItem = mNumberList.get(position);              Toast.makeText(MainActivity.this, "number=" + numberItem.getNumber(), Toast.LENGTH_SHORT).show();          }      };</code></pre>    <p>设置 Adapter</p>    <pre>  <code class="language-java">recyclerView.setAdapter(new RecyclerAdapter<NumberItem>(mItemListener) {          @Override          public ItemWrapper getItemHolder(int position) {              // 可获取到对应的 model ,可根据实际需求返回不一样的 Item              // NumberItem numberItem = getItem(position);              return new ItemWrapper(R.layout.item, ItemHolder.class);          }      });</code></pre>    <h2>About</h2>    <p>Adapter 方法说明</p>    <p>对数据操作的方法,最后都会 notify</p>    <table>     <thead>      <tr>       <th>方法名</th>       <th>备注</th>      </tr>     </thead>     <tbody>      <tr>       <td>getItem(position)</td>       <td>获取对应的 item</td>      </tr>      <tr>       <td>getItemCount()</td>       <td>获取 item 的数量</td>      </tr>      <tr>       <td>getDataList()</td>       <td>获取数据集合</td>      </tr>      <tr>       <td>refresh(dataList)</td>       <td>清空原先的数据再加入新的数据后刷新</td>      </tr>      <tr>       <td>add(position, data)</td>       <td>在指定位置插入</td>      </tr>      <tr>       <td>add(data)</td>       <td>在最后位置插入</td>      </tr>      <tr>       <td>addAll(positionStart, dataList)</td>       <td>在指定开始位置插入一个集合</td>      </tr>      <tr>       <td>addAll(dataList)</td>       <td>在最后位置插入一个集合</td>      </tr>      <tr>       <td>move(fromPosition, toPosition)</td>       <td>把 fromPosition 的 item 移动到 toPosition</td>      </tr>      <tr>       <td>change(position, data)</td>       <td>改变指定位置的数据,然后刷新 item</td>      </tr>      <tr>       <td>remove(position)</td>       <td>移除指定位置的 item</td>      </tr>      <tr>       <td>clear()</td>       <td>清空</td>      </tr>      <tr>       <td>setItemListener(listener)</td>       <td>设置监听,也可在构造方法中传入</td>      </tr>      <tr>       <td>getItemHolder(position)</td>       <td>获取指定位置的 ItemWrapper ,该方法由 ItemProvide 接口声明,继承 RecyclerAdapter 需要实现该方法</td>      </tr>     </tbody>    </table>    <p>ItemWrapper 说明</p>    <p>该类用于控制 itemView 的 Type</p>    <table>     <thead>      <tr>       <th>变量成员</th>       <th>备注</th>      </tr>     </thead>     <tbody>      <tr>       <td>mLayoutResId</td>       <td>布局文件Id</td>      </tr>      <tr>       <td>mHolderClass</td>       <td>RecyclerHolder 的子类 Class</td>      </tr>     </tbody>    </table>    <p>欢迎大家 PR 、提 issues ,一起加入更多方便实用的方法。</p>    <h2>Proguard</h2>    <pre>  <code class="language-java">-keepclassmembers public class * extends com.github.nukc.recycleradapter.RecyclerHolder {      public <init>(...);  }</code></pre>    <h2>Thanks</h2>    <ul>     <li><a href="/misc/goto?guid=4959644053987400831" rel="nofollow,noindex">StanKocken/EfficientAdapter</a> 封装用了反射,就是看到这个库才想到反射的。</li>    </ul>    <h2>License</h2>    <pre>  <code class="language-java">Copyright 2016, nukc    Licensed under the Apache License, Version 2.0 (the "License");  you may not use this file except in compliance with the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0    Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and  limitations under the License.</code></pre>    <p> </p>    <p> </p>