【问题标题】:How to change color of a listview's item and keep it unchaged until another item clicked from that listview android?如何更改列表视图项目的颜色并保持不变,直到从该列表视图 android 单击另一个项目?
【发布时间】:2014-06-10 10:56:33
【问题描述】:

如何更改列表视图的颜色并使其保持不变,直到从该列表视图中单击另一个项目?

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
       super.onCreate(savedInstanceState);
       setContentView(R.layout.dialog);

       List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<3;i++){
            HashMap<String, String> hm = new HashMap<String,String>();

            hm.put("ICONIMG", Integer.toString(drwbl_imgs[i]) );   
            hm.put("TITLE",str_tit[i]);
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "ICONIMG","TITLE"};

        // Ids of views in listview_layout
        int[] to = { R.id.img_listicon,R.id.txtvw_slidebar_listview};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.child_slidebar_listview_layout, from, to);

        // Setting the adapter to the listView
        listView.setAdapter(adapter);
  }
}

ma​​in_layout.xml

  <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 

        android:background="@android:color/white"
        android:divider="@android:color/transparent" >

    </ListView>   

child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_listview_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:orientation="horizontal"
>


<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:background="#808080"

     >


 <ImageView
        android:id="@+id/img_listicon"
        android:layout_width="15dp"
        android:layout_height="15dp"


    />


 <TextView
    android:id="@+id/txtvw_slidebar_listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
android:gravity="left|center"
    android:text=""
    />

     </LinearLayout>
    </LinearLayout>

【问题讨论】:

    标签: android listview


    【解决方案1】:

    您需要为列表项提供选择器。

    left_menu_list_seletor.xml:

        <item android:drawable="@color/listItemSelectedItemBgColor" android:state_activated="true" android:state_enabled="true"/>
        <item android:drawable="@color/listItemSelectedItemBgColor" android:state_pressed="true"/>
        <item android:drawable="@color/listItemColor"/>
        <item android:drawable="@color/listItemColor" android:state_enabled="false"/>
    
    </selector>
    

    您可以在 colors.xml 文件中指定颜色

    将背景属性添加到您的 child_layout,例如 android:background="@drawable/left_menu_list_seletor"

    不要忘记在列表视图中添加选择模式

    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    

    【讨论】:

    • 您可以将 android:choiceMode="singleChoice" 添加到 XML 以节省 Java 代码
    【解决方案2】:

    我不确定您所说的“列表视图的颜色”是指列表视图的背景颜色还是列表视图项的背景颜色?

    假设您想在这里设置所选列表项的背景颜色是我的答案。

    1. 在你的活动类中声明这个变量:

      私人查看 mPrevSelectedSong;

    2. 在Activity onCreate() 中编写如下onListItemClick 方法:

      mListView.setOnItemClickListener(new OnItemClickListener()
      {
      
          @Override
          public void onItemClick(AdapterView<?> arg0, final View view,
                  final int pos, long arg3)
          {
                  mLocationClientHandler.animateMapToOffer(pos);
                  if (mPrevSelectedSong != null)
                      mPrevSelectedSong.setBackgroundColor(Color.TRANSPARENT);
                  //view.setSelected(true);
                  view.setBackgroundColor(Color.parseColor("#669ab311"));//any color code that you want to set for selected state
                  mPrevSelectedSong = view;
          }
      });
      

    希望这能解决您的问题...

    【讨论】:

    • 该解决方案不需要任何Java。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多