如何在nestedscrollview中实现recyclerview scrolllistener

时间:2018-03-03 11:23:00

标签: android android-recyclerview android-nestedscrollview

我有一个使用nestedscrollview.inside的布局,我有2个recyclerview和其他一些视图的相对布局。

我想将scroll lisntener设置为recyclelerview之一。我尝试使用下面的代码,但它不能在嵌套的scrollview中工作。

的xml:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_deals_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relativeLayoutId"
            android:background="#015cb7"
            >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Deal"
            android:textColor="#f9f9f9"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchButtonId"
            android:src="@drawable/search"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="10dp"
            android:layout_centerVertical="true"
            android:background="?android:attr/selectableItemBackground"
            />
        </RelativeLayout>

        <Button

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/adPostButtonId"
            android:text="Post an ad"
            android:background="#c44"
            android:textColor="#f9f9f9"
            android:layout_below="@+id/relativeLayoutId"

            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Featured Ad"
            android:layout_below="@+id/adPostButtonId"
            android:id="@+id/featuredTvId"
            android:textSize="18sp"
            android:layout_marginTop="10dp"
            android:padding="5dp"
            android:textColor="#000000"
            />

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerViewId_featuredDeal"
            android:background="#fdfbfb"
            android:nestedScrollingEnabled="false"
            android:layout_below="@+id/featuredTvId"
            android:layout_marginTop="10dp"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Latest Ad"
            android:layout_below="@+id/recyclerViewId_featuredDeal"
            android:id="@+id/latestDealTvId"
            android:layout_marginTop="30dp"
            android:textSize="18sp"
            android:padding="5dp"
            android:textColor="#000000"
            />

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerViewId_deal"
            android:background="#fdfbfb"
            android:nestedScrollingEnabled="false"
            android:layout_below="@+id/latestDealTvId"
            android:layout_marginTop="10dp"
            />

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

我从活动中尝试过:

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (recyclerViewId_deal!=null && linearLayoutManager!=null){


                    visibleItemCount=recyclerViewId_deal.getChildCount();
                    totalItemCount=linearLayoutManager.getItemCount();
                    firstItem=linearLayoutManager.findFirstVisibleItemPosition();

                    if ((firstItem + visibleItemCount >= totalItemCount) && firstItem > 0 && oldScrollY > 0 && !scrollCheck) {
                        dealListShow(pageNo);
                        scrollCheck = true;
                    }
                }
            }
        });

但是我的滚动侦听器不在nestedscroll视图中工作,但在没有nestedscrollview的情况下运行良好。我尝试了一些SO解决方案,但没有用,有没有办法在嵌套的scrollview中滚动监听器

1 个答案:

答案 0 :(得分:0)

  

无需在nestedScrollView上设置scrollListener而是使用

 yourRecyclerView.setNestedScrollingEnabled(false);

并在您的recyclerView

上使用scrollListener
 yourRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            // Put your code here..
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

        }
    });

注意: -

请记住通过

禁用recyclerView的嵌套滚动行为

yourRecyclerView.setNestedScrollingEnabled(false);

修改

您也可以在onBindViewHolder的{​​{1}}中尝试

recyclerViewAdapter