调整和定位 RecyclerView 项目
Resizing and positioning RecyclerView items
我正在使用 RecyclerView 在网格布局中显示项目。
下面是模拟器的图像和我需要实现的期望结果。
左侧(执行)图像是我模拟器上的输出,而右侧(期望)是我想要实现的输出。
Activity布局XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3c3c3">
<include
android:id="@+id/toolBarIncluded"
layout="@layout/toolbar_new_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolBarIncluded"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:id="@+id/recyclerAdView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="0.65"
android:background="#009688" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/recyclerAdView"
android:layout_gravity="center"
android:layout_weight="0.35"
android:background="#00000000"
android:gravity="center">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="#00000000"
android:foregroundGravity="center" />
</RelativeLayout>
</LinearLayout>
列表项XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llGridItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="6dp"
android:background="@drawable/drawable_grid_item_background"
android:gravity="center"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:orientation="vertical">
<ImageView
android:id="@+id/ivItemIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/ic_new_receipt_wrong" />
<TextView
android:layout_marginTop="6dp"
android:id="@+id/tvItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:text="My Complaints"
android:textSize="14sp" />
</LinearLayout>
Activity 代码段
gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(demoDashAdapter);
适配器代码片段
int viewHeight ,viewWidth ;
public onCreateViewHolder(){
viewHeight = viewGroup.getMeasuredHeight() / 4;
viewWidth = viewGroup.getMeasuredWidth() / 3;
...
}
public onBindViewHolder(){
viewHolder.itemLayout.setMinimumWidth(viewWidth);
viewHolder.itemLayout.setMinimumHeight(viewHeight);
...
}
因此我的问题是,如何动态调整 recyclerView 中项目的大小。请注意,我使用的布局权重为 0.65 和 0.35 而不是 weightSum 1;
请指导我。
您可以尝试在 onCreateViewHolder
中为此使用 LayoutParams,如下所示:
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View rowLayout = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
ViewGroup.LayoutParams layoutParams = rowLayout.getLayoutParams();
layoutParams.height = (int) (parent.getHeight() * 0.15); // control the recyclerView row height from here
rowLayout.setLayoutParams(layoutParams);
return new Holder(rowLayout);
}
为什么 viewHeight = viewGroup.getMeasuredHeight() / 4
?
如果要显示 3 行,只需除以 3
。
还要考虑 item.xml
中的边距
因此获取一行中每个项目的高度应该类似于:
(viewGroup.getMeasuredHeight()/3) - (marginTop + marginBot)
不要使用 RecyclerView.Adapter
来改变视图的布局方式。这是 RecyclerView.LayoutManager
的责任。
您可以覆盖方法验证 View.LayoutParams
以均匀分配大小:
//customized GridLayoutManager
gridLayoutManager = new GridLayoutManager(MainActivity.this, 3){
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
// force size of viewHolder here, this will override layout_height and layout_width from xml
lp.height = getHeight() / getSpanCount();
lp.width = getWidth() / getSpanCount();
return true;
}
};
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(gridLayoutManager);
我正在使用 RecyclerView 在网格布局中显示项目。 下面是模拟器的图像和我需要实现的期望结果。 左侧(执行)图像是我模拟器上的输出,而右侧(期望)是我想要实现的输出。
Activity布局XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3c3c3">
<include
android:id="@+id/toolBarIncluded"
layout="@layout/toolbar_new_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolBarIncluded"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:id="@+id/recyclerAdView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="0.65"
android:background="#009688" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/recyclerAdView"
android:layout_gravity="center"
android:layout_weight="0.35"
android:background="#00000000"
android:gravity="center">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="#00000000"
android:foregroundGravity="center" />
</RelativeLayout>
</LinearLayout>
列表项XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llGridItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="6dp"
android:background="@drawable/drawable_grid_item_background"
android:gravity="center"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:orientation="vertical">
<ImageView
android:id="@+id/ivItemIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/ic_new_receipt_wrong" />
<TextView
android:layout_marginTop="6dp"
android:id="@+id/tvItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:text="My Complaints"
android:textSize="14sp" />
</LinearLayout>
Activity 代码段
gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(demoDashAdapter);
适配器代码片段
int viewHeight ,viewWidth ;
public onCreateViewHolder(){
viewHeight = viewGroup.getMeasuredHeight() / 4;
viewWidth = viewGroup.getMeasuredWidth() / 3;
...
}
public onBindViewHolder(){
viewHolder.itemLayout.setMinimumWidth(viewWidth);
viewHolder.itemLayout.setMinimumHeight(viewHeight);
...
}
请指导我。
您可以尝试在 onCreateViewHolder
中为此使用 LayoutParams,如下所示:
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View rowLayout = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
ViewGroup.LayoutParams layoutParams = rowLayout.getLayoutParams();
layoutParams.height = (int) (parent.getHeight() * 0.15); // control the recyclerView row height from here
rowLayout.setLayoutParams(layoutParams);
return new Holder(rowLayout);
}
为什么 viewHeight = viewGroup.getMeasuredHeight() / 4
?
如果要显示 3 行,只需除以 3
。
还要考虑 item.xml
因此获取一行中每个项目的高度应该类似于:
(viewGroup.getMeasuredHeight()/3) - (marginTop + marginBot)
不要使用 RecyclerView.Adapter
来改变视图的布局方式。这是 RecyclerView.LayoutManager
的责任。
您可以覆盖方法验证 View.LayoutParams
以均匀分配大小:
//customized GridLayoutManager
gridLayoutManager = new GridLayoutManager(MainActivity.this, 3){
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
// force size of viewHolder here, this will override layout_height and layout_width from xml
lp.height = getHeight() / getSpanCount();
lp.width = getWidth() / getSpanCount();
return true;
}
};
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(gridLayoutManager);