我能得到的背景改變一個人的ListView項目在setOnItemClickListener與不能「取消選擇」列表視圖項
view.setBackgroundResource(R.color.green);
我只需要一次一個,所以當其他列表項選擇點擊,我試過lv.invalidate()
和lv.getChildAt(0).invalidate()
,但都沒有工作,第二次導致空指針異常。任何想法使顏色迴歸?
我能得到的背景改變一個人的ListView項目在setOnItemClickListener與不能「取消選擇」列表視圖項
view.setBackgroundResource(R.color.green);
我只需要一次一個,所以當其他列表項選擇點擊,我試過lv.invalidate()
和lv.getChildAt(0).invalidate()
,但都沒有工作,第二次導致空指針異常。任何想法使顏色迴歸?
我做了一些畫面分割的東西和XML選擇不工作。要設置顏色,我最終保存了在View currentlySelectedView
中點擊的視圖,並且在單擊另一個視圖時將背景設置爲透明。
currentlySelectedView.setBackgroundResource(R.color.transparent);
將一個選擇器設置爲您在適配器中包含的視圖的背景。在選擇器中,您可以設置按下該項目時的顏色,焦點和解除按壓。這是正確的做法。
還有另一種「解決方法」,我認爲值得一提。您可以爲您的ListView創建自定義適配器類,以便爲您選擇(突出顯示)或取消選擇(取消突出顯示)該項目。
//you can extend whatever kind of adapter you want
public class AutoSelectingListViewAdapter extends ArrayAdapter<Thingy>{
private LayoutInflater inflater;
public boolean shouldHighlight = false;
public int highlightIndex = -1;
public AutoSelectingListViewAdapter(Context context, int resourceId, List<Thingy> objects) {
super(context, resourceId, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ItemHolder holder = null;
if (convertView == null){
// always recycle!
holder = new ItemHolder();
convertView = inflater.inflate(R.layout.custom_list_item, null);
holder.clue = (TextView) convertView.findViewById(R.id.custom_list_item_text);
convertView.setTag(holder);
}else{
holder = (ItemHolder) convertView.getTag();
}
// fake highlighting!
if (shouldHighlight && highlightIndex == position){
convertView.setBackgroundColor(0xffb5bfcc); // put your highlight color here
}else{
convertView.setBackgroundColor(0x00000000); // transparent
}
Thingy thingy = this.getItem(position);
holder.clue.setText(thingy.textData);
return convertView;
}
class ItemHolder{
TextView text;
// any other views you need here
}
}
這一類,則可以手動通過這樣突出的項目:
targetListView.setSelection(4); // give item 4 focus
AutoSelectingListViewAdapter myAdapter = (AutoSelectingListViewAdapter) targetListView.getAdapter();
myAdapter.highlightIndex = 4; // highlight item 4
myAdapter.shouldHighlight = true;
myAdapter.notifyDataSetChanged(); // force listview to redraw itself
你也可以unhighlight:
myAdapter.shouldHighlight = false;
myAdapter.notifyDataSetChanged();
可以使用android.widget的clearChoices()
方法。列表顯示。 http://developer.android.com/reference/android/widget/AbsListView.html#clearChoices()
以下方法OnItemClickListener.onItemClick
爲我工作,並已清除了選擇列表行:
adapter.notifyDataSetInvalidated();
我不確定,但是此方法在ListView(或ListView.getAdapter())中不存在 – Cocorico 2014-04-01 13:24:19
真的哥們,你固定我的問題。總共喜歡解決方法:* – Shivansh 2014-07-22 11:12:55