簡體   English   中英

在子Recyclerview android中禁用滾動

[英]Disable Scrolling in child Recyclerview android

我的布局包含一個帶有子Recyclerview的Parent RecyclerView

我知道將列表放在另一個列表中是不好的但我必須這樣我才能使用子列表功能,如滑動和拖放

我的問題是,孩子Recyclerview獲得焦點並停止父母滾動如果觸摸點在它上面只是我想如果觸摸垂直在孩子上Recyclerview父母上下滾動,如果觸摸是水平或點擊然后子Recyclerview列表項左右滑動。 有沒有幫助實現這一目標?

我終於找到了解決方案。

創建自定義LinearLayoutManager

public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);

}

// it will always pass false to RecyclerView when calling "canScrollVertically()" method.
@Override
public boolean canScrollVertically() {
    return false;
}
}

然后像這樣實例化它以進行垂直滾動

CustomLinearLayoutManager customLayoutManager = new CustomLinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);

最后將自定義布局設置為回收器視圖的布局管理器

recyclerView.setLayoutManager(customLayoutManager);

雖然嵌入回收站視圖可能不是一個好習慣,但有時您無法避免。 像這樣的東西可能會起作用:

public class NoScrollRecycler extends RecyclerView {

    public NoScrollRecycler(Context context){
        super(context);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs, int style){
        super(context, attrs, style);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){

        //Ignore scroll events.
        if(ev.getAction() == MotionEvent.ACTION_MOVE)
            return true;

        //Dispatch event for non-scroll actions, namely clicks!
        return super.dispatchTouchEvent(ev);
    }
}

這將禁用滾動事件,但不會禁用單擊事件。 將此類用於“child”RecyclerView。 您希望PARENT recyclerview能夠滾動,而不是孩子。 那么這應該這樣做,因為父母將只是標准的RecyclerView,但孩子將是這個自定義的沒有滾動,但處理點擊。 可能需要禁用單擊父級RecyclerView ..不確定,因為我沒有測試過這個,所以請考慮它只是一個例子......

另外,要在XML中使用它(如果您不知道),請執行以下操作:

<com.yourpackage.location.NoScrollRecycler
     ...
     ... >

     ...
     ...

</com.yourpackage.location.NoScrollRecycler>

在您的ActivityName.java上,在onCreate()方法內寫入:

RecyclerView v = (RecyclerView) findViewById(R.id.your_recycler_view_id);
v.setNestedScrollingEnabled(false);

無論如何,如果您正在使用協調器布局,如果您想簡化操作,並且您想要禁用嵌套滾動。

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

再次,您應用相同的原則:在您的ActivityName.java上,在onCreate()方法內寫:

RecyclerView v = (RecyclerView) findViewById(R.id.your_recycler_view_id);
v.setNestedScrollingEnabled(false);

所以基本上在XML中,你必須指定app:layout_behavior

app:layout_behavior="@string/appbar_scrolling_view_behavior">

子級RecyclerView中的android:nestedScrollingEnabled="false"

你可以加

android:nestedScrollingEnabled="false"

用XML或XML格式的RecyclerView

childRecyclerView.setNestedScrollingEnabled(false);

到您的RecyclerView in Java。

編輯:-

childRecyclerView.setNestedScrollingEnabled(false); 將僅適用於android_version> 21台設備。 要在所有設備上工作,請使用以下命令

ViewCompat.setNestedScrollingEnabled(childRecyclerView, false);

你可以使用setNestedScrollingEnabled(false); 在子RecyclerView上停止在子RecyclerView內滾動。

在我的情況下代碼是

mInnerRecyclerView.setNestedScrollingEnabled(false); 其中mInnerRecyclerView是內部RecyclerView

我嘗試了許多建議的解決方案,找不到一個適用於我的情況。 我使用GridLayoutManager在ScrollView中有超過1個RecyclerView。 上面建議的結果導致ScrollView在我抬起手指時停止滾動(當我的手指被提升到RecyclerView上時,它沒有滑到視圖的頂部或底部)

查看RecyclerView源代碼,在onTouchEvent內部調用布局管理器:

final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
final boolean canScrollVertically = mLayout.canScrollVertically();

如果在自定義布局管理器中覆蓋這些,則返回false並停止滾動。 它還解決了ScrollView突然停止滾動的問題。

我想我已經太遲了但是在這里我找到了解決方案,如果它還在討厭某人:

    RecyclerView v = (RecyclerView); 
    findViewById(R.id.your_recycler_view_id);
    v.setNestedScrollingEnabled(false);
    sensorsRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent    event) {
            return true;
        }
    });
 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_marginTop="2dp"
                android:layout_marginLeft="2dp"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="2dp"
                android:nestedScrollingEnabled="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
                </RelativeLayout>

把代碼放在linner布局中......不需要務實地做任何事情

如果您不想創建自定義視圖,另一個選項是在RecyclerView前面創建相同大小的布局,並使其可單擊。

編輯:但不幸的是它也阻止了列表項的事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM