Android:添加自定义视图以清空GridLayout

时间:2012-09-04 05:12:57

标签: android android-gridlayout

在我的应用程序中,我必须显示一个ListView,listview的每一行应该包含一个GridLayout。 gridlayout的项目是通过适配器类动态添加的。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        // if it's not create convertView yet create new one and consume it
        if (convertView == null) {
            // instantiate convertView using our list_item
            convertView = inflater.inflate(R.layout.order_grid_list_view, null);
            // get new ViewHolder
            holder = new ViewHolder();
            holder.order_row = (GridLayout) convertView
                    .findViewById(R.id.order_row);
            holder.order_no = (TextView) convertView
                    .findViewById(R.id.order_no);
            holder.order_date = (TextView) convertView
                    .findViewById(R.id.order_date);
            holder.complete = (Button) convertView
                    .findViewById(R.id.order_btn);
        }
        // if it's exist convertView then consume it
        else {
            Log.d("ListAdaptor", "convertView is not null");
            holder = (ViewHolder) convertView.getTag();
        }

        orderno_ = data.get(position).getNo() + "";
        // set data to holder
        holder.order_no.setText(data.get(position).getNo() + "");
        holder.order_date.setText(data.get(position).getDateTime());
        if (mode.equalsIgnoreCase("Y")) {
            holder.complete.setText("InComplete");

        } else {
            holder.complete.setText("Complete");
            holder.complete.setBackgroundDrawable(context.getResources()
                    .getDrawable(R.drawable.green_button_bg));
        }
        for (int i = 0; i < data.get(position).getItems().size(); i++) {
            holder.order_row.addView(getChildView(position, i));
        }

        holder.complete.setOnClickListener(new ButtonOnClickListener(
                clickListener, orderno_));
        return convertView;
    }

    private View getChildView(int pos, int i) {
        View child = inflater.inflate(R.layout.order_item, null);
        InnerViewHolder inholder = new InnerViewHolder();
        inholder.name = (TextView) child.findViewById(R.id.order_name);
        inholder.items = (ListView) child.findViewById(R.id.order_list);

        inholder.name.setText(data.get(pos).getItems().get(i).getItemName());
        inholder.items.setAdapter(new OrderItemOptionAdapter(context, data
                .get(pos).getItems().get(i).getOptions()));

        return child;
    }

    static class ViewHolder {
        GridLayout order_row;
        TextView order_no, order_date;
        Button complete;

    }

    static class InnerViewHolder {
        TextView name;
        ListView items;
    }

但我看不到任何网格项目。请告诉我正确的实施方式

1 个答案:

答案 0 :(得分:0)

在getChildView()中,我编写了这段代码。现在它显示网格。但我不想使用硬编码值。

child.setLayoutParams(new GridLayout.LayoutParams(new LayoutParams(250,300)));

赞赏任何有效的答案