在自定义列表视图中更改列表项的背景颜色

时间:2012-11-03 07:45:42

标签: android

我有一个包含7个文本视图的自定义列表视图..我为此创建了一个自定义线性布局。然后我填充它...我想要做的是设置自定义颜色...当用户点击列表项并保持突出显示直到他点击另一个项目...我将如何做到这一点?...

这是列表项的自定义布局......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LLtv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/colors"
    android:cacheColorHint="#00000000" >

    <TextView
        android:id="@+id/TvVLdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/TvVLdate"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLcardno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/TvVLtime"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLsaletext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TvVLdate"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:text="Sale transaction:Rs"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLamnt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TvVLsaletext"
        android:layout_toRightOf="@+id/TvVLsaletext"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLtexttip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TvVLamnt"
        android:layout_toRightOf="@+id/TvVLamnt"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:text="Tip:Rs"
        android:textColor="#000000"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/TvVLtip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TvVLtexttip"
        android:layout_toRightOf="@+id/TvVLtexttip"
        android:paddingTop="8dp"
        android:textColor="#000000"
        android:textSize="15dp" />

</RelativeLayout>

我的列表布局..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0E0E0"
    android:orientation="vertical" >

    <include
        android:id="@+id/Hvoid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/header" />

    <RelativeLayout
        android:id="@+id/RLvoid"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Hvoid"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="12dp"
            android:text="Main Menu"
            android:textColor="#000000"
            android:textSize="15dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:src="@drawable/ivoid" />
    </RelativeLayout>

    <View
        android:id="@+id/Vvoidtop"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_below="@+id/RLvoid"
        android:background="@android:color/darker_gray" />

    <ListView
        android:id="@+id/Lvvoid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/Bconfirm"
        android:layout_below="@+id/Vvoidtop"
        android:background="#E0E0E0"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/transparent"
        android:dividerHeight="20dp" >
    </ListView>

    <Button
        android:id="@+id/Bconfirm"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_above="@+id/Vvoidbot"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/cbconfirm" />

    <View
        android:id="@+id/Vvoidbot"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_above="@+id/Tvfooter"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/Tvfooter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:text="© India Transact Services Ltd."
        android:textColor="#000000"
        android:textSize="15dp" />

</RelativeLayout>

可绘制的颜色xml文件......

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Selected Item -->
    <item android:drawable="@android:color/darker_gray" android:state_selected="true"/>
    <item android:drawable="@android:color/darker_gray"  android:state_focused="true"/>
    <!-- Default Item -->
    <item android:drawable="@android:color/white" />
</selector>

我的代码......

public class Void extends Activity {
    ListView lmenu;
    View Vlistselector;
    String s;
    View layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voidlist);

        Menu Menu_data[] = new Menu[] {
                new Menu("29/09/2012", "05:40", "2234 xxxx xxxx 1990", "100",
                        "10"),
                new Menu("09/12/2012", "00:34", "2234 xxxx xxxx 1990", "99",
                        "12"),
                new Menu("29/09/2012", "12:20", "2234 xxxx xxxx 1990", "111",
                        "0"),
                new Menu("31/01/2012", "13:40", "2234 xxxx xxxx 1990", "105",
                        "19"),
                new Menu("13/10/2012", "01:40", "2234 xxxx xxxx 1990", "203",
                        "1"),
                new Menu("23/11/2012", "07:40", "2234 xxxx xxxx 1990", "44",
                        "5") };

        MenuAdapter adapter = new MenuAdapter(this, R.layout.voidlisttext,
                Menu_data);

        lmenu = (ListView) findViewById(R.id.Lvvoid);

        lmenu.setAdapter(adapter);
        // lmenu.setSelector(R.drawable.listcolorselector);
        lmenu.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> ada, View v, int position,
                    long id) {
                // TODO Auto-generated method stub

                /*
                 * v.setSelected(true);
                 */
                v.setBackgroundColor(Color.parseColor("#FCD5B5"));

                if (!(Vlistselector == null) && Vlistselector != v)
                    Vlistselector.setBackgroundColor(Color
                            .parseColor("#EEEEEE"));
                Vlistselector = v;

                Toast.makeText(getApplicationContext(),
                        String.valueOf(position + 1), Toast.LENGTH_LONG).show();
            }
        });

        findViewById(R.id.BLogout).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    public class Menu {
        public String date;
        public String time;
        public String cardno;
        public String amnt;
        public String tip;

        public Menu() {
            super();
        }

        public Menu(String date, String time, String cardno, String amnt,
                String tip) {
            super();
            this.date = date;
            this.time = time;
            this.cardno = cardno;
            this.amnt = amnt;
            this.tip = tip;

        }
    }

    public class MenuAdapter extends ArrayAdapter<Menu> {

        Context context;
        int layoutResourceId;
        Menu data[] = null;

        public MenuAdapter(Context context, int layoutResourceId, Menu[] data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            MenuHolder holder = null;

            if (row == null) {
                LayoutInflater inflater = ((Activity) context)
                        .getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new MenuHolder();
                holder.date = (TextView) row.findViewById(R.id.TvVLdate);
                holder.time = (TextView) row.findViewById(R.id.TvVLtime);
                holder.cardno = (TextView) row.findViewById(R.id.TvVLcardno);
                holder.amnt = (TextView) row.findViewById(R.id.TvVLamnt);
                holder.tip = (TextView) row.findViewById(R.id.TvVLtip);
                row.setTag(holder);

            } else {
                holder = (MenuHolder) row.getTag();
            }

            Menu Menu = data[position];
            holder.date.setText(Menu.date);
            holder.time.setText(Menu.time);
            holder.cardno.setText(Menu.cardno);
            holder.amnt.setText(Menu.amnt);
            holder.tip.setText(Menu.tip);

            return row;
        }

        public class MenuHolder {
            TextView date;
            TextView time;
            TextView cardno;
            TextView amnt;
            TextView tip;
        }
    }
}

我想要的是代替颜色较深的灰色设置一种新的自定义颜色.... 我是以正确的方式完成的,还是有其他方法可以做到这一点?????

2 个答案:

答案 0 :(得分:0)

对于这种情况,您应该在运行时使用setBackground()而不是依赖于选择器。

答案 1 :(得分:0)

View listColor;//declare this at the top

点击listviewitem

@Override
protected void onListItemClick(ListView list, View view, int position, long id) {

if(listColor!=null){
    listColor.setBackgroundColor(Color.BLACK);
    listColor=view;
}else{
    listColor=view;
}
listColor.setBackgroundColor(Color.BLUE); //intead of blue you can change it to something you want like darkergray
//show toast on the clicked item 
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

}