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中的类,加一个变量,用来标记是否被选择

1
2
3
4
5
6
7
8
9
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中写一个方法,用来初始化所有“是否被选择”的标记位

1
2
3
4
5
6
7
8
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的回调

1
2
3
4
5
6
7
8
9
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对应的列表,这里很关键,请看下一步<br>  <br>          return true; } });

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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保持上次的位置

1
2
3
4
5
6
7
8
9
10
11
12
/****从这里开始记录位置信息***/
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 @   kunyashaw  阅读(868)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
回到顶部
点击右上角即可分享
微信分享提示