RecyclerView 点击条目自动将该条目吸附到顶部的位置
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView 点击条目自动将该条目吸附到顶部的位置相关的知识,希望对你有一定的参考价值。
(mBinding.rvSentenceList.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
position, 0
);
也可以在adapter中设置
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>
private List<MyData> mDataList;
private int mSelectedPosition = RecyclerView.NO_POSITION;
public interface OnItemClickListener
void onItemClick(int position);
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener listener)
mOnItemClickListener = listener;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
// inflate view and create ViewHolder
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
// bind data to ViewHolder
// set selected state
holder.itemView.setSelected(position == mSelectedPosition);
holder.itemView.setOnClickListener(v ->
// update selected state and notify adapter
mSelectedPosition = position;
notifyDataSetChanged();
// scroll to selected position
RecyclerView recyclerView = (RecyclerView) parent;
recyclerView.scrollToPosition(position);
);
@Override
public int getItemCount()
return mDataList.size();
以上是关于RecyclerView 点击条目自动将该条目吸附到顶部的位置的主要内容,如果未能解决你的问题,请参考以下文章