RecyclerView滚动到位置并获取该视图 [英] RecyclerView scroll to position and get that view

本文介绍了RecyclerView滚动到位置并获取该视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将列表项自动滚动到回收器视图的顶部(firstVisible项),并在该位置获取视图,以便可以从片段中突出显示它。



所以,这是我的片段代码的要旨:

 私人无效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));
}

如果职位 1 2 3 4 等, mRecyclerView.findViewHolderForPosition(位置)返回一个有效的ViewHolder,
,因为我想RecyclerView已经为dataSet中的那些索引绘制了ViewHolders。



但是,如果我通过职位 25 mRecyclerView.findViewHolderForPosition(position)返回null,因为我假设尚未绘制,即使我调用 mLayoutManager.scrollToPositionWithOffset(position,0)在它上面。



我该怎么做才能实现这两件事?


  1. 将dataSet索引 position 的列表项滚动到firstVisibleItem。 / p>


  2. 获取该listItem的View或ViewHolder对象,因此我可以更改背景或其他内容。



解决方案

不是在其他地方访问ViewHolder,而是在适配器项布局xml文件中使用自定义选择器背景,而是在您的适配器中跟踪位置适配器作为选定位置,并在滚动完成后调用notifydatasetchanged()/ notifyItemRangeChanged()使其突出显示。您已经具有scrollToPosition()方法,并具有滚动侦听器以跟踪滚动状态。



选择器背景示例:item_selector.xml

 < ;? xml version = 1.0 encoding = utf-8?> 
<选择器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>
< ---您在此处的布局项目--->
< / RelativeLayout>

您的适配器代码:

 公共类SampleAdapter扩展了RecyclerView.Adapter< SampleAdapter.ViewHolder> {

私人最终字串[]清单;
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持有者=新的ViewHolder(view);
退税持有人;
}

@Override
public void onBindViewHolder(ViewHolderholder,int position){
holder.choiceName.setText(list [position]);
holder.containerView.setSelected(position == lastCheckedPosition);
}

@Override
public int getItemCount(){
return list.length;
}

公共类ViewHolder扩展了RecyclerView.ViewHolder {
@Bind(R.id.choice_name)
TextView choiceName;
@Bind(R.id。container_list_item)
查看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)

让我知道是否有帮助。


I want to auto scroll a list item to top (firstVisible item) in my recycler view and get the view at that position, so I can highlight it, from my fragment.

So, this is the gist of my fragment code :

    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));
}

If position is 1,2,3,4 etc, mRecyclerView.findViewHolderForPosition(position) returns a valid ViewHolder, because I guess RecyclerView has drawn ViewHolders for those indexes in dataSet.

However, if I pass in position as say, 25, mRecyclerView.findViewHolderForPosition(position) returns null, because I assume, it hasn't been drawn yet, even thoughI called mLayoutManager.scrollToPositionWithOffset(position, 0) above it.

What can I do to achieve these two things?

  1. Scroll the list item of dataSet index position to firstVisibleItem.

  2. Get the View or ViewHolder object of that listItem, so I can change the background or whatever.

解决方案

Instead of accessing ViewHolder somewhere else, go for a custom selector background in your adapters item layout xml file instead, track the position in your adapter as selected position and call notifydatasetchanged()/notifyItemRangeChanged() to highlight it once the scroll is complete. You already have the scrollToPosition() method, have a scroll listener to track the scroll state.

Example for selector background : 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>

Set it as background to your item 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>

Your adapter code :

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);

                }
            });
        }
    }
}

To scroll to any position :

recyclerview.scrollToPosition(position)

Let me know if this helps.

这篇关于RecyclerView滚动到位置并获取该视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆