嵌套的RecyclerView中smoothScrollToPosition无法正常工作

6

我有一个recyclerView,每个项目都是一个recyclerView。我想在特定条件下滚动到内部recyclerView的自定义位置。

我在innerRecyclerView使用了这段代码,但它没有起作用:

innerRecyclerView.smoothScrollToPosition(position); 

以及这个:

innerRecyclerView.scrollToPosition(position); 

还有这个:

layoutManager.scrollToPositionWithOffset(position, offset);

现在我有两个问题:
1)是否可以滚动到内部RecyclerView的自定义位置?
2)如果可以,怎么做?
这是初始化主要RecyclerView的代码:
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
        adapter = new FreightsListRVAdapter(new FreightListCallBack(), new FreightListVHFactory());
        rvFreightsList.setLayoutManager(layoutManager);
        rvFreightsList.setItemViewCacheSize(30);
        rvFreightsList.setNestedScrollingEnabled(true);
        rvFreightsList.setAdapter(adapter);

以下是初始化内部RecyclerView的代码:

LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), RecyclerView.VERTICAL, false);
        layoutManager.setItemPrefetchEnabled(true);
        layoutManager.setInitialPrefetchItemCount(7);
        rvFreights.setNestedScrollingEnabled(false);
        rvFreights.setLayoutManager(layoutManager);
        rvFreights.setItemViewCacheSize(20);

        adapter = new FreightsRVAdapter(new FreightCallBack(), new FreightSingleVHFactory());
        rvFreights.setAdapter(adapter);

这是我的主要RecyclerView适配器:

public class FreightsListRVAdapter extends ListAdapter<FreightListModel, FreightListVH> {

    private final FreightListVHFactory mFactory;
    private final PublishSubject<FreightListVHAction> mClickUserPS = PublishSubject.create();

    @Inject
    public FreightsListRVAdapter(@NonNull FreightListCallBack diffCallback, FreightListVHFactory factory) {
        super(diffCallback);
        mFactory = factory;
    }

    @NonNull
    @Override
    public FreightListVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return mFactory.create(parent);
    }

    @Override
    public void onBindViewHolder(@NonNull FreightListVH holder, int position) {
        holder.getVM().setObject(getItem(position));
        holder.bind();
        holder.itemOnClick(mClickUserPS);

    }

    public PublishSubject<FreightListVHAction> getmClickUserPS() {
        return mClickUserPS;
    }
}

以下是mainRecyclerView每一行的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatTextView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/txtDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingRight="10dp"
        android:textColor="#992C2C2C"
        android:textSize="11sp"
        app:fontFamily="@font/iransans"
        tools:ignore="HardcodedText,SmallSp" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/txtDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        android:gravity="right"
        android:textColor="#2C2C2C"
        android:textSize="20sp"
        app:fontFamily="@font/iransans_medium"
        tools:ignore="RtlHardcoded"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvFreights"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none" />
</androidx.appcompat.widget.LinearLayoutCompat>

谢谢你。@Zoe - Masoud Mokhtari
尝试使用innerRecyclerView.scrollToPosition(position); - Shivam Yadav
你需要将内部RecyclerView的引用存储在某个ArrayList中(因为外部RecyclerView是动态的),之后可以使用该引用进行滚动。不要忘记在外部RecyclerView的onBindMethod中传递引用。 - Firu
@ShivamYadav 没有起作用。 - Masoud Mokhtari
@Firu 我尝试过了,但没有效果。 - Masoud Mokhtari
显示剩余8条评论
2个回答

1
在Kotlin-AndroidX中,recyclerview是一种解决方案。
val parentViewHolder = parentRecyclerView?.findViewHolderForAdapterPosition(parentPosition) as? ParentViewHolder?

var childY = 0F
if (parentViewHolder != null)
  childY = parentViewHolder.childRecyclerView?.getChildAt(lastPosition)?.y ?: 0F
parentRecyclerView.scrollBy(0, childY.toInt())

PrentViewHolder.kt

class PrentViewHolder(
    view: InflateBinding
): RecyclerView.ViewHolder(view.root) {
    var recyclerView: RecyclerView? = null

    fun populateData(data: Data) {
        viewDataBinding.recyclerview.adapter = adapter
        recyclerView = viewDataBinding.recyclerview
    }
}

0

请您尝试一下这个:

 float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
 mainRecyclerview.smoothScrollTo(0, (int) y);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接