[英]Change background color of an item in Android ListActivity onListItemClick
我知道這聽起來很簡單,對此有疑問。 但它都不能解決我的問題。 所以我們走了:
我想在用戶點擊時更改ListActivity
列表項的背景顏色,並在用戶再次單擊時將其更改回原始顏色(即選擇/取消選擇項目類型)
我嘗試使用getChildAt,如果我在一個屏幕上看到所有項目而不必滾動,它就能很好地工作。
碼:
getListView().getChildAt(position).setBackgroundColor(Color.CYAN);
當我在列表中有更多項目並且用戶必須滾動它們時,問題就開始了。 一旦項目的背景發生變化,當我滾動時,背景顏色會顯示在新顯示的項目上。 此外,再次單擊該項時, getChildAt(position)
將返回null
(因此返回NullPointerException
)。
任何人都可以幫我一個簡單的代碼,幫助我改變列表項的背景顏色?
提前致謝!
當然可以。 我會在自定義ListAdapter
的getView()
方法中執行此ListAdapter
。
MyAdapter extends SimpleAdapter {
private ArrayList<Integer> coloredItems = new ArrayList<Integer>();
public MyAdapter(...) {
super(...);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (coloredItems.contains(position)) {
v.setBackgroundColor(Color.CYAN);
} else {
v.setBackgroundColor(Color.BLACK); //or whatever was original
}
return v;
}
}
單擊列表項時更新coloredItems
。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (coloredItems.contains(position)) {
//remove position from coloredItems
v.setBackgroundColor(Color.BLACK); //or whatever was original
} else {
//add position to coloredItems
v.setBackgroundColor(Color.CYAN);
}
}
如果您正在處理ListFragment
那么此代碼將有所幫助,
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (view != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
catagoryValueListView=getListView();
catagoryValueListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (ColoredView != null)
ColoredView.setBackgroundColor(Color.WHITE); //original color
view.setBackgroundColor(Color.BLUE); //selected color
ColoredView = view;
}
});
}
}
我所做的是創建一個名為ie list_background的xml文件並將其放在drawable文件夾中。
xml看起來像這樣:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/list_selected" android:state_pressed="true" />
<item android:drawable="@android:color/white" />
</selector>
在ListView的項目的xml代碼中,我把這個xml作為項目背景即
item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Fill"
android:background="@drawable/list_background">
<!-- Your layout here -->
</RelativeLayout>
style = @ style / Fill只是我為android制作的捷徑:layout_height =“match_parent”和android:layout_width =“match_parent
然后在onListItemCLick中:
public void onListItemClick(ListView l, View v, int position, long id) {
v.setPressed( !v.isPressed ) //Toggle between colors of the view
}
你可以在onListItemClick
方法中這樣做
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
for (int a = 0; a < l.getChildCount(); a++) {
l.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
}
ColorDrawable colorDrawable1 = new ColorDrawable(
Color.parseColor("#A0A3A0"));
v.setBackgroundDrawable(colorDrawable1);
if (position == 0) {
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
}
}
這就是我做到的:
創建一個全局變量View ColoredView
; 然后當你為ListView
setOnItemClickListener
時,執行以下操作:
MenuList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (ColoredView != null)
ColoredView.setBackgroundColor(Color.WHITE); //original color
view.setBackgroundColor(Color.BLUE); //selected color
ColoredView = view;
}
});
這是我認為最簡單的方法。
謝謝heycosmo。 你的解決方案解決了我的問
不知道為什么我們應該在2個地方設置背景。
1.適配器的getView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
....
....
....
if(arrayBools[position]) {
view.setBackgroundColor(Common.colorBkgroundSelected);
}
else{
view.setBackgroundColor(Common.colorBkgroundNormal);
}
....
....
....
}
2. ListActivity的onListItemClick()。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
arrayBools[position] = ( arrayBools[position] ? false : true );
if(arrayBools[position]) {
v.setBackgroundColor(colorBkgroundSelected);
}
else{
v.setBackgroundColor(colorBkgroundNormal);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.