当前位置: 首页>移动开发>正文

安卓开发 RecyclerView item变化动画 androidx.recyclerview.widget.recyclerview

一、添加依赖

app.gradle里添加依赖:

compile 'com.android.support:recyclerview-v7:26.+'

如果使用的是androidx:

implementation 'androidx.recyclerview:recyclerview:1.1.0'

二、基本使用方法

1、activity的xml布局

就是写一个RecyclerView的控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

2、列表的item布局

随意写了一下,此处纯展示功能,所以就写了个TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#d4d2d2"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:background="#FFFFFF"
        android:layout_height="200dp"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textSize="22sp"
        android:text="item"/>

</LinearLayout>

3、Adapter

public class InfoRecyclerViewAdapter extends RecyclerView.Adapter<InfoRecyclerViewAdapter.ViewHolder> {

    private ArrayList<String> mData;
     private Context mContext;//记录一个Context,如果以后需要弹出个提示框这种的,也好用
  
    /**
     * 事件回调监听
     */
    private InfoRecyclerViewAdapter.OnItemClickListener onItemClickListener;

    public InfoRecyclerViewAdapter(Context context,ArrayList<String> data) {
        this.mContext = context;
        this.mData = data;      
    }
   public InfoRecyclerViewAdapter(Context context) {
      this.mContext = context;
   }

     /**
     * 新增数据:得到此次选择的数据后,更新数据
     */
    public void initData(ArrayList<String> data,int pageIndex) {
       if (mData == null) {
            mData = new ArrayList<>();
        }
       if(pageIndex == 0){//现在增加的是第一页数据,则清除之前的数据
          mData.clear();
       }
        if (data != null) {
            int start = mData.size();  
            mData.addAll(data);         
            int count = mData.size() - start;
            notifyItemRangeChanged(start, count);
        }
    }
   
   /**
   * 提供给外部获取数据源的方法
   */
     public List<String> getDatas() {
        List<String> list = new ArrayList<>(mData);
        return list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        // 实例化展示的view
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_vertical, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // 绑定数据
        holder.tvItem.setText(mData.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, pos);
                }
                //表示此事件已经消费,不会触发单击事件
                return true;
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if(position == getItemCount() - 1){
            return 1;
        }else {
            return 0;
        }
    }
    
    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    /**
     * 往列表第一行新增一个item
     */
    public void addNewItem() {
        if(mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(0, "new Item");
        notifyItemInserted(0);
    }

    /**
     * 删除item
     */
    public void deleteItem(int position) {
        if(mData == null || mData.isEmpty()) {
            return;
        }
        if(position >= 0 && position < mData.size()){
           mData.remove(position);
           notifyItemRemoved(position);
        }
    }

    /**
     * 设置回调监听
     * @param listener
     */
    public void setOnItemClickListener(InfoRecyclerViewAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_item)
        TextView tvItem;

        ViewHolder(View view) {
            super( view);
            ButterKnife.bind(this, view);
        }
    }
}

activity往列表中新增数据:

ArrayList<String> mDataList= new ArrayList<>();//activity里的数据源,获取到数据后就传到adapter里更新列表

获取到数据后就传到adapter里更新列表:

infoRecyclerViewAdapter.initData(mDataList);
 // 由于Adapter内部是直接在首个Item位置做增加操作,增加完毕后列表移动到首个Item位置
infoLayoutManager.scrollToPosition(0);

删除某个位置的item:

infoRecyclerViewAdapter.deleteItem(2);//比如我要删除位置为第三的item
// 删除完毕后列表移动到Item位置
 infoLayoutManager.scrollToPosition(2);

4、activity

//activity里的数据源,获取到数据后就传到adapter里更新列表
ArrayList<String> mDataList= new ArrayList<>();

网格列表(像GridView):

//竖直滑动,每行只显示四个
 GridLayoutManager infoLayoutManager = new GridLayoutManager( this,4);
//如果在初始化adapter的时候就已经获取到了数据源可以在初始化adapter的时候就传入数据源
  InfoRecyclerViewAdapter infoRecyclerViewAdapter = new InfoRecyclerViewAdapter(this,mDataList);

  //如果像是异步网络请求数据后再刷新列表的话:
   InfoRecyclerViewAdapter infoRecyclerViewAdapter = new InfoRecyclerViewAdapter(this);
   //获取到数据后就传到adapter里更新列表
   infoRecyclerViewAdapter.initData(mDataList,0);
// 设置布局管理器
  infoRecyclerView.setLayoutManager(infoLayoutManager);
   // 设置adapter
   infoRecyclerView.setAdapter(infoRecyclerViewAdapter);
    //设置间隔样式
    infoRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));

垂直列表(像ListView):数据更新方式同上

infoLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// 设置布局管理器
infoRecyclerView.setLayoutManager(infoLayoutManager);
// 设置adapter
infoRecyclerViewAdapter = new InfoRecyclerViewAdapter(mDataList);
//item的点击事件
infoRecyclerViewAdapter.setOnItemClickListener(new InfoRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(ActVerticalRecycler.this,"click " + position + " item", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(ActVerticalRecycler.this,"long click " + position + " item", Toast.LENGTH_SHORT).show();
            }
 });
infoRecyclerView.setAdapter(infoRecyclerViewAdapter);
//设置间隔样式
// infoRecyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL));
infoRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
// 设置Item添加和移除的动画
 infoRecyclerView.setItemAnimator(new DefaultItemAnimator());

水平列表:

// 设置布局管理器
        infoLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        infoRecyclerView.setLayoutManager(infoLayoutManager);
        // 设置adapter
        infoRecyclerViewAdapter = new InfoRecyclerViewAdapter( mDataList );
        infoRecyclerView.setAdapter(infoRecyclerViewAdapter);
        //设置间隔样式
//        infoRecyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));
        infoRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));

设置列表item之间的间隔

//设置列表item之间的间隔
        recycleView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = 2;
                outRect.right = 2;
                outRect.top = 2;
                outRect.bottom = 2;
//                super.getItemOffsets(outRect, view, parent, state);
            }
        });

三、 长按拖动

/**
 * 长按拖拽
 */
public class ItemDragHelperCallBack extends ItemTouchHelper.Callback {

    private OnItemDragListener onItemDragListener;

    public ItemDragHelperCallBack(OnItemDragListener onItemDragListener) {
        this.onItemDragListener = onItemDragListener;
    }

    /**
     * 返回可以滑动的方向
     *
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        int dragFlags;
        if (manager instanceof GridLayoutManager || manager instanceof StaggeredGridLayoutManager) {
            //网格布局管理器允许上下左右拖动
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        } else {
            //其他布局管理器允许上下拖动
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
        return makeMovementFlags(dragFlags, 0);
    }

    /**
     * 拖拽到新位置时候的回调方法
     *
     * @param recyclerView
     * @param viewHolder
     * @param target
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //不同Type之间不允许移动
        if (viewHolder.getItemViewType() != target.getItemViewType()) {
            return false;
        }
        if (onItemDragListener != null) {
            onItemDragListener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        }
        return true;
    }

    /**
     * 当用户左右滑动的时候执行的方法
     *
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

    /**
     * 重写拖拽不可用
     * @return
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }

    public void setOnItemDragListeber(OnItemDragListener onItemDragListener) {
        this.onItemDragListener = onItemDragListener;
    }

    public interface OnItemDragListener {
        void onItemMove(int startPos,int endPos);
    }
}

activity中使用:

mItemTouchHelper = new ItemTouchHelper(new ItemDragHelperCallBack(new ItemDragHelperCallBack.OnItemDragListener() {

            @Override
            public void onItemMove(int startPos, int endPos) {
                //交换变换位置的集合数据并刷新
                Collections.swap(mAdapter.getDatas(), startPos, endPos);
                mAdapter.notifyItemMoved(startPos, endPos);
            }
        }));
//关联RecyclerView
mItemTouchHelper.attachToRecyclerView(recyclerView);

四、滑动冲突

1、ScrollView嵌套RecycleView

xml布局:

<ScrollView
        android:layout_below="@+id/title_bar"
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_marginTop="20dp"
                    android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
    </ScrollView>

代码设置:

GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,4){
            @Override
            public boolean canScrollVertically() {
                //解决ScrollView里存在多个RecyclerView时滑动卡顿的问题
                return false;
            }
        };
        recyclerView.setLayoutManager(gridLayoutManager);
        //解决数据加载不完的问题
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        recyclerView.setFocusable(false);

五、recyclerview的item位序

1、Recyclerview.getLayoutPosition()问题

使用Recyclerview 时,如果要添加item的点击监听等功能,可以在Recyclerview.Adapter的onBindViewHolder中设置:

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.tv.setHeight(150*(1+position%4));
    holder.tv.setWidth(150*(1+position%4));
    holder.tv.setText(data.get(position));
    if(mOnItemClickListener!=null){
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos=holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(v,pos);
            }
        });
        holder.tv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos=holder.getLayoutPosition();
                mOnItemClickListener.onItemLongClick(v,pos);
                return false;
            }
        });
    }
}

注意这里使用了ViewHolder的getLayoutPosition方法,此方法返回的pos值与onBindViewHolder方法传入的position值有可能不同。
根据SDK中的解释,在Recyclerview 进行添加、移除item等操作时,position位置可能会变化,而所有的adapter的刷新并不总是及时的,只有这个方法返回的才是当前item经过一些变换后所处的真正位置。
getLayoutPosition:返回布局中最新的计算位置,和用户所见到的位置一致,当做用户输入(例如点击事件)的时候考虑使用

getAdapterPosition:返回数据在Adapter中的位置(也许位置的变化还未来得及刷新到布局中),当使用Adapter的时候(例如调用Adapter的notify相关方法时)考虑使用

六、分割线

1、ItemDecoration样式drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:centerColor="#ff00ff00"
        android:endColor="#ff0000ff"
        android:startColor="#ffff0000"
        android:type="linear" />
    <size android:height="4dp"/>

</shape>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name= "android:listDivider">@drawable/bg_divider </item >       
    </style>

2、ItemDecoration

public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;

    public DividerGridItemDecoration(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, State state) {
        drawHorizontal(c, parent);
        drawVertical(c, parent);
    }

    /**
     * 绘制水平分割线
     */
    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();//item总个数
        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.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
    /**
     * 绘制垂直分割线
     */
    public void drawVertical(Canvas c, RecyclerView parent) {
        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 top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    /**
     * 列数
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }

    /**
     * 是否是最后一列
     */
    private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
                                int childCount) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0) // 如果是最后一列,则不需要绘制右边
            {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0) // 如果是最后一列,则不需要绘制右边
                {
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount) // 如果是最后一列,则不需要绘制右边
                    return true;
            }
        }
        return false;
    }

    /**
     * 是否是最后一行
     * @param parent
     * @param pos 当前item的位序
     * @param spanCount 列数
     * @param childCount item总个数
     * @return
     */
    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                              int childCount) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一行,则不需要绘制底部
            if (pos >= childCount){
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行,则不需要绘制底部
                if (pos >= childCount){
                    return true;
                }
            } else {
                // StaggeredGridLayoutManager 且横向滚动
                // 如果是最后一行,则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,
                               RecyclerView parent) {
        int spanCount = getSpanCount(parent);//列数
        int childCount = parent.getAdapter().getItemCount();//item总个数
        if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部
        {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        } else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边
        {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(),
                    mDivider.getIntrinsicHeight());
        }
    }
}



https://www.xamrdz.com/mobile/4a41960697.html

相关文章: