Android ListView控件选中item项,并保持选中状态

这几天在做公司的项目,有这样一个需求,就是在listView里,点击item子项的时候需要保持item子项的选中状态,上网搜了一些资料,最后也实现了。现在就记录下来,一方面方便以后自己回顾好复习温故,另外也和大家分享。

不多说了,上代码。先贴xml布局文件的。

main.xml



    
        

item_module.xml




    

xml文件如果有问题的话就需要各位自己去稍微调整调整了。参考了网上的一些资料,都有自定义一个适配器,这里我自己也写了一个adapter。

public class CustomListViewAdapter extends BaseAdapter {
	
	private Context context;
	private List> list;
	private int selectItem = -1;
	
	public CustomListViewAdapter(Context context, List> maps) {
		this.context = context;
		this.list = maps;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null && list.size() != 0) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.item_module, null);
			viewHolder.itemTextView = (TextView) convertView.findViewById(R.id.itemName);
			viewHolder.portalLinearLayout = (LinearLayout) convertView.findViewById(R.id.portalLinearLayout);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
			
		}
		viewHolder.itemTextView.setText(list.get(position).get("itemName").toString());
		if (selectItem == position) {
			viewHolder.itemTextView.setSelected(true);
			viewHolder.itemTextView.setPressed(true);
			viewHolder.portalLinearLayout.setBackgroundColor(Color.YELLOW);
		} else {
			viewHolder.itemTextView.setSelected(false);
			viewHolder.itemTextView.setPressed(false);
			viewHolder.portalLinearLayout.setBackgroundColor(Color.TRANSPARENT);
		}
		return convertView;
	}
	
	private class ViewHolder {
		private TextView itemTextView;
		private LinearLayout portalLinearLayout;
	}

}

这里有几点了,在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);
	}


}
这样以后就能实现了。

你可能感兴趣的:(Android)