我想在回收者视图中启用滚动/快速滚动。我的意思是,如果用户执行挥动动作,则视图会继续滚动并开始减速(我想这就是快速滚动)。
我在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>
我将布局管理器设置为线性
RecyclerView itemsView = findViewById(R.id.items);
stocksView.setLayoutManager(new LinearLayoutManager(this));
我确实在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();
}
}
我的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();
}
}
无论如何,只要我尝试滚动,它就会在我抬起手指的那一秒停止。如何获取它才能继续尝试滚动。
答案 0 :(得分:1)
从布局中删除ScrollView
,并且不要使用wrap_content
作为RecyclerView
的高度。 RecyclerView本身已提供滚动功能,您可以使用ScrollView上的任何layout_
属性来使RecyclerView尺寸合适。
答案 1 :(得分:0)
您是这样写的:
我在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"/>
但是RecyclerView
已经支持fling,请从xml文件中删除ScrollView
。并设置RecyclerView match_parent
的高度,如下所示:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/items"/>
答案 2 :(得分:0)
使用滚动视图时,您需要关闭nestedscroll enable。
recyclerview.setNestedScrollingEnabled(false);
答案 3 :(得分:0)
1-将ScrollView更改为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-使用recyclerview.setNestedScrollingEnabled(false);