RecyclerView未扩展到RelativeLayout和ScrollView内部

时间:2015-12-08 13:45:40

标签: android android-layout

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

这是我的源代码:

<?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没有扩展。

1 个答案:

答案 0 :(得分:1)

只需使用此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;
    }
}
}