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);

  

转载于:https://www.cnblogs.com/kunyashaw/p/4750296.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值