mpo*_*tus 3 android gridlayoutmanager android-recyclerview
我正在尝试使用RecyclerView和GridLayoutManager来显示项目列表。
这是我要满足的条件:
spanCount我发现如果android:layout_width="match_parent"在项目视图上进行设置,则项目宽度将通过用RecyclerView的宽度除以来设置spanCount。
问题是我不知道使项目高度与布局管理器确定的宽度成正比。
我唯一的解决方案是放弃布局管理器进行度量,并在中明确设置项目尺寸onBindViewHolder:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
ViewGroup.LayoutParams p = v.getLayoutParams();
p.width = parent.getWidth() / mLayoutManager.getSpanCount();
p.height = p.width * 3/2;
v.setLayoutParams(p);
return new MyViewHolder(v);
}
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何能帮助我改善解决方案的指针。
如何ConstraintLayout在item_layoutXML中使用A。约束布局使您可以将高度设置为宽度的比率。
例如:
<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="wrap_content">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
因此,当您使用时,app:layout_constraintDimensionRatio="3:2"
您要为3高度设置2高度的比例。
无论跨度计数如何都应该工作
| 归档时间: |
|
| 查看次数: |
2312 次 |
| 最近记录: |