我正在动态更改listviewAdapter行选定项。在适配器中,默认情况下selectedItem为-1。
public static int selectedItem = -1; // no item selected by default
和方法highlightItem在适配器getview方法中调用。
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.catalogue_row, null);
holder = new ViewHolder();
highlightItem(selectedItem,position,vi);
这是我的highlightItem方法。
private static void highlightItem(int selectedItem,int position, View result) {
System.out.println("selected item "+selectedItem);
if(position == selectedItem) {
// you can define your own color of selected item here
ViewHolder.lyCatalogueRow.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwHightlightColor()));
} else {
// you can define your own default selector here
ViewHolder.lyCatalogueRow.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwBackgroundColor()));
}
}
当我点击listview项目
时private OnItemClickListener itemlistener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
int item_position = position -1;
System.out.println("selectedItemposition "+item_position);
CatalogueEfficientAdapter.selectedItem=item_position;
catalogueEfficientAdapter.notifyDataSetChanged();
}
};
我的arraylist包含三个项目。当我点击第三个项目时,它会被突出显示。现在,如果我单击第二个元素,则只应突出显示第二个元素,但第三个元素正在高亮显示。如何仅选择和高亮显示所选项目
更新 问题是没有更新的highlightItem。
答案 0 :(得分:2)
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.catalogue_row, null);
if(position == selectedItem) {
// you can define your own color of selected item here
vi.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwHightlightColor()));
} else {
// you can define your own default selector here
vi.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwBackgroundColor()));
}
答案 1 :(得分:0)
如果您想手动和以编程方式选择项目,可以尝试从ViewHolder中丢弃。
确定getView方法中的每个元素!
答案 2 :(得分:0)
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// Dont decrement the position
int item_position = position;
System.out.println("selectedItemposition "+item_position);
CatalogueEfficientAdapter.selectedItem=item_position;
catalogueEfficientAdapter.notifyDataSetChanged();
}
答案 3 :(得分:0)
// try this way hope this will help you...
1.define your "ListView" item selector color in colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_selected_bg">#FF0000</color>
</resources>
2.define drawable selector for "ListView" item
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="@color/list_item_selected_bg"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="@color/list_item_selected_bg" />
</selector>
3.use this selector for "ListView" item selection
yourListView.setSelector(R.drawable.list_item_selector);