如何更改适配器中的布局?

时间:2015-06-03 12:41:42

标签: android listview layout adapter

我正在使用自定义适配器填充listview,但现在,如果id包含在数组中,我想更改行的布局。

这是我的代码:

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        CalcHolder holder = new CalcHolder();
        PojoCalc f = calcList.get(position);

        Integer[] calcs_lite = {6, 8, 18, 37, 51, 75};

        boolean exists = containsValue(calcs_lite, f.get_id());

        // First let's verify the convertView is not null
        // Value exist, so, we enable layout
        if (convertView == null) {
            // This a new view we inflate the new layout
            if(exists){
                Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::AVAILABLE");
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.calc_row_adapter, null);
            }else{
                Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::UNAVAILABLE");
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.calc_row_adapter_disabled, null);
            }

            // Now we can fill the layout with the right values
            TextView tv_name = (TextView) v.findViewById(R.id.name);
            TextView tv_subtitle = (TextView) v.findViewById(R.id.subtitle);


            holder.calcNameView = tv_name;
            holder.calcSubtitleView = tv_subtitle;

            v.setTag(holder);
        }
        else{
            holder = (CalcHolder) v.getTag();
        }

        holder.calcNameView.setText(f.getName());
        holder.calcSubtitleView.setText(f.getSubtitle());


        return v;
    }

我第一次获得列表时,它可以工作,但当我下拉以检索更多元素时,它会失败,它不会改变视图。我认为持有人的“其他”存在问题,但我不确定。

4 个答案:

答案 0 :(得分:1)

1.在自定义适配器中覆盖getItemViewType和getViewTypeCount
2.在getView中使用getItemViewType

答案 1 :(得分:1)

这是因为列表视图正在重复使用每一行,并且您正在尝试为每一行设置不同的布局,您可以将两个布局放在一个文件中并切换它(即隐藏/可见),如您所愿

   if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) _context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = vi.inflate(R.layout.grid_list_item, null);

        holder = new ViewHolder();

        holder.btn_delete_photo = (Button) convertView
                .findViewById(R.id.btn_delete_photo);
        holder.im_photo = (ImageView) convertView
                .findViewById(R.id.im_photo);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    DataHolderClassName item = itemList.get(position);

    holder.btn_delete_photo.setTag(item);
    holder.im_photo.setTag(item);

    if(exist)
    {
    hide();
    }else{
    visible();}

答案 2 :(得分:1)

你一切都很好......只需将viewgroup传递给inflate()方法并将第三个参数添加为false 就像

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    CalcHolder holder = new CalcHolder();
    PojoCalc f = calcList.get(position);

    Integer[] calcs_lite = {6, 8, 18, 37, 51, 75};

    boolean exists = containsValue(calcs_lite, f.get_id());

    // First let's verify the convertView is not null
    // Value exist, so, we enable layout
    if (convertView == null) {
        // This a new view we inflate the new layout
        if(exists){
            Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::AVAILABLE");
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.calc_row_adapter, parent, false); //changes in this line
        }else{
            Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::UNAVAILABLE");
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.calc_row_adapter_disabled, parent, false); //changes in this line
        }

        // Now we can fill the layout with the right values
        TextView tv_name = (TextView) v.findViewById(R.id.name);
        TextView tv_subtitle = (TextView) v.findViewById(R.id.subtitle);


        holder.calcNameView = tv_name;
        holder.calcSubtitleView = tv_subtitle;

        v.setTag(holder);
    }
    else{
        holder = (CalcHolder) v.getTag();
    }

    holder.calcNameView.setText(f.getName());
    holder.calcSubtitleView.setText(f.getSubtitle());


    return v;
}

答案 3 :(得分:0)

答案是你的回答。我改变了inflater的线条,我动态地改变了布局,改变了颜色。

最终的代码是:

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        LinearLayout ll_row;
        TextView tv_name, tv_subtitle;

        CalcHolder holder = new CalcHolder();
        PojoCalc f = calcList.get(position);

        Integer[] calcs_lite = {6, 8, 18, 37, 51, 75};

        boolean exists = containsValue(calcs_lite, f.get_id());

        // First let's verify the convertView is not null
        // Value exist, so, we enable layout
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.calc_row_adapter, parent, false); //changes in this line

            // Now we can fill the layout with the right values
            ll_row = (LinearLayout) v.findViewById(R.id.ll_row);
            tv_name = (TextView) v.findViewById(R.id.name);
            tv_subtitle = (TextView) v.findViewById(R.id.subtitle);


            if(exists){
                ll_row.setBackgroundColor(Color.WHITE);
                tv_name.setTextColor(Color.BLACK);
                tv_subtitle.setTextColor(Color.BLACK);
            }else{
                ll_row.setBackgroundColor(Color.LTGRAY);
                tv_name.setTextColor(Color.GRAY);
                tv_subtitle.setTextColor(Color.GRAY);
            }

            holder.calcNameView = tv_name;
            holder.calcSubtitleView = tv_subtitle;

            v.setTag(holder);
        }
        else{
            // Now we can fill the layout with the right values
            ll_row = (LinearLayout) v.findViewById(R.id.ll_row);
            tv_name = (TextView) v.findViewById(R.id.name);
            tv_subtitle = (TextView) v.findViewById(R.id.subtitle);

            if(exists){
                ll_row.setBackgroundColor(Color.WHITE);
                tv_name.setTextColor(Color.BLACK);
                tv_subtitle.setTextColor(Color.BLACK);
            }else{
                ll_row.setBackgroundColor(Color.LTGRAY);
                tv_name.setTextColor(Color.GRAY);
                tv_subtitle.setTextColor(Color.GRAY);
            }


            holder = (CalcHolder) v.getTag();
        }

        holder.calcNameView.setText(f.getName());
        holder.calcSubtitleView.setText(f.getSubtitle());


        return v;
    }

由于