如何更改列表视图的颜色并保持不变直到从该列表视图中单击另一个项目?
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);
}
}
main_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>
答案 0 :(得分:0)
您需要将选择器提供给列表项。
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
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
答案 1 :(得分:-1)
我不确定你的'Listview颜色'是什么意思你是列表视图的背景颜色还是listview项目的背景颜色?
假设您要设置所选列表项的背景颜色,这是我的答案。
在您的活动类中声明此变量:
私人查看mPrevSelectedSong;
在Activities 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;
}
});
希望这能解决你的问题...