实现点击RecyclerView的item时,将上一个item的底图滑块平移到目标item
时间: 2024-03-17 18:47:20 浏览: 215
RecyclerView滑动到指定Position的方法
实现RecyclerView item点击后的平移动画,可以通过使用RecyclerView的ItemDecoration来实现。
具体步骤如下:
1. 在RecyclerView的Adapter中添加接口,用来监听item的点击事件,例如:
```
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
}
// ...
}
```
2. 在RecyclerView的ItemDecoration中实现平移动画,例如:
```
public class MyItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private int mDividerHeight;
private int mLastSelectedPosition = -1;
private int mCurrentSelectedPosition = -1;
public MyItemDecoration(Context context) {
mDivider = ContextCompat.getDrawable(context, R.drawable.divider);
mDividerHeight = mDivider.getIntrinsicHeight();
}
public void setSelectedPosition(int position) {
mLastSelectedPosition = mCurrentSelectedPosition;
mCurrentSelectedPosition = position;
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (mLastSelectedPosition == -1 || mCurrentSelectedPosition == -1) {
return;
}
View lastSelectedView = parent.getChildAt(mLastSelectedPosition);
View currentSelectedView = parent.getChildAt(mCurrentSelectedPosition);
if (lastSelectedView == null || currentSelectedView == null) {
return;
}
int lastSelectedTop = lastSelectedView.getTop();
int currentSelectedTop = currentSelectedView.getTop();
int delta = currentSelectedTop - lastSelectedTop;
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int lastSelectedBottom = lastSelectedView.getBottom() - mDividerHeight;
int currentSelectedBottom = currentSelectedView.getBottom() - mDividerHeight;
mDivider.setBounds(left, lastSelectedBottom, right, lastSelectedBottom + mDividerHeight);
mDivider.draw(c);
mDivider.setBounds(left, currentSelectedBottom - delta, right, currentSelectedBottom - delta + mDividerHeight);
mDivider.draw(c);
}
}
```
3. 在RecyclerView的Activity或Fragment中设置ItemDecoration和OnItemClickListener,例如:
```
public class MyActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private MyItemDecoration mItemDecoration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mRecyclerView = findViewById(R.id.recyclerview);
mAdapter = new MyAdapter();
mItemDecoration = new MyItemDecoration(this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addItemDecoration(mItemDecoration);
mAdapter.setOnItemClickListener(this);
}
@Override
public void onItemClick(View view, int position) {
mItemDecoration.setSelectedPosition(position);
mRecyclerView.invalidateItemDecorations();
}
}
```
这样,当RecyclerView的item被点击时,就会触发OnItemClickListener的回调方法,在里面设置当前选中的item的位置,并调用RecyclerView的invalidateItemDecorations()方法来刷新ItemDecoration,从而实现平移动画。
阅读全文