在 GridLayout 中动态添加相同大小的视图
Adding equally sized views dynamically in GridLayout
我需要显示一个 4x10 的随机颜色网格。网格应水平和垂直填满屏幕,所有单元格的尺寸应相等。由于网格 (4x10) 尺寸在未来可能会发生变化,因此 GridLayout 对我来说比 TableLayout 更有意义。 GridView 和 RecyclerView 已出局,因为我不需要任何滚动行为。
因为我需要在运行时添加这些子视图,所以我开始计算单元格宽度和高度作为屏幕宽度和高度的比率。然后我偶然发现 this SO post which says GridLayout has better ways to achieve such behavior. There is a code example for static (XML) based views, but I am not able to find a Java/Kotlin example. I am experimenting with GridLayout.Spec 在子视图的 LayoutParams 中使用,但无法弄清楚它是如何工作的。
更新了所需布局的屏幕截图。此图像为 12x10,但我希望可以灵活地更改尺寸(编译时)。
If you want to generate grid layout col and row dynamically you can use grid layout with recycler view.
1. xml code
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</android.support.v4.widget.SwipeRefreshLayout>
2 create a model classs as per your requriement
public class AllAssignedTableModel {
@SerializedName("table_id")
private int table_id;
@SerializedName("table_name")
private String table_name;
@SerializedName("restaurant_id")
private int restaurant_id;
@SerializedName("waiter_id")
private int waiter_id;
@SerializedName("image")
private String image;
@SerializedName("table_status")
private int table_status;
@SerializedName("table_bill")
private double table_bill;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public int getTable_id() {
return table_id;
}
public void setTable_id(int table_id) {
this.table_id = table_id;
}
public String getTable_name() {
return table_name;
}
public void setTable_name(String table_name) {
this.table_name = table_name;
}
public int getRestaurant_id() {
return restaurant_id;
}
public void setRestaurant_id(int restaurant_id) {
this.restaurant_id = restaurant_id;
}
public int getWaiter_id() {
return waiter_id;
}
public void setWaiter_id(int waiter_id) {
this.waiter_id = waiter_id;
}
public int getTable_status() {
return table_status;
}
public void setTable_status(int table_status) {
this.table_status = table_status;
}
public double getTable_bill() {
return table_bill;
}
public void setTable_bill(double table_bill) {
this.table_bill = table_bill;
}
}
3. create adapter of recycler view as per your requirement.
package talent4assure.com.manageyourrestaurant.adapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import talent4assure.com.manageyourrestaurant.R;
import talent4assure.com.manageyourrestaurant.model.AllAssignedTableModel;
import static android.content.Context.MODE_PRIVATE;
public class WaAssignedTableListAdapter extends RecyclerView.Adapter<WaAssignedTableListAdapter.EmployeeViewHolder> {
private Context context;
private List<AllAssignedTableModel> dataList;
// public ImageView img;
public static final String MY_BILL_PREFS = "MyBillPrefsFile";
String tableId,billAmount;
HashMap<String, String> billMap=new HashMap<String, String>();
public WaAssignedTableListAdapter(List<AllAssignedTableModel> dataList, Context context) {
this.dataList = dataList;
this.context=context;
}
@Override
public EmployeeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.waiter_cards_layout, parent, false);
return new EmployeeViewHolder(view);
}
@Override
public void onBindViewHolder(EmployeeViewHolder holder, int position) {
holder.tvAssignTableName.setText(dataList.get(position).getTable_name());
holder.tvCustomerBill.setText(String.valueOf(dataList.get(position).getTable_bill()));
int status = dataList.get(position).getTable_status();
if(status == 1){
holder.llBackground.setBackgroundResource(R.drawable.llred_background);
}else {
holder.llBackground.setBackgroundResource(R.drawable.llblue_background);
}
}
@Override
public int getItemCount() {
return dataList.size();
}
class EmployeeViewHolder extends RecyclerView.ViewHolder {
TextView tvAssignTableName,tvCustomerBill;
LinearLayout llBackground;
EmployeeViewHolder(View itemView) {
super(itemView);
tvAssignTableName = (TextView) itemView.findViewById(R.id.tvAssignTableName);
tvCustomerBill = (TextView)itemView.findViewById(R.id.tvCustomerBill);
llBackground = (LinearLayout)itemView.findViewById(R.id.llBackground);
}
}
}
4. java class activity code
no of column u can pass statically or you can pass val dynamically as per your requirement(in this example i am taking it as 4). and no of row will get created as per your data dynamically. hope it will help you.
private WaAssignedTableListAdapter adapter;
private RecyclerView recyclerView;
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new WaAssignedTableListAdapter(allAssignedTableModels, WaiterActivity.this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 4, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
与 LinearLayout 类似,GridLayout 规范中有一个权重属性 - 您可以为所有视图将其设置为 1。类似于 LinearLayout,使用权重使宽度和高度为 0。使用 Undefined 作为位置,因为你想按顺序绘制视图,否则为每个单元格传递相应的位置。
gridLayout.rowCount = rowCount
gridLayout.columnCount = colCount
for (i in 1..rowCount*colCount){
val layoutParams: GridLayout.LayoutParams = GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, 1f),
GridLayout.spec(GridLayout.UNDEFINED, 1f)).apply {
width = 0
height = 0
}
val blueView = View(this).apply {
setBackgroundColor(Color.BLUE)
}
gridLayout.addView(blueView, layoutParams)
}
我需要显示一个 4x10 的随机颜色网格。网格应水平和垂直填满屏幕,所有单元格的尺寸应相等。由于网格 (4x10) 尺寸在未来可能会发生变化,因此 GridLayout 对我来说比 TableLayout 更有意义。 GridView 和 RecyclerView 已出局,因为我不需要任何滚动行为。
因为我需要在运行时添加这些子视图,所以我开始计算单元格宽度和高度作为屏幕宽度和高度的比率。然后我偶然发现 this SO post which says GridLayout has better ways to achieve such behavior. There is a code example for static (XML) based views, but I am not able to find a Java/Kotlin example. I am experimenting with GridLayout.Spec 在子视图的 LayoutParams 中使用,但无法弄清楚它是如何工作的。
更新了所需布局的屏幕截图。此图像为 12x10,但我希望可以灵活地更改尺寸(编译时)。
If you want to generate grid layout col and row dynamically you can use grid layout with recycler view.
1. xml code
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</android.support.v4.widget.SwipeRefreshLayout>
2 create a model classs as per your requriement
public class AllAssignedTableModel {
@SerializedName("table_id")
private int table_id;
@SerializedName("table_name")
private String table_name;
@SerializedName("restaurant_id")
private int restaurant_id;
@SerializedName("waiter_id")
private int waiter_id;
@SerializedName("image")
private String image;
@SerializedName("table_status")
private int table_status;
@SerializedName("table_bill")
private double table_bill;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public int getTable_id() {
return table_id;
}
public void setTable_id(int table_id) {
this.table_id = table_id;
}
public String getTable_name() {
return table_name;
}
public void setTable_name(String table_name) {
this.table_name = table_name;
}
public int getRestaurant_id() {
return restaurant_id;
}
public void setRestaurant_id(int restaurant_id) {
this.restaurant_id = restaurant_id;
}
public int getWaiter_id() {
return waiter_id;
}
public void setWaiter_id(int waiter_id) {
this.waiter_id = waiter_id;
}
public int getTable_status() {
return table_status;
}
public void setTable_status(int table_status) {
this.table_status = table_status;
}
public double getTable_bill() {
return table_bill;
}
public void setTable_bill(double table_bill) {
this.table_bill = table_bill;
}
}
3. create adapter of recycler view as per your requirement.
package talent4assure.com.manageyourrestaurant.adapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import talent4assure.com.manageyourrestaurant.R;
import talent4assure.com.manageyourrestaurant.model.AllAssignedTableModel;
import static android.content.Context.MODE_PRIVATE;
public class WaAssignedTableListAdapter extends RecyclerView.Adapter<WaAssignedTableListAdapter.EmployeeViewHolder> {
private Context context;
private List<AllAssignedTableModel> dataList;
// public ImageView img;
public static final String MY_BILL_PREFS = "MyBillPrefsFile";
String tableId,billAmount;
HashMap<String, String> billMap=new HashMap<String, String>();
public WaAssignedTableListAdapter(List<AllAssignedTableModel> dataList, Context context) {
this.dataList = dataList;
this.context=context;
}
@Override
public EmployeeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.waiter_cards_layout, parent, false);
return new EmployeeViewHolder(view);
}
@Override
public void onBindViewHolder(EmployeeViewHolder holder, int position) {
holder.tvAssignTableName.setText(dataList.get(position).getTable_name());
holder.tvCustomerBill.setText(String.valueOf(dataList.get(position).getTable_bill()));
int status = dataList.get(position).getTable_status();
if(status == 1){
holder.llBackground.setBackgroundResource(R.drawable.llred_background);
}else {
holder.llBackground.setBackgroundResource(R.drawable.llblue_background);
}
}
@Override
public int getItemCount() {
return dataList.size();
}
class EmployeeViewHolder extends RecyclerView.ViewHolder {
TextView tvAssignTableName,tvCustomerBill;
LinearLayout llBackground;
EmployeeViewHolder(View itemView) {
super(itemView);
tvAssignTableName = (TextView) itemView.findViewById(R.id.tvAssignTableName);
tvCustomerBill = (TextView)itemView.findViewById(R.id.tvCustomerBill);
llBackground = (LinearLayout)itemView.findViewById(R.id.llBackground);
}
}
}
4. java class activity code
no of column u can pass statically or you can pass val dynamically as per your requirement(in this example i am taking it as 4). and no of row will get created as per your data dynamically. hope it will help you.
private WaAssignedTableListAdapter adapter;
private RecyclerView recyclerView;
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new WaAssignedTableListAdapter(allAssignedTableModels, WaiterActivity.this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 4, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
与 LinearLayout 类似,GridLayout 规范中有一个权重属性 - 您可以为所有视图将其设置为 1。类似于 LinearLayout,使用权重使宽度和高度为 0。使用 Undefined 作为位置,因为你想按顺序绘制视图,否则为每个单元格传递相应的位置。
gridLayout.rowCount = rowCount
gridLayout.columnCount = colCount
for (i in 1..rowCount*colCount){
val layoutParams: GridLayout.LayoutParams = GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, 1f),
GridLayout.spec(GridLayout.UNDEFINED, 1f)).apply {
width = 0
height = 0
}
val blueView = View(this).apply {
setBackgroundColor(Color.BLUE)
}
gridLayout.addView(blueView, layoutParams)
}