Change background color of an item in Android ListActivity onListItemClick

后端 未结 6 1059
醉酒成梦
醉酒成梦 2021-02-14 09:12

I know it sounds very simple, and there are questions about this. But none of it could solve my problem. So here we go:

I want to change background color of a list item

6条回答
  •  渐次进展
    2021-02-14 09:33

    Simply you can do it like this in onListItemClick method

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    
        for (int a = 0; a < l.getChildCount(); a++) {
            l.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
        }
    
        ColorDrawable colorDrawable1 = new ColorDrawable(
                Color.parseColor("#A0A3A0"));
        v.setBackgroundDrawable(colorDrawable1);   
    
        if (position == 0) {
            Intent i = new Intent(MainActivity.this, NewActivity.class);
    
            startActivity(i);
        }
    
    }
    

提交回复
热议问题