0

我在“列表视图”中有一个列表,可以使用这种混乱、分组滚动列表,并且列表有一个带有图标的标题。

公共类 MyCustomAdapter 扩展 BaseAdapter {

private static final String ASSETS_DIR = "images/";
private static final int TYPE_HEAD = -1;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;


private Context ctx;

private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter(Context context) {
    this.ctx = context;
    mInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(final String item) {
    mData.add(item);
    notifyDataSetChanged();
}

public void addSeparatorItem(final String item) {
    mData.add(item);
    mSeparatorsSet.add(mData.size() - 1);
    notifyDataSetChanged();
}

public void addHeadItem(){
    mData.add("");
    mSeparatorsSet.add(0);
    notifyDataSetChanged();
}

@Override
public int getCount() {

    return mData.size();
    //return equipos.size();
}

@Override
public String getItem(int position) {

    return mData.get(position) ;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public int getItemViewType(int position) {
    if (position==0)
        return TYPE_HEAD;

    return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    int type = getItemViewType(position);
    System.out.println("getView " + position + " " + convertView
            + " type = " + type);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
        case TYPE_ITEM:
            convertView = mInflater.inflate(R.layout.list_item, null);

            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
            holder.textView3 = (TextView) convertView.findViewById(R.id.textView3);
            holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);

            String[] datos = mData.get(position).split("-");

            holder.textView1.setText(String.format(" %s - %s", datos[0],datos[1]));
            holder.textView2.setText(datos[2]);
            holder.textView3.setText(datos[3]);

            String sel_bandera =  datos[4].trim() ;

            String imgFilePath = "";

            if (sel_bandera.equals("verde")){
                 imgFilePath = ASSETS_DIR + "circle_green.png" ;
            }else if (sel_bandera.equals("amarilla")){
                 imgFilePath = ASSETS_DIR + "circle_yellow.png";
            }else {
                 imgFilePath = ASSETS_DIR + "circle_red.png";
            }
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(this.ctx.getResources().getAssets().open(imgFilePath));
                holder.imageView1.setImageBitmap(bitmap);
                //bandera.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }


            break;
        case TYPE_SEPARATOR:
            convertView = mInflater.inflate(R.layout.list_group, null);
            holder.textView1 = (TextView) convertView.findViewById(R.id.textSeparator);
            holder.textView1.setText(mData.get(position));
            break;

        case TYPE_HEAD:
            convertView = mInflater.inflate(R.layout.list_head, null);

            break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;

}


public static class ViewHolder {

    public TextView textView1;
    public TextView textView2;
    public TextView textView3;
    public ImageView imageView1;

    public TextView getTextView1() {
        return textView1;
    }
    public void setTextView1(TextView textView1) {
        this.textView1 = textView1;
    }
    public TextView getTextView2() {
        return textView2;
    }
    public void setTextView2(TextView textView2) {
        this.textView2 = textView2;
    }
    public TextView getTextView3() {
        return textView3;
    }
    public void setTextView3(TextView textView3) {
        this.textView3 = textView3;
    }
    public ImageView getImageView1() {
        return imageView1;
    }
    public void setImageView1(ImageView imageView1) {
        this.imageView1 = imageView1;
    }

}

}

公共类 EquiposActivity 扩展 ListFragment 实现 OnTouchListener {

public View onCreateView(LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState) { mAdapter = new MyCustomAdapter(getActivity());

            if (lista.length > 0) {
                String[] datos = lista[0].split("-");
                cabecera_grupo = datos[4];
            }

            mAdapter.addHeadItem();

            for (int i = 0; i < lista.length; i++) {

                String[] datos = lista[i].split("-");
                String grupo = datos[4];


                if (i == 0) {
                    mAdapter.addSeparatorItem(grupo.replace("_", " "));

                }

                if (!grupo.equals(cabecera_grupo)) {

                    mAdapter.addSeparatorItem(grupo.replace("_", " "));
                    cabecera_grupo = grupo;
                }

                mAdapter.addItem(String.format("%s - %s - %s - %s - %s",
                        datos[0], datos[1], datos[2], datos[3], datos[5]));

            }

            setListAdapter(mAdapter);

return super.onCreateView(inflater, container, savedInstanceState); }

4

1 回答 1

0

我建议你重写你的 getView() 方法,因为我认为你错误地使用了 ViewHolder 模式。阅读:http : //www.jmanzano.es/blog/?p=166 或者只是摆脱 ViewHolder 和代码 getView() 没有它。

于 2013-02-05T20:23:38.220 回答