kunyashaw博客主页 关注kunyashaw新博客 关于kunyashaw 转到底部

53、listview、expandableListview如何选中时保持高亮?

一、listView被选中后保持高亮


70down voteaccepted

To hold the color of listview item when you press it, include the following line in your listview layout:

android:background="@drawable/bg_key"

Then define bg_key.xml in drawable folder like this:

 
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_selected="true"
        android:drawable="@color/pressed_color"/>
    <item
        android:drawable="@color/default_color" />
</selector>

Finally, include this in your listview onClickListener:

listView.setOnItemClickListener(new OnItemClickListener() { 
 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
        view.setSelected(true);
        ... 
    } 
} 

This way, only one item will be color-selected at any time. You can define your color values in res/values/colors.xml with something like this:

 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="pressed_color">#4d90fe</color>
    <color name="default_color">#ffffff</color>
</resources>

 

二、expandableListview被选中后保持高亮

1、在expandableListview的adapter中的childList中的类,加一个变量,用来标记是否被选择

public class SettingTitleComponent {

	public int set_for_what = 0;//设置什么
	public int icon_id = 0;//图标id
	public String title = "";//设置项标题
	public String title_hint = "";//设置项提示

	public boolean flagSelected = false;//用来记录是否被选择
}

  

2、在expandableListview对应的adapter中写一个方法,用来初始化所有“是否被选择”的标记位

  public void initFlagSelected() {
        for (int t = 0; t < childList.size(); t++) {
            for (int k = 0; k < childList.get(t).size(); k++) {
                childList.get(t).get(k).flagSelected = false;
            }

        }
    }

  

3、在activity中设置expandableListview的回调

expLv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                final SettingTitleComponent stc = childList.get(groupPosition).get(childPosition);
                expLvAdapter.initFlagSelected();//初始化
                stc.flagSelected = true//设置被选择
          expLvAdapter.notifyDataSetChanged()//更新adapter对应的列表,这里很关键,请看下一步
  
          return true; } });

 

4、刷新adapter时,根据标识为去判断是否应该高亮

@Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        final SettingTitleComponent stc = childList.get(groupPosition).get(childPosition);

        CommonUtils.LogWuwei(tag, "stc title is " + stc.title + " childPosition is " + childPosition + " groupPosition is " + groupPosition);

        LayoutInflater inflater = (LayoutInflater) ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View grid = inflater.inflate(R.layout.relativelayout_setting_title, null);


        ImageView ivIcon = (ImageView) grid.findViewById(R.id.imageview_setting_title_icon);
        TextView tvTitle = (TextView) grid.findViewById(R.id.textview_setting_title_text);
        TextView tvHint = (TextView) grid.findViewById(R.id.textview_setting_title_hint);

        ivIcon.setBackgroundResource(stc.icon_id);
        tvTitle.setText(stc.title);
        tvHint.setText(stc.title_hint);


        if (stc.flagSelected) {
            grid.setBackgroundColor(ctxt.getResources().getColor(R.color.Blue));
        } else {
            grid.setBackgroundColor(ctxt.getResources().getColor(R.color.Constrast));
        }
        return grid;
    }

  

三、listview保持上次的位置

/****从这里开始记录位置信息***/
int index = listviewAllProduct.getFirstVisiblePosition();
View v = listviewAllProduct.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listviewAllProduct.getPaddingTop());
/****记录结束***/

listviewAllProcutAdapter = new ListviewStockPlanProductAdapter(listStoreProduct, ctxt, mUiHandler);
listviewAllProduct.setAdapter(listviewAllProcutAdapter);
listviewAllProcutAdapter.notifyDataSetChanged();

/********恢复位置**********/
listviewAllProduct.setSelectionFromTop(index, top);

  

posted @ 2015-08-22 15:05  kunyashaw  阅读(837)  评论(0编辑  收藏  举报
回到顶部