将 2 列放在所有屏幕的单行水平回收视图中
Put 2 columns in single row horizontal recycleview for all screens
我有一个 parent Horizontal Recycleview
,其中包含十二个 Vertical Recycleviews
。我想每页只显示两列,只在一个水平行上。我使用 GridLayoutManager
。
GridLayoutManager layoutManager = new GridLayoutManager(this,1);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
将跨度增加到 2
它创建了我不想要的两行。
我还尝试将 xml 中的 layout:width
设置为 dp
或 sp
中的固定值,这会导致不同屏幕尺寸上的列数不同。
xml 每个块
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_table_view_game_block_constraint_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_round_top"
android:layout_marginTop="8dp">
<TextView
android:id="@+id/textView_table_view_odds_block_time"
android:layout_width="0dp"
android:layout_height="50sp"
android:gravity="center_vertical"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_table_view_odds_block_hometeam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:textAlignment="textStart"
android:background="@android:color/transparent"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_table_view_odds_block_time" />
<Button
android:id="@+id/button_table_view_odds_block_drawable"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="@android:color/transparent"
android:textAlignment="textStart"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_table_view_odds_block_hometeam" />
<Button
android:id="@+id/button_table_view_game_odds_awayteam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="@android:color/transparent"
android:textAlignment="textStart"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_table_view_odds_block_drawable" />
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
根据屏幕大小动态设置宽度。这是获得完美结果的最佳方式。
像那样更新 Horizontal Recyclerview 的 onBindViewHolder。
@Override
public void onBindViewHolder(@NonNull UsersAdapter.UsersAdapterVh holder, int position) {
UserModel userModel = userModelList.get(position);
String username = userModel.getUserName();
String prefix = userModel.getUserName().substring(0,1);
holder.tvUsername.setText(username);
holder.tvPrefix.setText(prefix);
holder.item_layout.getLayoutParams().width = getScreenWidth(context)/2;
}
public static int getScreenWidth(Context context) {
WindowManager wm= (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
这是给定示例的 RecyclerView 项目布局的样子。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:padding="10dp"
android:id="@+id/holder_view"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="50dp"
android:background="@drawable/users_bg"
android:layout_height="50dp">
<TextView
android:id="@+id/prefix"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/headerColor"
android:text="T"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/headerColor"
android:text="username"
android:layout_marginStart="90dp"
android:layout_centerVertical="true"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView"
android:layout_margin="10dp"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_navigate_next_black_24dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
我有一个 parent Horizontal Recycleview
,其中包含十二个 Vertical Recycleviews
。我想每页只显示两列,只在一个水平行上。我使用 GridLayoutManager
。
GridLayoutManager layoutManager = new GridLayoutManager(this,1);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
将跨度增加到 2
它创建了我不想要的两行。
我还尝试将 xml 中的 layout:width
设置为 dp
或 sp
中的固定值,这会导致不同屏幕尺寸上的列数不同。
xml 每个块
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_table_view_game_block_constraint_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_round_top"
android:layout_marginTop="8dp">
<TextView
android:id="@+id/textView_table_view_odds_block_time"
android:layout_width="0dp"
android:layout_height="50sp"
android:gravity="center_vertical"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_table_view_odds_block_hometeam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:textAlignment="textStart"
android:background="@android:color/transparent"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_table_view_odds_block_time" />
<Button
android:id="@+id/button_table_view_odds_block_drawable"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="@android:color/transparent"
android:textAlignment="textStart"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_table_view_odds_block_hometeam" />
<Button
android:id="@+id/button_table_view_game_odds_awayteam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="@android:color/transparent"
android:textAlignment="textStart"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_table_view_odds_block_drawable" />
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
根据屏幕大小动态设置宽度。这是获得完美结果的最佳方式。
像那样更新 Horizontal Recyclerview 的 onBindViewHolder。
@Override
public void onBindViewHolder(@NonNull UsersAdapter.UsersAdapterVh holder, int position) {
UserModel userModel = userModelList.get(position);
String username = userModel.getUserName();
String prefix = userModel.getUserName().substring(0,1);
holder.tvUsername.setText(username);
holder.tvPrefix.setText(prefix);
holder.item_layout.getLayoutParams().width = getScreenWidth(context)/2;
}
public static int getScreenWidth(Context context) {
WindowManager wm= (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
这是给定示例的 RecyclerView 项目布局的样子。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:padding="10dp"
android:id="@+id/holder_view"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="50dp"
android:background="@drawable/users_bg"
android:layout_height="50dp">
<TextView
android:id="@+id/prefix"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/headerColor"
android:text="T"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/headerColor"
android:text="username"
android:layout_marginStart="90dp"
android:layout_centerVertical="true"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView"
android:layout_margin="10dp"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_navigate_next_black_24dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>