我在网上搜索了整个网络后发布了这个问题,但没有找到解决我问题的有效方法。我知道它在其他几篇文章中都有讨论,但没有一篇文章帮助我解决了这个问题。
我已经实现了一个 ListView,其中突出显示了所选项目。一切运行良好,当我选择一行时,它突出显示并在使用 myListView.setSelection(index) 时被突出显示,但我有一个我真正需要解决的问题:当我点击布局中的任何其他视图时(按钮,复选框,单选按钮 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);
}
列表视图以非常简单的方式添加到布局中
<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;
}
});
}
}
我仍在等待更好的方法的建议......无论如何,这现在有效:)