RecyclerView - 触摸后禁用fling stop

时间:2017-08-03 11:53:07

标签: android android-recyclerview

我正在创建一个老虎机应用程序,为此我使用RecycleView作为轨道。因此,RecyclerView禁用所有touch \ click \ scroll事件。

为了制作“旋转”,我使用RecyclerView.fling(...)方法,因为它有更好的动画效果。

当我触摸回收者视图时,

但是突然停止。 为什么?我该如何禁用它?

的xml:

 <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_rail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/margin_4"
        android:layout_marginStart="@dimen/margin_4"
        android:layout_weight="1"
        android:padding="@dimen/dp_16"/>

RecyclerView的初始化:

LinearLayoutManager mRailLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);       
mRailsBinding.rvRail.setHasFixedSize(true);
new LinearSnapHelper().attachToRecyclerView(mRailsBinding.rvRail);
mRailsBinding.rvRail.setLayoutManager(mRailLayoutManager);
mRailsBinding.rvRail.addOnItemTouchListener(new RecyclerViewDisabler());
mRailsBinding.rvRail.addOnScrollListener(railScrollListener); //checking here for time when scroll ended
mRailsBinding.rvRail.setAdapter(mRailAdapter);

适配器没什么特别的。它只是在视图中,并且没有click \ touch evenets。

RecyclerViewDisabler:

public class RecyclerViewDisabler implements RecyclerView.OnItemTouchListener {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return true;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {}

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean state) {}
}

1 个答案:

答案 0 :(得分:-1)

只需仔细阅读文档并找到解决方案。我需要手动使用dispatchTouchEvenet。见代码:

public class UntouchableRecyclerView extends RecyclerView {
    public UntouchableRecyclerView(Context context) {
        super(context);
    }

    public UntouchableRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public UntouchableRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return true; //consume
    }
}