mau*_*iya 21 android android-recyclerview
我想设置RecylerView的Max Height.我可以使用下面的代码设置最大高度.下面的代码使高度为当前屏幕的60%.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int a = (displaymetrics.heightPixels * 60) / 100;
recyclerview1.getLayoutParams().height = a;
Run Code Online (Sandbox Code Playgroud)
但现在的问题是,如果它没有项目,那么它的高度也是60%.所以我想在没有项目时将其高度设置为0.
我希望实现类似的东西.
if(recyclerview's height > maxHeight)
then set recyclerview's height to maxHeight
else dont change the height of recyclerview.
Run Code Online (Sandbox Code Playgroud)
我怎么设置它?请帮助我,我坚持下去
Fat*_*tum 42
这是你需要的东西.将RecyclerView子类化并覆盖onMeasure,如下所示:
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
heightSpec = MeasureSpec.makeMeasureSpec(Utils.dpsToPixels(240), MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, heightSpec);
}
Run Code Online (Sandbox Code Playgroud)
请确保您提供适当数量的像素作为第一个参数makeMeasureSpec().我个人需要RecyclerView,最多240dps.
小智 21
我认为这至少对我有用
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/Id_const_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/Id_my_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_top_space"
android:scrollbars="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="@+id/Id_const_layout"
app:layout_constraintEnd_toEndOf="@+id/Id_const_layout"
app:layout_constraintHeight_max="150dp"
app:layout_constraintHeight_min="30dp"
app:layout_constraintStart_toStartOf="@+id/Id_const_layout"
app:layout_constraintTop_toTopOf="@+id/Id_const_layout"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
您可以根据需要更改constraintHeight_max和constraintHeight_min。
Fra*_*con 16
我们可以稍微优化@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);
}
}
Run Code Online (Sandbox Code Playgroud)
值/ attrs.xml
<declare-styleable name="MaxHeightScrollView">
<attr name="maxHeight" format="dimension" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
use*_*104 14
ConstraintLayout为其孩子提供最大高度.
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
小智 13
在尝试了太多复杂的建议之后,我终于通过反复试验弄清楚了这一点。
首先,将 RecyclerView 包装在某种布局中。在我的例子中,我使用了约束布局,我什至在 RecyclerView 上方和下方的布局中还有其他元素。通过将布局的高度设置为 0dp,将布局的高度设置为自动调整,然后将默认布局高度约束设置为 wrap 并定义布局最大高度约束。
然后,在您的 RecyclerView 上,将高度设置为 wrap_content,将 layout_constrainedHeight 设置为 true。
它应该是这样的:
<android.support.constraint.ConstraintLayout
android:layout_width="600px"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="450px">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintTop_ToTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
19122 次 |
| 最近记录: |