Android ListView 项目背景颜色

发布于 2024-12-09 22:10:03 字数 201 浏览 1 评论 0原文

我试图让用户选择多个项目。我想“突出显示”所选的每个列表项,以便您可以知道已选择哪些项目。

我尝试过: view.setBackgroundResource(); view.setBackgroundColor(); view.setBackgroundDrawable();

我没有取得任何成功。

感谢您的帮助!

I am trying to allow the user to select more than one item. I want to "highlight" each list item selected so you can tell which item(s) have been selected.

I have tried:
view.setBackgroundResource();
view.setBackgroundColor();
view.setBackgroundDrawable();

I havent had any success.

Thannks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-12-16 22:10:03

您需要使用自定义数组适配器,如果您使用仅包含一个元素的列表视图,您可能可以摆脱一些实现的列表视图方法,但如果您制作自定义数组适配器,您将拥有更大的灵活性和更少的麻烦。您可以访问其中的所有元素,每个元素可以有自己的 onClickListener 和所有内容

You need to use a custom array adapter , if you are using a listview with only one element you MAY be able to get away with some implemented listview method, but you will have much more flexibility and less headache if you make a custom array adapter instead. You can access all elements in it, each element can have its own onClickListener and everything

顾北清歌寒 2024-12-16 22:10:03

如果将字符串数组传递给适配器,则可以像这样创建自定义适配器,并且可以更改所选项目的背景颜色,如下所示:

将适配器设置为 listview,如下所示:

String[] options={"abc","def","ghi","jkl"};

CustomAdapter ca=new CustomAdapter(this,options);    
listView.setAdapter(ca);

这是自定义适配器类:

public class CustomAdapter extends BaseAdapter
    {
        String items[];
        LayoutInflater mInflater;
        Context context;

    public CustomAdapter(Context context,String[] items)
    {
        this.items=items;
        this.context=context;
        mInflater = LayoutInflater.from(context);
    }

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

        ViewHolder holder;

        if(convertView==null)
        {
             convertView = mInflater.inflate(R.layout.cutsom_listitem, null);
             holder = new ViewHolder();

             holder.itemName=(TextView)convertView.findViewById(R.id.itemName);

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


        String option=items[position];
        holder.itemName.setText(option);

        holder.itemName.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                holder.itemName.setBackgroundColor(Color.parseColor("#FF0000"));  // making selected item red colored
            }
        });

        return convertView;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return items[position];
    }

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

public static class ViewHolder
{
    TextView itemName;
}

If you are passing a String array to the adapter,you can create custom adapter like this and can change background color of a selected item as below:

set the adapter to listview like:

String[] options={"abc","def","ghi","jkl"};

CustomAdapter ca=new CustomAdapter(this,options);    
listView.setAdapter(ca);

and here is the custom adapter class:

public class CustomAdapter extends BaseAdapter
    {
        String items[];
        LayoutInflater mInflater;
        Context context;

    public CustomAdapter(Context context,String[] items)
    {
        this.items=items;
        this.context=context;
        mInflater = LayoutInflater.from(context);
    }

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

        ViewHolder holder;

        if(convertView==null)
        {
             convertView = mInflater.inflate(R.layout.cutsom_listitem, null);
             holder = new ViewHolder();

             holder.itemName=(TextView)convertView.findViewById(R.id.itemName);

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


        String option=items[position];
        holder.itemName.setText(option);

        holder.itemName.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                holder.itemName.setBackgroundColor(Color.parseColor("#FF0000"));  // making selected item red colored
            }
        });

        return convertView;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return items[position];
    }

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

public static class ViewHolder
{
    TextView itemName;
}
原谅我要高飞 2024-12-16 22:10:03

您可以在适配器中覆盖 setOnTouchListener:

view.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                //the bacground when u select item
                v.setBackgroundResource(android.R.color.holo_blue_light);
                break;

            case MotionEvent.ACTION_UP:

                //设置背景为未选中正常状态
            v.setBackgroundResource(android.R.color.background_light);
                break;

            default:
                v.setBackgroundResource(R.drawable.mm_listitem_simple);
                break;

            }
            return false;
        }

您可以在操作挂起时设置差异背景。

u can overwrite setOnTouchListener in your adapter:

view.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                //the bacground when u select item
                v.setBackgroundResource(android.R.color.holo_blue_light);
                break;

            case MotionEvent.ACTION_UP:

                //设置背景为未选中正常状态
            v.setBackgroundResource(android.R.color.background_light);
                break;

            default:
                v.setBackgroundResource(R.drawable.mm_listitem_simple);
                break;

            }
            return false;
        }

you can set diffrence background when your action hanppend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文