태그 보관물: android-recyclerview

android-recyclerview

NestedScrollView에서 RecyclerView를 사용하는 방법은 무엇입니까? android:padding=”@dimen/keyline_1″>

RecyclerView내부 사용 방법 NestedScrollView?
RecyclerView어댑터를 설정 한 후 내용이 보이지 않습니다.

업데이트 레이아웃 코드가 업데이트되었습니다.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/keyline_1">

    </RelativeLayout>

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#e5e5e5" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/conversation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

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


답변

recyclerView를 다음과 같이 바꾸십시오.

<android.support.v7.widget.RecyclerView
    android:id="@+id/conversation"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

여기,

app:layout_behavior="@string/appbar_scrolling_view_behavior"

나머지 것들을 관리 할 것입니다.

하나 더, 네 RecycledView를 NestedScrollView 안에 넣을 필요가 없습니다.


답변

업데이트 1

Android 지원 라이브러리 23.2.0부터 setAutoMeasureEnabled(true)LayoutManagers에 대한 메소드가 추가되었습니다 . RecyclerView를 사용하여 내용을 감싸고 매력처럼 작동합니다.
http://android-developers.blogspot.ru/2016/02/android-support-library-232.html

따라서 다음과 같이 추가하십시오.

    LayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setAutoMeasureEnabled(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setNestedScrollingEnabled(false);

업데이트 2

27.1.0 setAutoMeasureEnabled은 더 이상 사용되지 않으므로 재정의 된 메소드를 사용하여 LayoutManager의 사용자 정의 구현을 제공해야합니다.isAutoMeasureEnabled()

그러나 RecyclerView를 많이 사용하는 경우 래핑 모드 에서 사용하지 않는 것이 좋습니다 . 원인이 아닙니다. 여러 항목 유형의 일반 단일 RecyclerView를 사용하여 전체 레이아웃을 리팩토링하십시오. 또는 아래에 마지막 수단으로 설명한 LinearLayout을 사용하는 접근 방식

기존 답변 (권장하지 않음)

RecyclerView내부에서 사용할 수 있습니다 NestedScrollView. 우선 당신이 당신의 자신의 정의를 구현해야합니다 LinearLayoutManager, 당신을 만드는 RecyclerView내용을 포장. 예를 들면 다음과 같습니다.

public class WrappingLinearLayoutManager extends LinearLayoutManager
{

    public WrappingLinearLayoutManager(Context context) {
        super(context);
    }

    private int[] mMeasuredDimension = new int[2];

    @Override
    public boolean canScrollVertically() {
        return false;
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
            int widthSpec, int heightSpec) {
        final int widthMode = View.MeasureSpec.getMode(widthSpec);
        final int heightMode = View.MeasureSpec.getMode(heightSpec);

        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            if (getOrientation() == HORIZONTAL) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);

                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);

                height = height + mMeasuredDimension[1];
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }

        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
            int heightSpec, int[] measuredDimension) {

        View view = recycler.getViewForPosition(position);
        if (view.getVisibility() == View.GONE) {
            measuredDimension[0] = 0;
            measuredDimension[1] = 0;
            return;
        }
        // For adding Item Decor Insets to view
        super.measureChildWithMargins(view, 0, 0);
        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
        int childWidthSpec = ViewGroup.getChildMeasureSpec(
                widthSpec,
                getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view),
                p.width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(
                heightSpec,
                getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view),
                p.height);
        view.measure(childWidthSpec, childHeightSpec);

        // Get decorated measurements
        measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
        measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
        recycler.recycleView(view);
    }
}

그 후에 이것을 사용 LayoutManager하십시오.RecyclerView

recyclerView.setLayoutManager(new WrappingLinearLayoutManager(getContext()));

그러나이 두 가지 방법을 호출해야합니다.

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);

여기에서 setNestedScrollingEnabled(false)스크롤을 비활성화 RecyclerView하여 스크롤 이벤트를 차단하지 않습니다 NestedScrollView. 그리고 setHasFixedSize(false)어댑터 내용의 변화의 크기를 변경할 수 있는지 결정RecyclerView

중요 사항 : 이 솔루션은 경우에 따라 거의 버그가 없으며 성능에 문제가 있으므로 많은 항목이있는 경우 목록 기반의 RecyclerView사용자 정의 LinearLayout기반 구현 을 사용하고 어댑터의 아날로그를 작성하여 작성하는 것이 좋습니다 처럼 행동 ListView또는RecyclerView


답변

1) 지원 라이브러리 23.2.0 이상을 사용해야합니다.

2) RecyclerView높이가됩니다 wrap_content.

삼) recyclerView.setNestedScrollingEnabled(false)

그러나 이렇게하면 재활용 패턴이 작동하지 않습니다 . (즉, 모든 뷰는 한 번에로드 될 때문에 wrap_content전체의 요구에 높이 RecyclerView가 모든 자식 그릴 수 있도록 Views at once. No view will be recycled). Try not to use this pattern unless it is really required. Try to useviewType and add all other views that need to scroll toRecyclerView rather than usingRecyclerView inScrollview`합니다. 성능에 미치는 영향이 매우 높은 것입니다.

간단하게하기 위해 ” LinearLayout모든 자식보기 에서처럼 작동 합니다”


답변

android:fillViewport="true"NestedScrollView측정하는 데 사용할 수 있습니다 RecyclerView. 는 RecyclerView남아있는 높이를 채울 것입니다. 당신이 스크롤 싶다면 NestScrollView, 당신은 설정할 수 있습니다 RecyclerView‘들 minHeight.


답변

단순히 추가 recyclerView.setNestedScrollingEnabled(false);하기 setAdapter만해도 저에게 효과적이었습니다. app:layout_behavior="@string/appbar_scrolling_view_behavior"어디에도 추가 하지 않았으며 맞춤 레이아웃 관리자를 설정하지 않았습니다.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:text="Some Text..."
                android:padding="15dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:padding="15dp"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Quick Links"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:textAllCaps="true"
                android:paddingLeft="20dp"
                android:drawableLeft="@drawable/ic_trending_up_black_24dp"
                android:drawablePadding="10dp"
                android:layout_marginBottom="10dp"
                android:textSize="16sp"/>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#efefef"/>

            <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    </LinearLayout>

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

답변

이것이 나를 위해 일하는 것입니다.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>

답변

확인 할 수있는 간단하고 테스트 코드가 있습니다.

<android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v7.widget.RecyclerView
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
   </android.support.v4.widget.NestedScrollView>