2017-03-09 203 views
4

我想设置RecylerView的最大高度。我可以使用下面的代码设置最大高度。下面的代码使高度为当前屏幕的60%。如何设置RecyclerView的最大高度

 DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int a = (displaymetrics.heightPixels * 60)/100; 
    recyclerview1.getLayoutParams().height = a; 

但现在的问题是,如果它没有项目,那么它的高度是60%。 所以我想设置它的高度0,当它没有项目。

我想达到的东西。

if(recyclerview's height > maxHeight) 
then set recyclerview's height to maxHeight 
    else dont change the height of recyclerview. 

我该如何设置? 请帮助我,我坚持了下来

+0

你有没有找到这方面的解决方案? –

+0

[如何在android中设置包装内容的最大高度?](https://stackoverflow.com/questions/18425758/how-to-set-a-maximum-height-with-wrap-content-in -android) – user1506104

回答

10

这是你需要的东西。子类RecyclerView并覆盖onMeasure如下:

@Override 
protected void onMeasure(int widthSpec, int heightSpec) { 
    heightSpec = MeasureSpec.makeMeasureSpec(Utils.dpsToPixels(240), MeasureSpec.AT_MOST); 
    super.onMeasure(widthSpec, heightSpec); 
} 

确保,你给像素为makeMeasureSpec()第一个参数的适当数量。我个人需要RecyclerView,最多240dps。

+0

这救了我的命,谢谢! –

+0

请问您是如何对RecyclerView进行分类的? – Dusan

+1

@Dusan创建了一个新类,即扩展了RecyclerView。 – Fattum

2

写这个语句中的if else ,让我知道,

ViewGroup.LayoutParams params=recyclerview.getLayoutParams(); 
params.height=100; 
recyclerview.setLayoutParams(params); 
+0

如果你还在谈论什么? – Maulik009

+0

>设置其高度为0时,没有任何项目,你可以确定这个?@ Maulik009,在那个地方。是@Piyush,bhai :) – Radhey

+0

你没有得到正确的问题。 – Maulik009

0

您可以在RecyclerView使用WRAP_CONTENT。它会根据其内容自动衡量您的回收站。

您还可以计算当前高度并设置最大高度。所以Recyclerview将使用wrap_content属性值直到最大高度。

public static void getTotalHeightofRecyclerView(RecyclerView recyclerView) { 

     RecyclerView.Adapter mAdapter = recyclerView.getAdapter(); 

     int totalHeight = 0; 

     for (int i = 0; i < mAdapter.getItemCount(); i++) { 
      View mView = recyclerView.findViewHolderForAdapterPosition(i).itemView 

      mView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 

      totalHeight += mView.getMeasuredHeight(); 
     } 

     if (totalHeight > 100) { 
      ViewGroup.LayoutParams params = recyclerView.getLayoutParams(); 
      params.height = 100; 
      recyclerView.setLayoutParams(params); 
     } 
    } 
+0

我一直在布局中保留了wrap_content。但是我想设置它的最大高度,以便它只能在当前屏幕上滚动。因为我有下面的按钮,我总是想显示。 – Maulik009

+0

我在运行时通过计算更新了答案。 – mertsimsek

+0

引起:显示java.lang.NullPointerException:尝试从字段“android.view.View android.support.v7.widget.RecyclerView $ ViewHolder.itemView”上读取空对象引用上线 ERR 查看MVIEW = recyclerView。findViewHolderForAdapterPosition(i).itemView – Maulik009

1

我们可以优化@Fattum的方法。 我们可以使用app:maxHeight来设置maxHeight。你可以使用dp或px。

public class MaxHeightRecyclerView extends RecyclerView { 
    private int mMaxHeight; 

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

    public MaxHeightRecyclerView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initialize(context, attrs); 
    } 

    public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     initialize(context, attrs); 
    } 

    private void initialize(Context context, AttributeSet attrs) { 
     TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView); 
     mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightScrollView_maxHeight, mMaxHeight); 
     arr.recycle(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     if (mMaxHeight > 0) { 
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST); 
     } 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

值/ attrs.xml

<declare-styleable name="MaxHeightScrollView"> 
    <attr name="maxHeight" /> 
</declare-styleable> 
+0

其不工作 –

+0

@VikashParajuli你能分享你的代码吗 –

0

这是一个有趣的方式,在我的情况下工作。我裹着我的RecyclerView两个视图之间配重等于1,则封闭整个事情的LinearLayout中,像这样:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <android.support.v7.widget.RecyclerView 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
</LinearLayout> 

不要忘记使用android:layout_height="wrap_content"为您RecyclerView。

相关问题