main.xml
item_module.xml
xml文件如果有问题的话就需要各位自己去稍微调整调整了。参考了网上的一些资料,都有自定义一个适配器,这里我自己也写了一个adapter。
public class CustomListViewAdapter extends BaseAdapter {
private Context context;
private List
这里有几点了,在getView()方法里可以看到,如果选中的项和适配器里的position相等的话,就改变item的背景色,但是在改变之前,要把TextView的的两项属性设置一下。
接下来就是activity了
public class PortalActivity extends FragmentActivity {
private ListView testModuleListView;
CustomListViewAdapter adapter;
private String[][] itemStrings = new String[][] {
{ "1.xxxx", Ket },
{ "2.xxxx", Ke },
{ "3.xxxx", Keest },
{ "4.xxxxx", Kt },
{ "5.xxxxx", Kt }, { "6.xxxxx", Kst },
{ "7.xxxxx", x },
{ "8.xxxx", x },
{ "9.xxxxx", t },
{ "10.xxxxx", t },
{ "11.xxxx", t },
{ "12.x", t } };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bindViews();
initListView();
bindEvents();
}
private void bindEvents() {
testModuleListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
Log.i("test", "点击了:" + position);
Map appItem = (Map) parent
.getItemAtPosition(position);
adapter.setSelectItem(position);
adapter.notifyDataSetInvalidated();
}
});
private List> initialModuleItem() {
List> list = new ArrayList>();
Map map;
for (int i = 0; i < itemStrings.length; i++) {
map = new HashMap();
map.put("itemName", itemStrings[i][0]);
map.put("action", itemStrings[i][1]);
list.add(map);
}
return list;
}
private void bindViews() {
testModuleListView = (ListView) findViewById(R.id.testModuleListView);
}
private void initListView() {
List> maps = initialModuleItem();
adapter = new CustomListViewAdapter(this, maps);
testModuleListView.setAdapter(adapter);
}
}
这样以后就能实现了。