demo图
注意点:
1.add delete等操作可以直接构造到adapter里面,然后通知刷新使用this.notifyItemInserted(pos);,确保有默认的刷新动画。
2.adapter中绑定View的onclick事件不直接使用view传进来的pos,因为随着add等操作 pos不立即更新,使用int realPos=holder.getLayoutPosition();
MainActivity.java
public class MainActivity extends Activity { private RecyclerView mRecyclerView; private List<String> mDatas; private LouisRecyclerViewAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); // LinearLayoutManager /** * 系统提供了3个实现类: LinearLayoutManager 现行管理器,支持横向、纵向。 GridLayoutManager * 网格布局管理器 StaggeredGridLayoutManager 瀑布就式布局管理器 */ mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); /* * mRecyclerView.addItemDecoration(new DividerItemDecoration(this, * DividerItemDecoration.VERTICAL_LIST));//简单的使用items.xml中android: * layout_margin设置 */ mAdapter = new LouisRecyclerViewAdapter(this, mDatas); mAdapter.setLouisOnItemClickListener(new louisOnItemClickListener() { @Override public void OnItemLongClick(View v, int pos) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "OnItemLongClick:pos=" + pos, 1).show(); } @Override public void OnItemClick(View v, int pos) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "OnItemClick:pos=" + pos, 1).show(); } }); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setItemAnimator(new DefaultItemAnimator());// 默认动画 其他参考https://github.com/gabrielemariotti/RecyclerViewItemAnimators } protected void initData() { mDatas = new ArrayList<String>(); for (int i = 'A'; i < 'z'; i++) { mDatas.add("" + (char) i);// (char)转换成字符 } } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.options_menu, menu); return true; // return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub int id = item.getItemId(); switch (id) { case R.id.action_add: mAdapter.louisAdd(1); break; case R.id.action_delete: mAdapter.louisDelete(1); break; case R.id.action_listview: mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); break; case R.id.action_gridview: mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); break; case R.id.action_gridview_hor: mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL)); break; case R.id.action_staggered: Intent intent = new Intent(this, StaggeredActivity.class); startActivity(intent); break; default: break; } return super.onOptionsItemSelected(item); } }
LouisRecyclerViewAdapter.java
public class LouisRecyclerViewAdapter extends RecyclerView.Adapter<LouisRecyclerViewAdapter.MyViewHolder> { public LouisRecyclerViewAdapter(Context mContext, List<String> mDatas) { super(); this.mContext = mContext; this.mDatas = mDatas; } private Context mContext; protected List<String> mDatas; //设置接口类 public interface louisOnItemClickListener { //接口方法 void OnItemClick(View v, int pos); void OnItemLongClick(View v, int pos); } private louisOnItemClickListener mLouisOnItemClickListener; public void setLouisOnItemClickListener(louisOnItemClickListener listener) { this.mLouisOnItemClickListener = listener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.items, parent, false)); return holder; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { holder.tv.setText(mDatas.get(position)); // 绑定每一个itemView的事件 抽取方法 供子类复用 setItemViewEvent(holder); } protected void setItemViewEvent(final MyViewHolder holder) { if (mLouisOnItemClickListener != null) { holder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int realPos = holder.getLayoutPosition(); mLouisOnItemClickListener.OnItemClick(holder.itemView, realPos); } }); // 绑定每一个itemView的事件 LongClick holder.itemView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub int realPos = holder.getLayoutPosition(); mLouisOnItemClickListener.OnItemLongClick(holder.itemView, realPos); return false; } }); } } @Override public int getItemCount() { return mDatas.size(); } void louisAdd(int pos) { mDatas.add(pos, "插入1条数据1"); this.notifyItemInserted(pos); } void louisDelete(int pos) { mDatas.remove(pos); this.notifyItemRemoved(pos); } class MyViewHolder extends ViewHolder { TextView tv; public MyViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.id_num); } } }
StaggeredActivity.java
public class StaggeredActivity extends Activity { private RecyclerView mRecyclerView; private List<String> mDatas; private LouisRecyclerView4StaggeredAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); //LinearLayoutManager /** * 系统提供了3个实现类: LinearLayoutManager 现行管理器,支持横向、纵向。 GridLayoutManager 网格布局管理器 StaggeredGridLayoutManager 瀑布就式布局管理器 */ mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)); /* mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));//简单的使用items.xml中android:layout_margin设置*/ mAdapter = new LouisRecyclerView4StaggeredAdapter(this,mDatas); mAdapter.setLouisOnItemClickListener(new louisOnItemClickListener() { @Override public void OnItemLongClick(View v, int pos) { // TODO Auto-generated method stub mAdapter.louisDelete(pos); } @Override public void OnItemClick(View v, int pos) { // TODO Auto-generated method stub mAdapter.louisDelete(pos); } }); mRecyclerView.setAdapter(mAdapter); } protected void initData() { mDatas = new ArrayList<String>(); for (int i = 'A'; i < 'z'; i++) { mDatas.add("" + (char) i);//(char)转换成字符 } } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.options_menu, menu); return true; //return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub int id=item.getItemId(); switch (id) {/* case R.id.action_listview: mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); break; case R.id.action_gridview: mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); break; case R.id.action_gridview_hor: mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.HORIZONTAL)); break; default: break; */} return super.onOptionsItemSelected(item); } }
LouisRecyclerView4StaggeredAdapter.java
public class LouisRecyclerView4StaggeredAdapter extends LouisRecyclerViewAdapter { private List<Integer> mHeights; public LouisRecyclerView4StaggeredAdapter(Context mContext, List<String> mDatas) { super(mContext, mDatas); // 模拟随机高度 mHeights = new ArrayList<Integer>(); for (int i = 0; i < mDatas.size(); i++) { mHeights.add((int) (100 + Math.random() * 300)); } } @Override public void onBindViewHolder(MyViewHolder holder, int position) { LayoutParams mLayoutParams = holder.itemView.getLayoutParams(); mLayoutParams.height = mHeights.get(position); holder.itemView.setLayoutParams(mLayoutParams); holder.tv.setText(mDatas.get(position)); setItemViewEvent(holder); } }
最后是开源的DividerItemDecoration
DividerItemDecoration.java
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * limitations under the License. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.State; import android.util.Log; import android.view.View; /** * This class is from the v7 samples of the Android SDK. It's not by me! * <p/> * See the license above for details. */ public class DividerItemDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[]{ android.R.attr.listDivider }; public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; private Drawable mDivider; private int mOrientation; public DividerItemDecoration(Context context, int orientation) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); setOrientation(orientation); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent) { Log.v("recyclerview - itemdecoration", "onDraw()"); if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } public void drawVertical(Canvas c, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext()); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } public void drawHorizontal(Canvas c, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } } }
<pre class="java" name="code">demo下载 :<pre class="java" name="code">http://download.csdn.net/detail/richiezhu/9215923