【问题标题】:How to change the color of a view widget which is placed in the ListView on clicking the ListView row?如何在单击 ListView 行时更改放置在 ListView 中的视图小部件的颜色?
【发布时间】:2014-05-02 11:20:28
【问题描述】:

我已经生成了一个 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"
    />

【问题讨论】:

  • 将选择器设置为所需的视图
  • @Ann 在下面检查我的答案,我可以突出显示 ListView 行。
  • @Raghunandan ...谢谢它的工作原理

标签: android listview


【解决方案1】:

试试下面的代码,它对我有用:

   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);

                        }
                    });

在上面的代码中设置你想要的任何背景,而不是我的背景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多