【问题标题】:Adding equally sized views dynamically in GridLayout在 GridLayout 中动态添加相同大小的视图
【发布时间】:2018-10-21 09:35:42
【问题描述】:

我需要显示一个 4x10 的随机颜色网格。网格应水平和垂直填充屏幕,所有单元格应具有相同的尺寸。由于网格 (4x10) 的尺寸将来可能会发生变化,所以 GridLayout 对我来说比 TableLayout 更有意义。 GridView 和 RecyclerView 退出了,因为我不需要任何滚动行为。

因为我需要在运行时添加这些子视图,所以我开始计算单元格宽度和高度作为屏幕宽度和高度的比率。然后我偶然发现了this SO post,它说 GridLayout 有更好的方法来实现这种行为。有一个基于静态 (XML) 的视图的代码示例,但我找不到 Java/Kotlin 示例。我正在尝试使用 GridLayout.Spec 在子视图的 LayoutParams 中使用,但无法弄清楚它是如何工作的。

更新了所需布局的屏幕截图。此图像为 12x10,但我希望能够灵活地更改尺寸(编译时间)。

【问题讨论】:

  • 如果你能分享一个截图(你想要实现的)或者你的xml布局更精确会更有帮助。
  • 添加了截图。到目前为止,我的布局有一个空的 GridLayout,因为我想动态添加颜色单元格 - 这就是我卡住的地方。

标签: android android-gridlayout


【解决方案1】:

与 LinearLayout 类似,GridLayout 规范中有一个 weight 属性 - 您可以将所有视图设置为 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)
    }

【讨论】:

    【解决方案2】:
            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);
    

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多