RecyclerView的简单使用

jopen 9年前

1、在布局文件中声明

<android.support.v7.widget.RecyclerView      android:id="@+id/my_recycler_view"      android:scrollbars="vertical"      android:layout_width="match_parent"      android:layout_height="match_parent"/>

2、在Activity中使用

// 1,找到这个View  mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);  // 2,设置布局管理LayoutManager  mLayoutManager = new LinearLayoutManager(this);  mRecyclerView.setLayoutManager(mLayoutManager);  // 3,(可选)如果可以确定每个item的高度是固定的,设置这个选项可以提高性能  mRecyclerView.setHasFixedSize(true);  // 4,设置Adapter  mRecyclerView.setAdapter(new MyAdapter());

目前SDK中提供了三种自带的LayoutManager:

  • LinearLayoutManager

  • GridLayoutManager

  • StaggeredGridLayoutManager

LinearLayoutManager
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
GridLayoutManager
mLayoutManager = new GridLayoutManager(context,columNum);  mRecyclerView.setLayoutManager(mLayoutManager);

:在Grid布局中也可以设置列表的Orientation属性,来实现横向和纵向的Grid布局。

StaggeredGridLayoutManager

瀑布流就使用StaggeredGridLayoutManager吧,具体使用方法见http://blog.csdn.net/duanymin/article/details/44979355

 

3、Adapter的创建

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {      public String[] datas = null;      public MyAdapter(String[] datas) {          this.datas = datas;      }      //创建新View,被LayoutManager所调用      @Override      public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);          ViewHolder vh = new ViewHolder(view);          return vh;      }      //将数据与界面进行绑定的操作      @Override      public void onBindViewHolder(ViewHolder viewHolder, int position) {          viewHolder.mTextView.setText(datas[position]);      }      //获取数据的数量      @Override      public int getItemCount() {          return datas.length;      }      //自定义的ViewHolder,持有每个Item的的所有界面元素      public static class ViewHolder extends RecyclerView.ViewHolder {          public TextView mTextView;          public ViewHolder(View view){          super(view);              mTextView = (TextView) view.findViewById(R.id.text);          }      }  }

4、添加单击事件

// 1 定义接口  public static interface OnRecyclerViewItemClickListener {      void onItemClick(View view , DataModel data);  }    // 2 添加接口和设置接口的方法  private OnRecyclerViewItemClickListener mOnItemClickListener = null;  public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {      this.mOnItemClickListener = listener;  }    // 3 在Adapter实现OnClickListener方法  public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener{      @Override      public ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);          ViewHolder vh = new ViewHolder(view);          //将创建的View注册点击事件          view.setOnClickListener(this);          return vh;      }      @Override      public void onBindViewHolder(ViewHolder viewHolder, final int i) {          viewHolder.mTextView.setText(datas.get(i).title);          //将数据保存在itemView的Tag中,以便点击时进行获取          viewHolder.itemView.setTag(datas.get(i));      }      ...      @Override      public void onClick(View v) {          if (mOnItemClickListener != null) {              //注意这里使用getTag方法获取数据            }      ...  }

在Activity中

mAdapter = new MyAdapter(getDummyDatas());  mRecyclerView.setAdapter(mAdapter);  mAdapter.setOnItemClickListener(new MyAdapter.OnRecyclerViewItemClickListener() {      @Override      public void onItemClick(View view, DataModel data) {          //DO your fucking bussiness here!      }  });

5、Item动画

// 设置item动画 mRecyclerView.setItemAnimator(new DefaultItemAnimator());

如果要自定义动画效果需要继承ItemAnimator

GitHub上的动画效果:RecyclerViewItemAnimators

 

6、更新数据

这里更新数据集不是用adapter.notifyDataSetChanged()而是 notifyItemInserted(position)与notifyItemRemoved(position) 否则没有动画效果。 

为adapter中添加两个方法:

public void addData(int position) {      mDatas.add(position, "Insert One");      notifyItemInserted(position);  }    public void removeData(int position) {      mDatas.remove(position);      notifyItemRemoved(position);  }

来自:http://www.cnblogs.com/huangzx/p/4594980.html