问题
By default i need to show one item as highlighted in horizontal list view and when the user selected another item in the horizontal list view i want to highlight that item(removing the earlier and highlight the currently selected) for that i'm trying with the following code,in my adapter
Adapter:-
int selectedIndex;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.hlist_rowitem, null);
if (position == selectedIndex) {
v.setBackgroundColor(Color.parseColor("#abcdef"));
}
}
and after selecting another item from activity in from the list view what to do in activity to change highlighting position of the item.
Activity:-
int sIndex;
sIndex = getIntent().getIntExtra("POSITION", 0);
hlAdapter = new HSelectedAdapter(InsuranceCard.this, rowItems, sIndex);
hListView.setAdapter(hlAdapter);
hListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//other code goes here
}
});
回答1:
I'd would use a color state list resource and have the ListView handle the selection with setSelection(position).
The color list would look something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#aabbcc"/> <!-- pressed -->
<item android:state_activated="true"
android:color="#fedcba"/> <!-- selected -->
<item android:color="#abcdef"/> <!-- default -->
</selector>
and it should be set as background of the R.layout.hlist_rowitem
or as listSelector
on the listview.
Edit: To change the selection when receiving a click event:
hListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
hListView.setSelection(position);
}
});
The ListView will deselect the old/default item and selects the new item at the specified position.
Edit 2: By default the ListView don't have a choice mode set so make sure you either set it in xml or in code: listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
回答2:
you can achieve this in two ways.
manually clear all item and set selected in onItemClick()
listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { for (int i = 0; i < adapter.getCount(); i++) { View item = listview.getChildAt(i); if (item != null) { item.setBackgroundResource(R.drawable.unselected); } arg1.setBackgroundResource(R.drawable.selected); } } });
use selector and let listview do itself.
/drawable/selector_list.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selected" android:state_selected="true"/>
<item android:drawable="@drawable/selected" android:state_activated="true"/>
<item android:drawable="@drawable/unselected"/>
</selector>
and add android:listSelector="@drawable/selector_list"
to your listview
回答3:
add listitemclick.xml in your drawblw folder this is the code. 2)then in your hlist_rowitem.xml set background="@drawable/listitemclick"
回答4:
follow these steps: 1)declare one boolean array. public static boolean ClickItem[]; 2)inside oncreate ClickItem=new boolean[your array size]; Arrays.fill(ClickItem, false);
in your adapter write this code
a)
if ClickItem[pos]
{
v.setBackgroundColor(Color.parseColor("#abcdef"));
}else
a) v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Arrays.fill(ClickItem, false);
ClickItem[pos]=true;
adapter1.notifyDataSetChanged();
}
});
来源:https://stackoverflow.com/questions/24630538/highlight-the-selected-item-in-horizontal-list-view