Android 如何设置RecyclerView的最大高度?
我必须Android 如何设置RecyclerView的最大高度?,android,android-layout,android-studio,android-relativelayout,Android,Android Layout,Android Studio,Android Relativelayout,我必须实现aRecylerView,如果其仅包含项内容的高度小于此高度,则应包装内容。如果项目的增加量像250dp,则应将其设置为maxheght(250dp),并可滚动。如何实现此目的。我的父布局是一个相对布局 这是我的布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" x
实现
aRecylerView
,如果其仅包含项内容的高度小于此高度,则应包装内容。如果项目的增加量像250dp
,则应将其设置为maxheght(250dp)
,并可滚动。如何实现此目的。我的父布局是一个相对布局
这是我的布局文件
<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>
您可以通过编程将高度设置为RecylerView
如果只有一项<代码>包装内容
ViewGroup.LayoutParams params=recycler_view.getLayoutParams();
params.height= RecyclerView.LayoutParams.WRAP_CONTENT;
recycler_view.setLayoutParams(params);
否则
250dp
浮动高度=getResources().getDimension(R.dimen.view\U高度)//获得高度
ViewGroup.LayoutParams params_new=recycler_view.getLayoutParams();
新参数。高度=高度;
回收器视图。setLayoutParams(新参数);
我有一个答案:
只需创建一个扩展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转换为像素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)
);
使用
ScrollView
作为父布局。@AlphaQScrollView
而不是RelativeLayout
或当前RelativeLayout
内部ScrollView
为什么谷歌要删除maxHeight,这真是一件很不方便的事情。
messageBody.getViewTreeObserver()
.addOnGlobalLayoutListener(
new OnViewGlobalLayoutListener(messageBody, 256, context)
);