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

更新:不要根据位置设置视图的背景,因为一旦滚动,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 回答