简体   繁体   English

在RecyclerView上快速滚动

[英]Fast Scrolling on RecyclerView

I would like to enable fling/fast scrolling on the recycler view. 我想在回收者视图中启用滚动/快速滚动。 What I mean is that if user performs fling motion, the view continues scrolling and starts decelerating (I guess that's called fast scrolling). 我的意思是,如果用户执行晃动动作,则视图会继续滚动并开始减速(我想这就是快速滚动)。

I have a RecyclerView in a ScrollView 我在ScrollView中有一个RecyclerView

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scroll">

    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/items"/>

</ScrollView>

I set layout manager to Linear 我将布局管理器设置为线性

    RecyclerView itemsView = findViewById(R.id.items);
    stocksView.setLayoutManager(new LinearLayoutManager(this));

I do have a custom adapter attached to the RecyclerView 我确实有一个自定义适配器连接到RecyclerView

public abstract class RecyclerAdapter<E>
        extends RecyclerView.Adapter<ItemView>
{
    private ItemTouchHelper touchHelper;

    protected final List<E> items = new ArrayList<>();

    protected RecyclerAdapter()
    {
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemView holder, int position)
    {
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView)
    {
        super.onAttachedToRecyclerView(recyclerView);
        touchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(this));
        touchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    public int getItemCount()
    {
        return items.size();
    }
}

And my ItemTouchHelperCallback is 我的ItemTouchHelperCallback是

public class ItemTouchHelperCallback
        extends ItemTouchHelper.Callback
{
    ItemTouchHelperCallback(RecyclerAdapter adapter)
    {
    }

    @Override
    public boolean isItemViewSwipeEnabled()
    {
        return true;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
    {
        int dragFlags  = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = 0;
        return ItemTouchHelper.Callback.makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target)
    {
        if (source.getItemViewType() != target.getItemViewType())
        {
            return false;
        }

        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int i)
    {
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState)
    {
        // We only want the active item to change
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE)
        {
            // Let the view holder know that this item is being moved or dragged
            ((ItemView) viewHolder).onItemSelected();
        }

        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
    {
        super.clearView(recyclerView, viewHolder);
        // Tell the view holder it's time to restore the idle state\
        ((ItemView) viewHolder).onItemClear();
    }
}

Yet somehow whenever I try to scroll it stops the second I lift my finger. 但是无论如何,只要我尝试滚动,它就会在我抬起手指的第二秒停止。 How do I get it to continue trying to scroll. 我如何获得它才能继续尝试滚动。

Remove the ScrollView from your layout, and don't use wrap_content for the RecyclerView 's height. 从布局中移除ScrollView ,并且不要将wrap_content用作RecyclerView的高度。 RecyclerView already provides scrolling by itself, and you can use whatever layout_ attributes you had on the ScrollView to make the RecyclerView the right size. RecyclerView已经可以layout_提供滚动,您可以使用ScrollView上具有的layout_属性来使RecyclerView具有正确的大小。

You wrote this: 你这样写:

I have a RecyclerView in a ScrollView 我在ScrollView中有一个RecyclerView

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scroll">

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/items"/>

But RecyclerView already supports fling, remove ScrollView from xml file. 但是RecyclerView已经支持fling,请从xml文件中删除ScrollView And set the height of RecyclerView match_parent , like below: 并设置RecyclerView match_parent的高度,如下所示:

  <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/items"/>

使用滚动视图时,您需要关闭nestedscroll enable。

recyclerview.setNestedScrollingEnabled(false);

1- Change ScrollView to NestedScrollView 1-将ScrollView更改为NestedScrollView

2- Use app:layout_behavior="@string/appbar_scrolling_view_behavior" in both Recyclerview and NestedScrollView 2-在Recyclerview和NestedScrollView中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"

<NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/scroll">

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/items"/>

3- Use recyclerview.setNestedScrollingEnabled(false); 3-使用recyclerview.setNestedScrollingEnabled(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中快速滚动RecyclerView - Fast Scrolling a RecyclerView in Android RecyclerView index=-1 快速滚动到底部时 - RecyclerView index=-1 when scrolling fast to the bottom RecyclerView和Firebase存储快速滚动ExoPlayer异常 - RecyclerView and Firebase Storage fast scrolling ExoPlayer exception 滚动RecyclerView太快时发生奇怪的事情 - something weird happens while scrolling RecyclerView too fast 快速滚动时,如何解决recyclerview中viewpager上的意外行为? - How to fix unexpected behavior on viewpager in recyclerview when fast scrolling? 在Android中,有没有办法在新的RecyclerView中实现快速滚动? - In Android, is there any way to implement fast scrolling in the new RecyclerView? 快速滚动并在onBindViewHolder上调用API后,RecyclerView崩溃 - RecyclerView is crashing after fast scrolling and calling API onBindViewHolder 使用DPAD快速滚动时,RecyclerView onCreateViewHolder过度调用 - RecyclerView onCreateViewHolder called excessively when scrolling fast with DPAD RecyclerView 崩溃同时快速滚动和过滤 - RecyclerView Crash while fast scrolling and filtering at the same time 快速滚动后,BottomSheetDialogFragment 中的 RecyclerView 项目需要双击 - RecyclerView items inside BottomSheetDialogFragment need double touch after fast scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM