【问题标题】:how to implement recyclerview scrolllistener inside a nestedscrollview如何在nestedscrollview中实现recyclerview滚动监听器
【发布时间】:2018-03-03 11:23:00
【问题描述】:

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

我想将滚动监听器设置为回收器视图之一。我已经尝试过使用下面的代码,但它在嵌套的滚动视图中不起作用。

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 解决方案但没有奏效,有没有办法在嵌套的滚动视图中做滚动监听器

【问题讨论】:

    标签: android android-recyclerview android-nestedscrollview


    【解决方案1】:

    无需在 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的nestedScrolling行为

    yourRecyclerView.setNestedScrollingEnabled(false);

    编辑

    你也可以在你的recyclerViewAdapteronBindViewHolder 中试试看

      onBindViewHolder(ViewHolder holder, int position)
       {
           if(position == yourList.size()-1)
             { 
                 loadMoreItems();
                 notifyDataSetChanged();
              }
       }
    

    【讨论】:

    • 我一开始试过这个,但当时没有调用滚动监听器
    • 不可能先@AAA setNestedScrollingEnabled(false) 然后你必须设置滚动监听器..
    • 我已经从 xml 设置了 setNestedScrollingEnabled(false)。
    • 以编程方式尝试一次..@AAA 并更新..因为..我用它解决了这个问题..
    • 我确实记录了。进入活动时调用一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2015-05-15
    相关资源
    最近更新 更多