scrollview 里面的 android-recycleview

标签 android android-recyclerview android-scrollview

在我的 Activity 中,我想在我的页面上方放一个 slideShow,然后放 recycleview,为此,我在我的布局中编写了这段代码:

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

此代码的问题是,当我运行 Activity 时,只有回收 View 滚动,我不希望那样,我希望如果我向下滚动,整个页面都向下滚动,而不仅仅是回收。

如何让整个页面滚动?

最佳答案

三个步骤:

  1. 将您的 ScrollView 替换为布局行为设置为 app:layout_behavior="@string/appbar_scrolling_view_behavior"
  2. 的 NestedScrollView
  3. 将所有小部件保持在线性/相对布局下(因为嵌套 ScrollView 只接受一个 subview ) 3.使用此代码创建 CustomLinearLayoutManager.java:

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

像这样将适配器设置为您的回收 View :

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

就是这样,它有效。

关于scrollview 里面的 android-recycleview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35746113/

相关文章:

android - 警告您的 Apk 正在使用需要隐私政策 : (android. 权限的权限。READ_PHONE_STATE)

java - Android初学者图形布局不起作用

java - 如何在RecyclerView中创建异构布局?

android - 轻推分页回收 View 以在不切换的情况下提示下一页

android - ScrollView 内的 ListView 仅显示一个列表项?如何显示所有列表项

java - 我可以从另一个应用程序中调用平台 channel 吗?

java - 在 Android 中使用 SAX 解析 XML

android - RecyclerView + CardView + 触控反馈

带有页眉、 ScrollView 和页脚的 android 布局,页脚带有键盘

android - 如何在 Android 中动态查找 scrollview 滚动位置?