我想在我的Recycler视图中自动滚动列表项到顶部(firstVisible项)并获取该位置的视图,因此我可以从我的片段中突出显示它。
所以,这是我的片段代码的要点:
private void activateNewListItem(int position) {
mLayoutManager().scrollToPositionWithOffset(position, 0);
RecyclerView.ViewHolder viewHolder = mRecyclerView.findViewHolderForLayoutPosition(position);
View view = viewHolder.getItemView();
view.setBackgroundColor(getResources().getColor(R.color.esr_light_grey));
}
如果position
1 , 2 , 3 , 4 等,{{1返回一个有效的ViewHolder,
因为我猜RecyclerView已经为dataSet中的那些索引绘制了ViewHolders。
但是,如果我传递mRecyclerView.findViewHolderForPosition(position)
,如 25 ,position
将返回null,因为我认为,即使我调用了{,它还没有被绘制在它之上{1}}。
我能做些什么来实现这两件事?
将dataSet索引mRecyclerView.findViewHolderForPosition(position)
的列表项滚动到firstVisibleItem。
获取该listItem的View或ViewHolder对象,以便我可以更改背景或其他内容。
答案 0 :(得分:1)
而不是在其他地方访问ViewHolder,而是在适配器项目布局xml文件中寻找自定义选择器背景,跟踪适配器中的位置作为选定位置,并调用notifydatasetchanged()/ notifyItemRangeChanged()以在滚动后突出显示它完成。你已经有了scrollToPosition()方法,有一个滚动监听器来跟踪滚动状态。
选择器背景示例:item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=
"http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:drawable="@color/primary_dark" />
<item android:drawable="@android:color/transparent" />
</selector>
将其设置为项目xml的背景
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_selector">
<--- Your layout items here --->
</RelativeLayout>
您的适配器代码:
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> {
private final String[] list;
private int lastCheckedPosition = -1;
public SampleAdapter(String[] list) {
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.sample_layout, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.choiceName.setText(list[position]);
holder.containerView.setSelected(position == lastCheckedPosition);
}
@Override
public int getItemCount() {
return list.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.choice_name)
TextView choiceName;
@Bind(R.id. container_list_item)
View containerView;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastCheckedPosition = getAdapterPosition();
notifyItemRangeChanged(0, list.length);
}
});
}
}
}
滚动到任意位置:
recyclerview.scrollToPosition(position)
如果有帮助,请告诉我。
答案 1 :(得分:0)
更新ViewHolder信息需要几秒钟。只需滚动并延迟mRecyclerView.findViewHolderForLayoutPosition
调用几毫秒(100毫秒听起来合理)
答案 2 :(得分:0)
将此代码放入您将要滚动到第一个RecyclerView项目的片段中:
RecyclerView.ViewHolder holder = mRecyclerView
.findViewHolderForAdapterPosition(0);
while (holder == null) {
int firstVisiblePosition = mLayoutManager()
.findFirstCompletelyVisibleItemPosition();
RecyclerView.ViewHolder firstVisibleHolder = mRecyclerView
.findViewHolderForAdapterPosition(firstVisiblePosition);
View v = firstVisibleHolder.getItemView();
if (v != null) mRecyclerView.scrollBy(0, (int) v.getY());
else break;
holder = mRecyclerView.findViewHolderForAdapterPosition(0);
}
View view = null;
if (holder != null) view = holder.getItemView();
if (view != null) mRecyclerView.scrollBy(0, (int) view.getY());