1. 程式人生 > >Android 巢狀XRecyclerView滑動遲緩以及上拉不好用解決辦法

Android 巢狀XRecyclerView滑動遲緩以及上拉不好用解決辦法

首先我們ScrollView要巢狀XrecyclerView等列表控制元件時,如果不需要上拉重新整理可以直接使用ScrollView,但是有上拉重新整理的需求時我們就需要選用NestedScrollView。話不多說,看程式碼。

<android.support.v4.widget.NestedScrollView
    android:id="@+id/StudingDetails_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="never"
    android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants">
        <com.jcodecraeer.xrecyclerview.XRecyclerView
            android:id="@+id/StudingDetails_evaluate_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="none"
            />

        </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

第一種ScrollView巢狀不需要上拉的列表控制元件:

    1. //解決ScrollView巢狀RecyclerView滑動遲緩問題
        mEvaluateList.setHasFixedSize(true);
        mEvaluateList.setNestedScrollingEnabled(false);

    2.重寫RecyclerView的管理器
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }

        };

  mEvaluateList.setLayoutManager(linearLayoutManager );

以上兩種都可以實現ScrollView巢狀RecyclerView或者ListView等滑動遲緩的問題。

第二種ScrollView巢狀需要上拉的列表控制元件核心程式碼():

//LinearLayoutManager是線性佈局,setOrientation可以設定他的方向是橫向還是縱向。
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mEvaluateList.setLayoutManager(layoutManager);
mEvaluateList.setPullRefreshEnabled(false);//設定XrecyclerView禁止下拉重新整理


//解決ScrollView巢狀RecyclerView滑動遲緩問題
mEvaluateList.setHasFixedSize(true);
mEvaluateList.setNestedScrollingEnabled(false);

Scrollview.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY > oldScrollY) {
            // 向下滑動
        }

        if (scrollY < oldScrollY) {
            // 向上滑動
        }

        if (scrollY == 0) {
            // 頂部
        }

        if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
            // 上拉重新整理實現
        }
    }
});

如果NestedScrollView巢狀XrecyclerView後進入頁面直接跳到列表最低部,在根部局加一行屬性:android:descendantFocusability="blocksDescendants"