在滚动列表背景改变android

时间:2012-10-31 10:00:18

标签: android listview

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;
    }

启动时它的背景是正确的。但是当我向下滚动列表并再次返回顶部时它的背景变化。例如,celltop背景位于顶部,这是heading.i只想为项目设置背景

1 个答案:

答案 0 :(得分: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
}


}