我發佈這個問題後谷歌瀏覽全部沒有找到我的問題的工作解決方案。我知道它在其他幾個帖子中都有討論,但他們都沒有幫助我解決問題。安卓ListView行亮點
我已經實現了一個ListView,其中所選項目被突出顯示。一切工作都很順利,當我選擇一行時,它突出顯示,並在使用myListView.setSelection(索引)時是高亮的,但我有一個問題,我真的需要解決:當我點擊佈局中的任何其他視圖(按鈕,複選框,單選按鈕ecc)ListView失去選擇。
這是列表視圖是如何實現的:
我已經擴展了標準的ListView
public SelectableListView(Context context) {
super(context);
initStyle(null);
}
public SelectableListView(Context context, AttributeSet attrs) {
super(context, attrs);
initStyle(attrs);
}
public SelectableListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initStyle(attrs);
}
private void initStyle(AttributeSet attrs) {
this.setBackgroundResource(R.drawable.red_border_fat);
this.setPadding(3, 3, 3, 3);
this.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//without the following 2 lines the selection won't work
requestFocusFromTouch();//
arg1.setSelected(true);
}
});
}
@Override
public void setSelection(int position) {
//without the following line the selection won't work
requestFocusFromTouch();
super.setSelection(position);
}
ListView控件添加到佈局艾利簡單的方式
<my.name.space.SelectableListView
android:layout_width="wrap_content"
android:layout_height="100dp" >
</my.name.space.SelectableListView>
這是適配器
public static SimpleAdapter GetClassAdapter(Context context,ArrayList<SeatClass> items){
try {
final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
String[] fields = new String[] { "className" };
int[] views = new int[] { R.skin_class.lblClassDescription};
SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.skin_class_list, fields, views);
for (SeatClass sc : items) {
HashMap<String, String> temp = new HashMap<String, String>();
temp.put("className", Typologic.getClasse(sc.Id).Description);
list.add(temp);
}
return adapter;
} catch (Exception e) {
LogHelper.WriteLogError("error in GetClassAdapter", e);
return null;
}
}
這是佈局對每行(R.layout.skin_class_list)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+skin_class/mainview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selectable_listview_background_selector"
android:orientation="horizontal">
<TextView
android:id="@+skin_class/lblClassDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DESCRIPTION"
android:textColor="@drawable/selectable_listview_textcolor_selector" />
</LinearLayout>
selectable_listview_background_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_focused="true" android:drawable="@color/black" />
<item android:state_selected="true" android:state_focused="false" android:drawable="@color/black" />
<item android:state_pressed="true" android:state_focused="true" android:drawable="@color/black" />
<item android:state_pressed="true" android:state_focused="false" android:drawable="@color/black" />
<item android:drawable="@color/White" />
</selector>
selectable_listview_textcolor_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/White" />:
<item android:state_pressed="true" android:color="@color/White" />:
<item android:color="@color/black" />
</selector>
正如我所說的這個作品完美,但是當我點擊任何其他視圖時,我無法保持突出顯示。我搜索了幾天尋找解決方案,但似乎沒有任何工作。
編輯: 我發現這個解決方案,我不再使用選擇器。這裏MyLabel是一些自定義的TextView - 在這種情況下不相關的 - 功能
public abstract class SelectableListView<T> extends ListView {
protected ArrayList<T> _data;
protected int _selectedIndex = 0;
public SelectableListView(Context context) {
super(context);
initStyle(null);
}
public SelectableListView(Context context, AttributeSet attrs) {
super(context, attrs);
initStyle(attrs);
}
public SelectableListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initStyle(attrs);
}
public abstract void loadData(ArrayList<T> data);
public T getSelectedItem() {
if (_selectedIndex < 0)
return null;
else
return _data.get(_selectedIndex);
}
public T getItemAt(int position) {
if (position < 0 || position > _data.size())
return null;
else
return _data.get(position);
}
@Override
public void setSelection(int position) {
colorizeSelection(position);
super.setSelection(position);
_selectedIndex = position;
}
private void colorizeSelection(int index){
try {
for (int i = 0; i < this.getCount(); i++) {
if(i==index)
this.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.Red));
else
this.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.White));
ViewGroup root=(ViewGroup) this.getChildAt(i);
colorizeSelection(root,i==index);
}
} catch (Exception e) {
LogHelper.WriteLogError("error in colorizeSelection function", e);
}
}
//this function is called recursively to scan all childs of a given view
private void colorizeSelection(ViewGroup v,boolean selected){
for (int j = 0; j < v.getChildCount(); j++) {
if(v.getChildAt(j) instanceof ViewGroup)
colorizeSelection((ViewGroup)v.getChildAt(j),selected);
else if (v.getChildAt(j) instanceof MyLabel) {
if(selected)
((MyLabel)v.getChildAt(j)).setTextColor(getResources().getColor(R.color.White));
else
((MyLabel)v.getChildAt(j)).setTextColor(getResources().getColor(R.color.black));
((NtvLabel)v.getChildAt(j)).invalidate();
}
}
}
private void initStyle(AttributeSet attrs) {
_selectedIndex = -1;
this.setBackgroundResource(R.drawable.red_border_fat);
this.setPadding(3, 3, 3, 3);
this.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
colorizeSelection(arg2);
_selectedIndex = arg2;
}
});
}
}
我仍然在等待一個建議,一個更好的方法......在任何情況下這現在工作:)
ALLCAPS不是必需的。 –
對不起,你在說什麼?我在哪裏使用ALLCAPS? – Apperside
您使用過[這裏](http://stackoverflow.com/revisions/13843119/1)。 –