1
public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
            case TYPE_ITEM:
                System.out.println("getView position " + position);

                convertView = mInflater.inflate(R.layout.groupitem, null);

                holder.textView = (TextView) convertView
                        .findViewById(R.id.textitem);
                holder.btn = (Button) convertView
                        .findViewById(R.id.button1);
                holder.btn.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(),
                                "Clickable button", Toast.LENGTH_LONG)
                                .show();
                        // TODO Auto-generated method stub

                    }
                });

                break;
            case TYPE_SEPARATOR:
                convertView = mInflater.inflate(R.layout.groupheader, null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.textSeparator);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(position==1){
            convertView.setBackgroundResource(R.drawable.celltop);
            }
        else if (position>1 && position!=1) {
            convertView.setBackgroundResource(R.drawable.cellcenter);
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }

開始時の背景は正しいですが、リストを下にスクロールして再び上に戻ると、背景が変わります。たとえば、セルトップの背景が上にあり、見出しになっています。アイテムの背景を設定したいだけです

4

1 に答える 1

0

UPDATE : スクロールすると UI ビューの位置が変更されるため、位置に基づいてビューの背景を設定しないでください。ビューのタグを設定し、タグを条件としてビューの背景を設定してみてください。これはカスタム アダプタのOverrided getViewメソッドです。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list, null);
TextView description = (TextView) vi.findViewById(R.id.description);
description.setTag(myArrayList.get(position).get("hashmamkey")); 
// Where myArrayList is ArrayList<HashMap<String,String>>

// Now make a condition  
if((HashMap<String,String>) description.getTag().equals("value")){
// Do Your change background for view Work Here
}


}
于 2012-10-31T10:45:15.607 に答える