android-recycleview里面的scrollview [英] android-recycleview inside scrollview

查看:57
本文介绍了android-recycleview里面的scrollview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中,我想在页面上方放置一张slideShow,然后在其后放置recycleview,为此,我在布局中编写了以下代码:

In my activity , I want to put a slideShow above my page and then put recycleview after that, to do so ,I'v written this code in my layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    android:padding="6dp">


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        card_view:cardBackgroundColor="#fff"
        card_view:cardCornerRadius="3dp"
        card_view:cardElevation="0dp">

        <cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="250dp" />
    </android.support.v7.widget.CardView>

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

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

此代码的问题在于,当我运行该活动时,只有recycleview滚动,我不希望那样,如果我向下滚动,整个页面不仅向下滚动,还向下滚动.

the problem with this code is that when I run the activity , only the recycleview scrolls , I don't want that, I want if I scroll down ,the whole page scrolls down not just the recycle .

如何使整个页面滚动?

How can I make the whole page scroll ?

推荐答案

执行此操作的三个步骤:

Three steps to do this:

  1. 使用NestedScrollView替换滚动视图,并将其布局行为设置为app:layout_behavior="@string/appbar_scrolling_view_behavior"
  2. 将所有小部件保持在线性/相对布局下(因为嵌套滚动视图仅接受一个孩子) 3.使用以下代码创建CustomLinearLayoutManager.java:

  1. Replace your scrollview with NestedScrollView with layout behaviour set to app:layout_behavior="@string/appbar_scrolling_view_behavior"
  2. Keep all your widgets under a linear/relative layout(because nested scroll view accepts only one child) 3.create CustomLinearLayoutManager.java with this code:

public class CustomLinearLayoutManager extends LinearLayoutManager {

public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout)    {
    super(context, orientation, reverseLayout);
}

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

@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++) {
        measureScrapChild(recycler, i,
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                mMeasuredDimension);

        if (getOrientation() == HORIZONTAL) {
            width = width + mMeasuredDimension[0];
            if (i == 0) {
                height = mMeasuredDimension[1];
            }
        } else {
            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 != null) {
        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                getPaddingLeft() + getPaddingRight(), p.width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                getPaddingTop() + getPaddingBottom(), p.height);
        view.measure(childWidthSpec, childHeightSpec);
        measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
        measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
        recycler.recycleView(view);
    }
}

}

像这样将适配器设置到您的recyclerview:

set adapter to your recyclerview like this:

CustomLinearLayoutManager customLinearLayoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        updatesRecyclerView.setHasFixedSize(true);
        updatesRecyclerView.setLayoutManager(customLinearLayoutManager);
        updatesRecyclerView.setAdapter(yourAdapter);
        updatesRecyclerView.setNestedScrollingEnabled(false);

就是这样,它可以正常工作.

That's it , it works.

这篇关于android-recycleview里面的scrollview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆