Bhoomi Zalavadiya
Bhoomi Zalavadiya

Reputation: 689

onChildViewDetachedFromWindow not working when recyclerview inside nestedscrollview

I am using LinearLayoutManager with RecyclerView which is inside NestedScrollView. Everything is working fine but addOnChildAttachStateChangeListener not wroking

My xml file of fragmnet is =====================================================:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/parent_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#dedcdc"
          android:orientation="vertical">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/layout_rel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/app_bg"
                android:paddingBottom="@dimen/margin_16dp"
                android:paddingLeft="@dimen/margin_10dp"
                android:paddingRight="@dimen/margin_10dp"
                android:paddingTop="@dimen/margin_16dp">

                <ImageView
                    android:id="@+id/image_user"
                    android:layout_width="@dimen/post_profile_dp_size"
                    android:layout_height="@dimen/post_profile_dp_size"
                    android:layout_centerVertical="true"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    tools:src="@drawable/default_user" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_margin="@dimen/margin_5dp"
                    android:layout_toLeftOf="@+id/button_add_post"
                    android:layout_toRightOf="@+id/image_user"
                    android:gravity="center_vertical"
                    android:text="Say something" />


            </RelativeLayout>

            <ProgressBar
                android:id="@+id/progress_bar_horizontal"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="10dip"
                android:scaleY="2"
                android:layout_below="@+id/layout_rel"
                android:layout_centerVertical="true"
                android:indeterminate="true" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_below="@+id/progress_bar_horizontal"/>


        </RelativeLayout>

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

</android.support.v4.widget.SwipeRefreshLayout>

and code in myfragmnet is

  @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    mNestedScrollView = (NestedScrollView) view.findViewById(R.id.nested_scrollview);
    mNestedScrollView.setNestedScrollingEnabled(false);

    mHorizontalProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar_horizontal);
    mHorizontalProgressBar.setVisibility(View.GONE);

    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    mSwipeRefreshLayout.setRefreshing(false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

    mRecyclerView.setNestedScrollingEnabled(false);
    mLinearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);

    mAdapter = new DataListAdapter(this, getContext(), arrayList);
    mRecyclerView.setAdapter(mAdapter);

    endlessNestedScrollListener = new EndlessNestedScrollListener(mLinearLayoutManager) {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
          LoadMoreData();

        }
    };

    mNestedScrollView.setOnScrollChangeListener(endlessNestedScrollListener);
    mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {

        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {
            Log.d("tag","on detach");
        }
    });

}

Method onChildViewDetachedFromWindow in above code is not called because of nrestedscrollview.Is there any solution for it? Without nestedscrollview its woking perfectly, but i need to use nestedscrollview in my code.

Upvotes: 0

Views: 554

Answers (1)

Andrey Danilov
Andrey Danilov

Reputation: 6592

onChildViewDetachedFromWindow is not called simply because when you use RecyclerView with WRAP_CONTENT inside NestedScrollView it attaches all elements at once and keeps them all the time. In other words elements will never be detached from RecyclerView inside NestedScrollView

Upvotes: 1

Related Questions