选择项目后,我需要在列表视图中突出显示所选行。
例如:我选择第一项突出显示,如果我选择第3项,则删除第1项突出显示,然后突出显示第3项。
我在Stackoverflow中发布了很多示例和问题但未找到合适的解决方案。
下面是我的代码,它可以工作,但我需要选择项目两次以突出显示,我该如何修改它才能顺利工作?
ListView mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of planet names.
planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune",
"Ceres","Pluto","Haumea","Makemake","Eris"};
planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
planetsAdapter = new PlanetsAdapter (this,planetList);
planetsAdapter .setNotifyOnChange(true);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(planetsAdapter );
mainListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mainListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
planetsAdapter.setSelectedPosition(position);
planetsAdapter.notifyDataSetChanged();
}
});
改编班级:
public class PlanetsAdapter extends ArrayAdapter<String> {
private ArrayList<String> planets;
private Context mContext = null;
private LayoutInflater inflater;
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public PlanetsAdapter (Context context,ArrayList<String> objects) {
super(context,0,objects);
mContext = context;
planets = objects;
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
}
public int getSelectedPosition(){
return selectedPos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView title = (TextView) convertView
.findViewById(R.id.rowTextView);
// change the row color based on selected state
if(position == selectedPos){
//title.setTextColor(Color.parseColor("#FFFFFF"));
title.setBackgroundColor(Color.parseColor("#ED07E1"));
}else{
//title.setTextColor(Color.parseColor("#000000"));
title.setBackgroundColor(Color.parseColor("#e2e2e2"));
}
title.setText(getItem(position));
return convertView;
}
}
编辑:已回答
For anyone who is looking for answers, i have answered it below hope it helps.
答案 0 :(得分:0)
将其设置为listview(xml或代码)
XML: android:choiceMode="singleChoice"
code: mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
在你的xml中,尝试添加android:listSelector背景,以便为你的选择提供反馈。
参考:http://developer.android.com/reference/android/widget/AbsListView.html
答案 1 :(得分:0)
经过大量研究,R&amp; D终于得到了解决方案:
活动或片段中的列表视图:
ListView mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of planet names.
planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune",
"Ceres","Pluto","Haumea","Makemake","Eris"};
planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
planetsAdapter = new PlanetsAdapter (this,planetList);
planetsAdapter .setNotifyOnChange(true);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(planetsAdapter );
mainListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mainListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
view.setSelected(true); //Important
}
});
适配器类:
public class PlanetsAdapter extends ArrayAdapter<String> {
private ArrayList<String> planets;
private Context mContext = null;
private LayoutInflater inflater;
public PlanetsAdapter (Context context,ArrayList<String> objects) {
super(context,0,objects);
mContext = context;
planets = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView title = (TextView) convertView
.findViewById(R.id.rowTextView);
title.setText(getItem(position));//Set the text to textview
return convertView;
}
}
以XML格式定义的ListView:
<ListView
android:id="@+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:cacheColorHint="#00000000">
</ListView>
每个列表项在xml list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="@drawable/list_item_selector"
android:textColor="@drawable/text_color_selector"
android:textSize="16sp" >
</TextView>
请注意,在上面的代码中,我使用了2个不同的选择器,一个用于背景,另一个用于更改文本颜色。
list_item_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/defalut_color" android:state_activated="false"/>
<item android:drawable="@color/defalut_color" android:state_activated="false" android:state_pressed="false"/>
<item android:drawable="@color/pressed_color" android:state_pressed="true"/>
<item android:drawable="@color/pressed_color" android:state_activated="true"/>
</selector>
text_color_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000000" android:state_activated="false"/>
<item android:color="#000000" android:state_activated="false" android:state_pressed="false"/>
<item android:color="#FFFFFF" android:state_pressed="true"/>
<item android:color="#FFFFFF" android:state_activated="true"/>
</selector>
RES /值/ colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="defalut_color">#e2e2e2</color>
<color name="pressed_color">#ED07E1</color>
</resources>
所以它很享受:)