如何在单击ListView行时更改放置在ListView中的视图窗口小部件的颜色?

时间:2014-05-02 11:20:29

标签: android listview

我已经生成了Slider ListView。目前,我能够突出显示并保持所选片段的行突出显示。我还有一个view元素,目前没有设置为任何颜色。我想在单击ListView时用橙色突出显示此视图。我怎样才能做到这一点?我用来突出Listview行的代码如下:请一步一步地指导我。

listselector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>

</selector>

list_item_bg_pressed

   <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
 <gradient
  android:startColor="@color/list_background_pressed"
  android:endColor="@color/list_background_pressed"
  android:angle="90" />
</shape>

list_itm_bg_normal

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
 <gradient
  android:startColor="@color/list_background"
  android:endColor="@color/list_background"
  android:angle="90" />
</shape>

activity_main.xml中

  <ListView
    android:id="@+id/left_drawer"
     android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    />

1 个答案:

答案 0 :(得分:0)

尝试以下代码,它适用于我:

   YOUR VIEW.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            holder.lnrMainLayout.setBackgroundResource(R.drawable.home_bg_square_selected);
                            final Timer timer = new Timer();
                            timer.schedule(new TimerTask() {

                                @Override
                                public void run() {
                                    getActivity().runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            try {
                                                holder.lnrMainLayout.setBackgroundResource(R.drawable.home_bg_square);
                                                launchActivity(obj);
                                                timer.cancel();
                                            } catch (Throwable e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });

                                }
                            }, 500, 500);

                        }
                    });

在上面的代码中设置您想要的背景而不是我的背景。