激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android ExpandableListView單選以及多選實現代碼

Android ExpandableListView單選以及多選實現代碼

2022-03-03 14:50不年少還青春 Android

這篇文章主要為大家詳細介紹了Android ExpandableListView單選以及多選的實現代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、概述

ExpandableListView是常用的一個控件,今天自己做了個小練習,主要需求是單選以及多選的實現,看似比較簡單,但是還是比較復雜,把代碼貼給大家,有這種需求的可以參考一下。

二、效果截圖

Android ExpandableListView單選以及多選實現代碼

三、實現過程

activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity" >
 
  <ExpandableListView
    android:id="@+id/exlistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:listSelector="@android:color/transparent" >
  </ExpandableListView>
 
</LinearLayout>

group_item.xml

?
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:descendantFocusability="blocksDescendants"
  android:padding="10dp" >
 
  <TextView
    android:id="@+id/id_group_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:padding="10dp"
    android:text="hao"
    android:textColor="@android:color/black"
    android:textIsSelectable="true"
    android:textSize="15sp" >
  </TextView>
 
  <CheckBox
    android:id="@+id/id_group_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" />
 
</RelativeLayout>

listview_item.xml

?
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:descendantFocusability="blocksDescendants"
  android:padding="10dp" >
 
  <TextView
    android:id="@+id/id_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:padding="10dp"
    android:layout_marginLeft="30dp"
    android:textColor="#55acac"
    android:textIsSelectable="true"
    android:textSize="15sp" >
  </TextView>
 
  <CheckBox
    android:id="@+id/id_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" />
 
</RelativeLayout>

 MainAcitivity.java

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
public class MainActivity extends Activity {
  private List<Map<String, String>> parentList = new ArrayList<Map<String, String>>();
  private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
  private ExpandableListView exListView;
  private Context context = this;
  private MyAdapter adapter;
 
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initData();
    setListener();
  }
 
  /**
   * 記錄正在選中的子listview的item條目 用hashset是為了去除重復值
   */
  private HashSet<String> hashSet;
 
  private void setListener()
  {
    exListView.setOnGroupExpandListener(new OnGroupExpandListener()
    {
       
      @Override
      public void onGroupExpand(int groupPosition)
      {
        //存取已選定的集合
        hashSet = new HashSet<String>();
      }
    });
    // ExpandableListView的Group的點擊事件
    exListView.setOnGroupClickListener(new OnGroupClickListener()
    {
 
      @Override
      public boolean onGroupClick(ExpandableListView parent, View v,
          int groupPosition, long id)
      {
        // 可以寫點擊后實現的功能
         
        return false;
      }
    });
    // ExpandableListView的child的點擊事件
 
    exListView.setOnChildClickListener(new OnChildClickListener()
    {
 
      @Override
      public boolean onChildClick(ExpandableListView parent, View v,
          int groupPosition, int childPosition, long id)
      {
        Map<String, String> map = childData.get(groupPosition).get(
            childPosition);
        String childChecked = map.get("isChecked");
        if ("No".equals(childChecked))
        {
          map.put("isChecked", "Yes");
          hashSet.add("選定" + childPosition);
        } else
        {
          map.put("isChecked", "No");
          if (hashSet.contains("選定" + childPosition))
          {
            hashSet.remove("選定" + childPosition);
          }
        }
        System.out.println("選定的長度==1" + hashSet.size());
        System.out.println("選定的長度==2"
            + childData.get(groupPosition).size());
        if (hashSet.size() == childData.get(groupPosition).size())
        {
          parentList.get(groupPosition).put("isGroupCheckd", "Yes");
        } else
        {
          parentList.get(groupPosition).put("isGroupCheckd", "No");
        }
        adapter.notifyDataSetChanged();
        return false;
      }
    });
  }
 
  // 初始化數據
  private void initData()
  {
    for (int i = 0; i < 10; i++)
    {
      Map<String, String> groupMap = new HashMap<String, String>();
      groupMap.put("groupText", "item" + i);
      groupMap.put("isGroupCheckd", "No");
      parentList.add(groupMap);
    }
    for (int i = 0; i < 10; i++)
    {
      List<Map<String, String>> list = new ArrayList<Map<String, String>>();
      for (int j = 0; j < 4; j++)
      {
        Map<String, String> map = new HashMap<String, String>();
        map.put("childItem", "childItem" + j);
        map.put("isChecked", "No");
        list.add(map);
      }
      childData.add(list);
    }
    adapter = new MyAdapter();
    exListView.setAdapter(adapter);
    exListView.expandGroup(0);
    hashSet = new HashSet<String>();
  }
 
  private void initView()
  {
    exListView = (ExpandableListView) findViewById(R.id.exlistview);
  }
 
  /**
   * 適配adapter
   */
 
  private class MyAdapter extends BaseExpandableListAdapter {
    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
      // TODO Auto-generated method stub
      return childData.get(groupPosition).get(childPosition);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
      // TODO Auto-generated method stub
      return childPosition;
    }
 
    @Override
    public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent)
    {
 
      ViewHolder holder = null;
      if (convertView == null)
      {
        holder = new ViewHolder();
        convertView = View.inflate(context, R.layout.listview_item,
            null);
        holder.childText = (TextView) convertView
            .findViewById(R.id.id_text);
        holder.childBox = (CheckBox) convertView
            .findViewById(R.id.id_checkbox);
        convertView.setTag(holder);
      } else
      {
        holder = (ViewHolder) convertView.getTag();
      }
      holder.childText.setText(childData.get(groupPosition)
          .get(childPosition).get("childItem"));
      String isChecked = childData.get(groupPosition).get(childPosition)
          .get("isChecked");
      if ("No".equals(isChecked))
      {
        holder.childBox.setChecked(false);
      } else
      {
        holder.childBox.setChecked(true);
      }
      return convertView;
    }
 
    @Override
    public int getChildrenCount(int groupPosition)
    {
      // TODO Auto-generated method stub
      return childData.get(groupPosition).size();
    }
 
    @Override
    public Object getGroup(int groupPosition)
    {
      return parentList.get(groupPosition);
    }
 
    @Override
    public int getGroupCount()
    {
      // TODO Auto-generated method stub
      return parentList.size();
    }
 
    @Override
    public long getGroupId(int groupPosition)
    {
      // TODO Auto-generated method stub
      return groupPosition;
    }
 
    @Override
    public View getGroupView(final int groupPosition,
        final boolean isExpanded, View convertView, ViewGroup parent)
    {
      ViewHolder holder = null;
      if (convertView == null)
      {
        holder = new ViewHolder();
        convertView = View.inflate(context, R.layout.group_item, null);
        holder.groupText = (TextView) convertView
            .findViewById(R.id.id_group_text);
        holder.groupBox = (CheckBox) convertView
            .findViewById(R.id.id_group_checkbox);
        convertView.setTag(holder);
      } else
      {
        holder = (ViewHolder) convertView.getTag();
      }
      holder.groupText.setText(parentList.get(groupPosition).get(
          "groupText"));
      final String isGroupCheckd = parentList.get(groupPosition).get(
          "isGroupCheckd");
 
      if ("No".equals(isGroupCheckd))
      {
        holder.groupBox.setChecked(false);
      } else
      {
        holder.groupBox.setChecked(true);
      }
     
      /*
       * groupListView的點擊事件
       */
      holder.groupBox.setOnClickListener(new OnClickListener()
      {
 
        @Override
        public void onClick(View v)
        {
          CheckBox groupBox = (CheckBox) v
              .findViewById(R.id.id_group_checkbox);
          if (!isExpanded)
          {
            //展開某個group view
            exListView.expandGroup(groupPosition);
          } else
          {
            //關閉某個group view
            exListView.collapseGroup(groupPosition);
          }
 
          if ("No".equals(isGroupCheckd))
          {
            exListView.expandGroup(groupPosition);
            groupBox.setChecked(true);
            parentList.get(groupPosition).put("isGroupCheckd",
                "Yes");
            List<Map<String, String>> list = childData
                .get(groupPosition);
            for (Map<String, String> map : list)
            {
              map.put("isChecked", "Yes");
            }
          } else
          {
            groupBox.setChecked(false);
            parentList.get(groupPosition)
                .put("isGroupCheckd", "No");
            List<Map<String, String>> list = childData
                .get(groupPosition);
            for (Map<String, String> map : list)
            {
              map.put("isChecked", "No");
            }
          }
          notifyDataSetChanged();
        }
      });
      return convertView;
    }
 
    @Override
    public boolean hasStableIds()
    {
      return true;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
      return true;
    }
 
  }
 
  private class ViewHolder {
    TextView groupText, childText;
    CheckBox groupBox, childBox;
  }
}

四、總結及注意點

1、設置CheckBox的點擊事件,而非別的

2、exListView.collapseGroup(groupPosition); 關閉正展開的子ListView.

這是demo地址,歡迎下載:

Demo下載地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/Hello201404/article/details/48546387

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人毛片免费看 | 韩国精品一区二区三区四区五区 | 成人午夜视频免费在线观看 | 曰批全过程40分钟免费视频多人 | 一区小视频 | 男女一边摸一边做羞羞视频免费 | 成人黄色小视频在线观看 | 久久久久久久久久久久久久国产 | 国内精品免费一区二区2001 | 中国毛片在线观看 | 天天操天天骑 | 欧美一级毛片欧美一级成人毛片 | 精品亚洲午夜久久久久91 | 日韩毛片一区二区三区 | 在线成人av观看 | 午夜精品视频免费观看 | 羞羞视频免费视频欧美 | 免费国产在线视频 | 成人综合一区二区 | 美女很黄很黄免费的 | 黄在线免费看 | 毛片大全在线观看 | 91久久夜色精品国产网站 | 亚洲精品3| 欧美中文字幕一区二区三区亚洲 | 18被视频免费观看视频 | 日本黄色一级视频 | 国产1区2| 在线成人影视 | 91精品国| 看中国一级毛片 | 欧美另类视频在线 | 久久网国产| 久久综合av | 中文字幕在线播放不卡 | 操操插插 | 欧美性受xxxx白人性爽 | 日本最新免费二区三区 | 精品国产一区二区三区在线观看 | 亚洲日韩精品欧美一区二区 | 欧美日韩成人一区二区 |