listview 选中变换背景
package com.tank;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class Device_adapter extends BaseAdapter{

    private Context context;
    private List<String> list;
    private int selectitem=-1;

    public Device_adapter(Context context, List<String> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if(view==null){
            viewHolder = new ViewHolder();
            view = inflater.inflate(R.layout.device_item,null);
            viewHolder.name = (TextView) view.findViewById(R.id.txt_devicename);
            viewHolder.img = (ImageView) view.findViewById(R.id.img_device);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        if(list.size()>0&&list!=null) {
            viewHolder.name.setText(list.get(i));
        }
        if (i == selectitem) {
//             view.setBackgroundColor(Color.CYAN);
            view.setBackgroundResource(R.mipmap.device_bg_click);
            viewHolder.img.setBackgroundResource(R.mipmap.device_click);
            viewHolder.name.setTextColor(Color.parseColor("#ffffff"));
        }
        else {
//             view.setBackgroundColor(Color.TRANSPARENT);
            view.setBackgroundResource(R.mipmap.device_bg_normal);
            viewHolder.img.setBackgroundResource(R.mipmap.device_normal);
            viewHolder.name.setTextColor(Color.parseColor("#606060"));
        }

            return view;
    }

    public void setselectnum(int selectitem){
     this.selectitem = selectitem;
    }


    class ViewHolder{
        TextView name;
        ImageView img;
    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011821032/article/details/52351066
文章标签: listview
个人分类: Android爬坑路
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

listview 选中变换背景

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭