Android RecyclerView未在RelativeLayout和ScrollView中展开

Android RecyclerView未在RelativeLayout和ScrollView中展开,android,android-layout,Android,Android Layout,我为android创建了一个移动应用程序,在该页面上显示回收者视图和幻灯片。我在recyclerview之前抛出scroll视图,但它需要relativelayout,因为scroll视图中有多个组件 这是我的源代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="

我为android创建了一个移动应用程序,在该页面上显示回收者视图和幻灯片。我在recyclerview之前抛出scroll视图,但它需要relativelayout,因为scroll视图中有多个组件

这是我的源代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="#eeeeee"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
    tools:context=".MainActivity">



<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent">
        <com.daimajia.slider.library.SliderLayout
            android:id="@+id/slider"
            android:layout_width="match_parent"
            custom:pager_animation="Accordion"
            custom:auto_cycle="true"
            custom:indicator_visibility="visible"
            custom:pager_animation_span="1100"

            android:layout_height="200dp"/>


    <com.daimajia.slider.library.Indicators.PagerIndicator
                android:id="@+id/custom_indicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                custom:selected_color="#0095BF"
                custom:unselected_color="#55333333"
                custom:selected_drawable="@drawable/banner1"
                custom:shape="oval"
                custom:selected_padding_left="5dp"
                custom:selected_padding_right="5dp"
                custom:unselected_padding_left="5dp"
                custom:unselected_padding_right="5dp"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                custom:selected_width="6dp"
                custom:selected_height="6dp"
                custom:unselected_width="6dp"
                custom:unselected_height="6dp"
                android:layout_marginBottom="20dp"
                />

    <com.daimajia.slider.library.Indicators.PagerIndicator
                android:id="@+id/custom_indicator2"
                style="@style/AndroidImageSlider_Corner_Oval_Orange"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="20dp"
                />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Produk Terbaru"

        android:id="@+id/judul"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp"
        android:layout_below="@id/slider"/>



    <view
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/judul"
        class="android.support.v7.widget.RecyclerView"
        android:id="@+id/recycler_view" />


    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    </RelativeLayout>
</ScrollView>


</RelativeLayout>


但是,在添加RelativeLayout后,RecyclerView没有展开

只需使用此Layoutmanager:

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.apkfuns.logutils.LogUtils;

public class MainActivity{

   onCreate(){
      recyclerview.setLayoutManager(new FullyLinearLayoutManager(MainActivity.this))
   }
   public class FullyLinearLayoutManager extends LinearLayoutManager {

    private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();
    private MeasureEndListener mMeasureEndListener;

    public FullyLinearLayoutManager(Context context) {
        super(context);
    }
    private float divHeight =0;
    public FullyLinearLayoutManager(Context context,float height) {
        super(context);
        divHeight = height;
    }

    public FullyLinearLayoutManager(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);

        Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
                + " \nheightMode " + heightSpec
                + " \nwidthSize " + widthSize
                + " \nheightSize " + heightSize
                + " \ngetItemCount() " + getItemCount());

        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 {
              //  LogUtils.e(mMeasuredDimension[1]);
                height = height + mMeasuredDimension[1];
                if(i!= getItemCount()-1){
                    height += divHeight;
//                    LogUtils.e(divHeight + "xxx add"+DensityUtils.dp2px(divHeight));
                }else{
                    height += 2*divHeight;
//                    LogUtils.e(divHeight+ "xxx no add"+ DensityUtils.dp2px(divHeight));
                }
                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:
        }
        if(mMeasureEndListener!=null){
            mMeasureEndListener.onMeasureEnd(width,height);
//            new Thread().interrupt();
        }
        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        try {
            View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException

            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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    public interface MeasureEndListener{
        void onMeasureEnd(int width,int height);
    }

    public void setMeasureEndListener(MeasureEndListener mMeasureEndListener){
        this.mMeasureEndListener = mMeasureEndListener;
    }
}
}
导入android.content.Context;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.view.view;
导入android.view.ViewGroup;
导入com.apkfuns.logutils.logutils;
公开课活动{
onCreate(){
setLayoutManager(新的FullyLinearLayoutManager(MainActivity.this))
}
公共类FullyLinearLayoutManager扩展了LinearLayoutManager{
私有静态最终字符串标记=FullyLinearLayoutManager.class.getSimpleName();
私有度量列表器;
公共FullyLinearLayoutManager(上下文){
超级(上下文);
}
专用浮点数高度=0;
公共FullyLinearLayoutManager(上下文、浮动高度){
超级(上下文);
高度=高度;
}
公共FullyLinearLayoutManager(上下文上下文、int方向、布尔反转){
超级(上下文、方向、反转);
}
私有int[]mMeasuredDimension=新int[2];
@凌驾
测量时的公共空隙(RecyclerView.Recycler Recycler,RecyclerView.State,
整数宽度规格、整数高度规格){
最终int-widthMode=View.MeasureSpec.getMode(widthmspec);
最终int heightMode=View.MeasureSpec.getMode(heightSpec);
最终int-widthSize=View.MeasureSpec.getSize(widthSpec);
最终int heightSize=View.MeasureSpec.getSize(heightSpec);
Log.i(标记“onMeasure called.\nwidthMode”+widthMode
+“\n高度模式”+高度规格
+“\n宽度大小”+宽度大小
+“\n高度大小”+高度大小
+“\ngetItemCount()”+getItemCount());
整数宽度=0;
整数高度=0;
对于(int i=0;i
chang recyclerview的Layoutmanager。如何在mainactivity.java上实现它?只需将其放入mainactivity中,并像recyclerview.setLayoutManager(新的FullyLinearLayoutManager())一样使用它;我得到了如下错误:12-09 01:00:46.028 1021-1021/id.malaka.app.compro E/FullyLinearLayoutManager.onMeasure(FullyLinearLayoutManager.java:63):192