compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' 哪个版本无所谓。
2.xml中引用
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView>属性大差不差,特别的属性 百度~
3.activity中使用recycleView.setLayoutManager(new LinearLayoutManager(this));//listview样式
final RecycleAdapter adapter = new RecycleAdapter(this, list);//实现适配器,传入数据
recycleView.setItemAnimator(new DefaultItemAnimator());//每一项的添加和删除的动画(这里是默认效果)
recycleView.setAdapter(adapter);//recycleView与适配器绑定
4.RecycleAdapter创建(继承的adapter<xxx>中xxx必须要填写)
public class RecycleAdapter extends PullableRecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
Context context;
ArrayList<String> list; public RecycleAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
} @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_transactioni_detail, parent, false));
return viewHolder;
} /** * view与数据的绑定
*
* @param holder * @param position */ @Override public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_transaction_money.setText(list.get(position));
} /** * 总长度
*
* @return */ @Override public int getItemCount() {
return list.size();
} /** * 增加数据
*
* @param position */ public void addItem(int position) {
list.add(position, "A");
notifyItemInserted(position);
} /** * 删除数据
*
* @param position */ public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
} /** * viewHodler的书写
*/
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_transaction_money; public MyViewHolder(final View itemView) {
super(itemView);
tv_transaction_money = (TextView) itemView.findViewById(R.id.tv_transaction_money);
}
}
}
5.使用时候通过add和delete相关的item即可。
adapter.addItem(0);//在recycleView的第一个位置添加, notifyItemRemoved(position)来更新;recycleView.scrollToPosition(0);//将
recycleView可见的移动到最上面。PullableRecyclerView实现了上拉加载下拉刷新的效果
http://blog.csdn.net/zhongkejingwang/article/details/38868463 陈靖_大神的博客实现的下拉上拉刷新框架。但是里面并没有适配RecyclerView。所以研究了一下。下面是源码。
package com.hgh.developmentframeworkhgh.pullableview; import android.content.Context; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.util.AttributeSet; /** * Created by hjg on 2017/7/25 0025. * @qq 347622814 */ public class PullableRecyclerView extends RecyclerView implements Pullable { private int lastPosition; public PullableRecyclerView(Context context) { super(context); } public PullableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public PullableRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 可以下拉 * * @return */ @Override public boolean canPullDown() { if (getChildCount() == 0) { return true; } else if (getChildAt(0).getTop() >= 0) { if (getLayoutManager() instanceof LinearLayoutManager) { int firstVisibleItem = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); if (firstVisibleItem == 0) { return true; } } else if (getLayoutManager() instanceof GridLayoutManager) { int firstVisibleItem = ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); if (firstVisibleItem == 0) { return true; } } } return false; } /** * 可以上拉 * * @return */ @Override public boolean canPullUp() { if (getChildCount() == 0) { return true; } else { RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { //通过LayoutManager找到当前显示的最后的item的position lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); } else if (layoutManager instanceof LinearLayoutManager) { lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { //因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组 //得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了 int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()]; ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(lastPositions); lastPosition = findMax(lastPositions); } //时判断界面显示的最后item的position是否等于itemCount总数-1也就是最后一个item的position //如果相等则说明已经滑动到最后了 if (lastPosition == getLayoutManager().getItemCount() - 1) { return true; } } return false; } private int findMax(int[] lastPositions) { int max = lastPositions[0]; for (int value : lastPositions) { if (value > max) { max = value; } } return max; } }自己测试了一下 ,完美的。