我有一个5行的ListView,每行有不同的图像。当我在任何位置单击listview时,该行上的ListView图像只会更改,并且每行在列表上都有不同的图像。
我在每行中都有包含ImageView和TextView的ListView项目,默认情况下,每行显示对应于行中文本的图像,当我单击列表的任何位置时,我想更改对应于列表单击位置的图像。我必须在列表单击上显示两个不同的图像。
在列表中有2个图像,一个用于事件“关闭”,第二个图像用于“开启”。
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(getApplicationContext(), "You have selected"
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}![enter image description here][1]
答案 0 :(得分:0)
有两种方法可以实现相同的
第一种方法 将列表项的背景设置为状态列表drawable 。如果在每个项目上设置此项,状态列表将自动保留状态。
第二种方法
Use HashMap to store which item the user has selected and set background depending on the user selection
Example
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class CustomListView extends ListActivity {
/** Called when the activity is first created. */
Context context = null;
HashMap<String, Boolean> userSelectionMap = new HashMap<String, Boolean>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS);
setListAdapter(arrayAdapter);
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
this.registerForContextMenu(lv);
}
static final String[] PENS = new String[]{
"item1",
"item2",
"item3",
"item4",
"item5",
"item6",
};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("...selected...");
if(isItemSelectedBefore(""+position)){
v.setBackgroundResource(R.drawable.whbk);
}else{
v.setBackgroundResource(R.drawable.grbk);
}
}
boolean isItemSelectedBefore(String position){
boolean userSelection = false;
if(userSelectionMap.containsKey(position)){
userSelection = !userSelection;
}
userSelectionMap.put(position, userSelection);
return userSelectionMap.get(position);
}
}
创建一个类似于以下 list_selector.xml
的选择器<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/android_green" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/black_alpha" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@color/black_alpha" />
<item android:drawable="@color/white_alpha" />
</selector>
xml of listview