ANdroid RecyclerView的基本用法

MargheritaJ 8年前
   <p>单一的RecyclerView实现的效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d9f31ac42d74ad7e4459efcde785d415.jpg"></p>    <p style="text-align:center">仿QQ动态界面.jpg</p>    <p>上图随意用的几个Android自带的icon,不太美观请轻点吐槽....</p>    <p>既然本篇博文介绍的是基本用法,仅提供本人拙见,就不手把手教程如何实现,所以请伸手党止步哈.</p>    <p>引用</p>    <ul>     <li>Gradle依赖 <pre>  <code class="language-java">compile 'com.android.support:recyclerview-v7:25.0.1'</code></pre> </li>     <li>主布局引用 <pre>  <code class="language-java"><android.support.v7.widget.RecyclerView        android:id="@+id/recyclerview"        android:scrollbars="none"        android:layout_width="match_parent"        android:layout_height="match_parent" /></code></pre> </li>    </ul>    <p>LayoutManager:</p>    <p>这是一个抽象类,负责将每个Item视图在RecylerView中的布局,系统提供了3个实现类:</p>    <ul>     <li>LinearLayoutManager 现行管理器,支持横向、纵向。</li>     <li>GridLayoutManager 网格布局管理器</li>     <li> <p>StaggeredGridLayoutManager 瀑布就式布局管理器</p> <pre>  <code class="language-java">LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(MainActivity.this);   //GridLayoutManager mGridLayoutManager = new GridLayoutManager(MainActivity.this, 4);   /*StaggeredGridLayoutManager mStaggeredGridLayoutManager =                         new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);*/      mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//竖直方向                                              //水平方向:LinearLayoutManager.HORIZONTAL</code></pre> </li>    </ul>    <p>ItemAnimator:</p>    <p>ItemAnimator也是一个抽象类,系统为我们提供了一种默认的实现类,当Adapter执行notifyItemRemoved()、</p>    <p>notifyItemInserted()进行增加/删除某一项Item时会有动画效果,还用一种是移动Item。</p>    <pre>  <code class="language-java">DefaultItemAnimator mDefaultItemAnimator=new DefaultItemAnimator();</code></pre>    <p>DividerItemDecoration</p>    <p>顾名思义就是Item间的分割线,实现很简单(注意RecyclerView包版本)</p>    <pre>  <code class="language-java">DividerItemDecoration mDividerItemDecoration =new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL);</code></pre>    <p>分割线样式在你的AppTheme里面添加:</p>    <pre>  <code class="language-java"><!-- Base application theme. -->  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">      ...     <item name="android:listDivider">@drawable/divider_bg</item> //shape  </style></code></pre>    <p>RecyclerViewAdapter</p>    <p>Adapter 没有提供Item点击回调给我们,因此,我们需要主动回调Item或Item内控件的点击事件。</p>    <pre>  <code class="language-java">public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {      Context mContext;         OnItemClickListener onItemClickListener;        List<UserInfo> userInfoList;        public RecyclerViewAdapter(Context mContext,List<UserInfo> userInfoList) {          this.mContext = mContext;          this.userInfoList = userInfoList;      }            @Override      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {          View view = LayoutInflater.from(mContext).inflate(R.layout.item_data_list, parent, false);          return new ViewHolder(view);      }        @Override      public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {          ViewHolder vh = (ViewHolder) holder;          /*当ItemView被点击时,我们通过接口回调给调用方法。如果还需对外提供ItemView内某个Button或            TextView的点击事件,将vh.itemView改为相应控件即可*/          UserInfo userInfo = userInfoList.get(position);          if(userInfo.getName()!=null) {               vh.tv_name..setText(userInfo.getName());           }          vh.itemView.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  if (onItemClickListener != null) {                      onItemClickListener.onItemClick();                  }              }          });      }        //向外提供点击回调方法。      public void setOnItemClickListener(OnItemClickListener onItemClickListener) {          this.onItemClickListener = onItemClickListener;      }        //Item的点击接口      public interface OnItemClickListener {          void onItemClick();      }        @Override      public int getItemCount() {          //Item总数          return userInfoList !=null ? userInfoList.size() : 0;      }        @Override      public int getItemViewType(int position) {            return super.getItemViewType(position);      }        private class ViewHolder extends RecyclerView.ViewHolder {          TextView tv_name;          ViewHolder(View itemView) {              super(itemView);              tv_name = (TextView) itemView.findViewById(R.id.tv_name);          }      }  }</code></pre>    <p>以上是最简单的Adapter写法,粗略介绍一下每个方法的作用</p>    <ul>     <li> <p>getItemCount():</p> <p>视图总数,如果我们不需要多样式的Item显示不同的数据,可以像上面写的一样返回List数据的个数。如果我们需要像博文开头的效果图那样的效果,就需要我们计算好所有Item的数量,例如头布局数+中间布局数+脚布局数+分割线数。</p> </li>     <li> <p>getItemViewType(int position):</p> <p>该方法里有个参数position,当Adapter正在加载位置为position的ItemView视图样式时,通过这个方法获取到我们自定义的ItemViewType,再一步步的通过这个ItemViewType加载视图样式然后填充数据...例如效果图,头部布局的位置(position)为0,我们设置头部的CardView布局的ItemViewType为0(不一定是0),在onCreateViewHolder方法中就可以通过viewType来加载相应的Item视图</p> <pre>  <code class="language-java">@Override  public int getItemViewType(int position) {    if (position == 0){        return  topViewType;//private final int topViewType = 0;    }    ...  }</code></pre> </li>     <li> <p>onCreateViewHolder(ViewGroup parent, int viewType):</p> <p>既然要实现多样式,那么你不同的Item的样式要改吧,我们可以通过viewType去加载相应的布局,并将布局传入ViewHolder进行保存,以达复用。</p> <pre>  <code class="language-java">@Override  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    if (viewType == topViewType){        View view = LayoutInflater.from(mContext).inflate(R.layout.item_top, parent, false);        return new ViewHolder(view);    }    ...  }</code></pre> </li>     <li> <p>onBindViewHolder(RecyclerView.ViewHolder holder, int position) :</p> <p>好了,拿到Adapter的position位置上的ViewHolder,我们将数据绑定在holder上。但是这里的position是Adapter的位置。也就是说,如果现在要绑定中间布局的数据,我们的position为头布局Item数+position。</p> <pre>  <code class="language-java">@Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {        if (holder instanceof ViewHolder){            ViewHolder vh = (ViewHolder) holder;        }        ...    }</code></pre> </li>     <li> <p>调用</p> <pre>  <code class="language-java">RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);  recyclerView.setItemAnimator(mDefaultItemAnimator);//设置默认动画(可选)  recyclerView.setLayoutManager(mLinearLayoutManager);//设置布局管理器(必选)  recyclerView.addItemDecoration(new DividerItemDecoration(this, mDividerItemDecoration);//设置分割线(可选)  RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, userInfoList);  recyclerView.setAdapter(recyclerViewAdapter);//加载适配器(必选)</code></pre> </li>    </ul>    <p> </p>    <p>来自:http://www.jianshu.com/p/d734c1f62e84</p>    <p> </p>