onregildViewDetachedFromWindow在nestedscrollview中的recyclelerview时无法正常工作

时间:2017-09-26 10:14:53

标签: android android-recyclerview android-nestedscrollview

我正在使用LinearLayoutManager和RecyclerView,它位于NestedScrollView中。一切正常,但addOnChildAttachStateChangeListener没有下降

我的fragmnet的xml文件是 ================================================== ===:

<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>

和myfragmnet中的代码是

  @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");
        }
    });

}

上面代码中的方法onChildViewDetachedFromWindow因为nrestedscrollview而未被调用。是否有任何解决方案?没有nestedscrollview它完美的woking,但我需要在我的代码中使用nestedscrollview。

1 个答案:

答案 0 :(得分:0)

onChildViewDetachedFromWindow的调用不简单,是因为当您在RecyclerView中将WRAP_CONTENTNestedScrollView一起使用时,它会一次附加 all 个元素并将其保留<强大。换句话说,元素将从不RecyclerView内的NestedScrollView分离