<android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/>
// 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:android
LinearLayoutManagergit
GridLayoutManagergithub
StaggeredGridLayoutManageride
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mLayoutManager = new GridLayoutManager(context,columNum); mRecyclerView.setLayoutManager(mLayoutManager);
注:在Grid佈局中也能夠設置列表的Orientation屬性,來實現橫向和縱向的Grid佈局。佈局
瀑布流就使用StaggeredGridLayoutManager吧,具體使用方法見http://blog.csdn.net/duanymin/article/details/44979355性能
1 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 2 public String[] datas = null; 3 public MyAdapter(String[] datas) { 4 this.datas = datas; 5 } 6 //建立新View,被LayoutManager所調用 7 @Override 8 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 9 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false); 10 ViewHolder vh = new ViewHolder(view); 11 return vh; 12 } 13 //將數據與界面進行綁定的操做 14 @Override 15 public void onBindViewHolder(ViewHolder viewHolder, int position) { 16 viewHolder.mTextView.setText(datas[position]); 17 } 18 //獲取數據的數量 19 @Override 20 public int getItemCount() { 21 return datas.length; 22 } 23 //自定義的ViewHolder,持有每一個Item的的全部界面元素 24 public static class ViewHolder extends RecyclerView.ViewHolder { 25 public TextView mTextView; 26 public ViewHolder(View view){ 27 super(view); 28 mTextView = (TextView) view.findViewById(R.id.text); 29 } 30 } 31 }
// 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! } });
// 設置item動畫 mRecyclerView.setItemAnimator(new DefaultItemAnimator());
若是要自定義動畫效果須要繼承ItemAnimatorthis
GitHub上的動畫效果:RecyclerViewItemAnimatorsspa
這裏更新數據集不是用adapter.notifyDataSetChanged()
而是 notifyItemInserted(position)
與notifyItemRemoved(position)
不然沒有動畫效果。 .net
爲adapter中添加兩個方法:
public void addData(int position) { mDatas.add(position, "Insert One"); notifyItemInserted(position); } public void removeData(int position) { mDatas.remove(position); notifyItemRemoved(position); }