如何在nestedscrollview中实现recyclerview scrolllistener

问题描述 投票:0回答:1

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

我想将scroll lisntener设置为recyclerview之一。我试过下面的代码,但它不在嵌套的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内滚动监听器

android android-recyclerview android-nestedscrollview
1个回答
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);

        }
    });

注意 :-

请记住禁用recyclelerView的nestedScrolling行为

yourRecyclerView.setNestedScrollingEnabled(false);

编辑

您也可以在onBindViewHolderrecyclerViewAdapter中尝试一下

  onBindViewHolder(ViewHolder holder, int position)
   {
       if(position == yourList.size()-1)
         { 
             loadMoreItems();
             notifyDataSetChanged();
          }
   }
© www.soinside.com 2019 - 2024. All rights reserved.