【问题标题】:How to set the max height of a RecyclerView?如何设置 RecyclerView 的最大高度?
【发布时间】:2017-05-22 03:20:16
【问题描述】:

我必须implementing 一个RecylerView,如果它只包含项目内容高度较小,它应该包装内容。如果项目的增加像250dp,它应该设置为最大heght(250dp)并且能够滚动。如何实现这个。我的父布局是Relative layout

这是我的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom" />

</RelativeLayout>

如果只有一项,则为屏幕截图。这应该是 wrap_content。

【问题讨论】:

  • 使用ScrollView作为父布局。
  • @AlphaQ ScrollView 而不是RelativeLayout 或当前Relative layoutScrollView
  • 谷歌为什么要删除maxHeight,这样做真的很不方便。

标签: android android-layout android-studio android-relativelayout


【解决方案1】:

我在这里有一个答案:

https://stackoverflow.com/a/29178364/1148784

只需创建一个扩展 RecyclerView 的新类并覆盖它的 onMeasure 方法。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxHeight > 0){
            int hSize = MeasureSpec.getSize(heightMeasureSpec);
            int hMode = MeasureSpec.getMode(heightMeasureSpec);

            switch (hMode){
                case MeasureSpec.AT_MOST:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.UNSPECIFIED:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.EXACTLY:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY);
                    break;
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

maxHeight 以像素为单位。

你可以用这个:

maxHeight = (int) getContext().getResources().getDisplayMetrics().density * 250;

将 250 dp 转换为像素

【讨论】:

    【解决方案2】:

    1.) 创建一个类来处理将最大高度设置为用户传递的内容:

    public class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
    
    
    private Context context;
    private int maxHeight;
    private View view;
    
    public OnViewGlobalLayoutListener(View view, int maxHeight, Context context) {
        this.context = context;
        this.view = view;
        this.maxHeight = dpToPx(maxHeight);
    }
    
    @Override
    public void onGlobalLayout() {
        if (view.getHeight() > maxHeight) {
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.height = maxHeight;
            view.setLayoutParams(params);
        }
    }
    
    public int pxToDp(int px) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return dp;
    }
    
    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }
    }
    

    2.) 将此附加到视图并在 DP 中传递最大高度:

    messageBody.getViewTreeObserver()
                .addOnGlobalLayoutListener(
                 new OnViewGlobalLayoutListener(messageBody, 256, context)
                 );
    

    【讨论】:

      【解决方案3】:

      您可以通过编程将高度设置为RecylerView

      如果只有一项...wrap content

      ViewGroup.LayoutParams params=recycler_view.getLayoutParams();
      params.height= RecyclerView.LayoutParams.WRAP_CONTENT;
      recycler_view.setLayoutParams(params);
      

      否则

      <dimen name="view_height">250dp</dimen>
      
      float height= getResources().getDimension(R.dimen.view_height); //get height
      
      ViewGroup.LayoutParams params_new=recycler_view.getLayoutParams();
      params_new.height=height;
      recycler_view.setLayoutParams(params_new);
      

      【讨论】:

        猜你喜欢
        • 2017-07-30
        • 1970-01-01
        • 2021-07-27
        • 2021-02-23
        • 2011-04-04
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        • 2020-02-10
        相关资源
        最近更新 更多