所以,我所做的是当我点击listview项目时,它变为灰色。但是当我从列表视图中选择另一个项目时,之前突出显示的灰色项目将变为默认值,新项目将变为灰色。
我想要的是让他们两个都突出显示,当点击突出显示的项目时,他们也应该恢复正常。
这是我的代码:
我的ListView XML
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:id="@+id/listView"
android:layout_marginRight="10dp"
android:choiceMode="multipleChoice"
android:listSelector="@drawable/selector"
android:layout_alignRight="@+id/group_name"
android:layout_alignEnd="@+id/group_name" />
我的Color.xml
<color name="list_pressed">#ffffff</color>
<color name="list_default">#d3d3d3</color>
我的selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:state_selected="true"
android:drawable="@color/list_pressed" />
<!-- Default Item -->
<item
android:state_selected="false"
android:drawable="@color/list_default" />
</selector>
Onclick Listner
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
view.setSelected(true);
GroupList clickedGroup = mygroupList.get(position);
String message = " you clicked name " + position + " and name is " + clickedGroup.getname();
Toast.makeText(addgroups.this, message, Toast.LENGTH_LONG).show();
}
});
等待帮助并提前致谢。
编辑: 这是我的listAdapter
private class MyListAdapter extends ArrayAdapter<GroupList>
{
public MyListAdapter()
{
super(addgroups.this, R.layout.grouplist, mygroupList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.grouplist, parent, false);
}
//find the contact to work
GroupList currentgroup = mygroupList.get(position);
TextView contactname = (TextView) itemView.findViewById(R.id.group_name);
contactname.setText(currentgroup.getname());
ImageView imageView = (ImageView) itemView.findViewById(R.id.add);
imageView.setImageResource(currentgroup.getimg());
return itemView;
//return super.getView(position, convertView, parent);
}
}