服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发实现ListView点击展开收起效果示例

Android开发实现ListView点击展开收起效果示例

2022-10-09 15:37水中鱼之1999 Android

这篇文章主要介绍了Android开发实现ListView点击展开收起效果,结合实例形式分析了Android ListView控件的布局及事件响应相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android开发实现ListView点击展开收起效果。分享给大家供大家参考,具体如下:

废话不说先上效果:

Android开发实现ListView点击展开收起效果示例

实际上这是采用一个ExpandableListView实现的

布局文件很简单:

?
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
<?xml version="1.0" encoding="utf-8"?>
<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" >
  <!--提示框-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="请选择您的类型:"
    android:textSize="30sp"
    android:textColor="#ffffffff"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#ff000000"/>
  <!--定义一个ExpandableListView组件-->
  <ExpandableListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </ExpandableListView>
</LinearLayout>

然后就是具体实现:

这里主要是添加几个必须的属性 大多数方法不用重写

参考我代码中的位置稍加改动就行

?
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
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //创建一个ExpandableListAdapter对象
    final ExpandableListAdapter adapter = new ExpandableListAdapter() {
      int[] logos = new int[]{
          R.drawable.human1st,
          R.drawable.human1st,
          R.drawable.human2nd,
          R.drawable.human3rd
      };
      private String[] humanTypes = new String[]{
          "不是人","聪明人","普通人","我这样的人"
      };
      private String[][] humans = new String[][]{
          {"上仙","大神","荷兰猪"},
          {"超人","一般聪明人","假的聪明人"},
          {"努力的人","快乐的普通人","苦逼的普通人"},
          {"天才","傻逼","蠢萌"}
      };
      //获得制定组的位置、指定子列表项处的字列表项数据
      private TextView getTextView(){
        AbsListView.LayoutParams layoutParams =
            new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,164);
        TextView textView = new TextView(MainActivity.this);
        textView.setLayoutParams(layoutParams);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36,0,0,0);
        textView.setTextSize(30);
        return textView;
      }
      @Override
      public void registerDataSetObserver(DataSetObserver observer) {
      }
      @Override
      public void unregisterDataSetObserver(DataSetObserver observer) {
      }
      @Override
      public int getGroupCount() {
        return humanTypes.length;
      }
      @Override
      public int getChildrenCount(int groupPosition) {
        return humans[groupPosition].length;
      }
      //获取制定组位置处的组数据
      @Override
      public Object getGroup(int groupPosition) {
        return humanTypes[groupPosition];
      }
      @Override
      public Object getChild(int groupPosition, int childPosition) {
        return humans[groupPosition][childPosition];
      }
      @Override
      public long getGroupId(int groupPosition) {
        return groupPosition;
      }
      @Override
      public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
      }
      @Override
      public boolean hasStableIds() {
        return true;
      }
      //该方法决定每个组选项的外观
      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout linearLayout = new LinearLayout(MainActivity.this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        ImageView logo = new ImageView(MainActivity.this);
        logo.setImageResource(logos[groupPosition]);
        linearLayout.addView(logo);
        TextView textView = getTextView();
        textView.setText(getGroup(groupPosition).toString());
        linearLayout.addView(textView);
//        linearLayout.setMinimumHeight(50);
        return linearLayout;
      }
      @Override
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getTextView();
        textView.setText(getChild(groupPosition,childPosition).toString());
        return textView;
      }
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
      }
      @Override
      public boolean areAllItemsEnabled() {
        return false;
      }
      @Override
      public boolean isEmpty() {
        return false;
      }
      @Override
      public void onGroupExpanded(int groupPosition) {
      }
      @Override
      public void onGroupCollapsed(int groupPosition) {
      }
      @Override
      public long getCombinedChildId(long groupId, long childId) {
        return 0;
      }
      @Override
      public long getCombinedGroupId(long groupId) {
        return 0;
      }
    };
    ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
      @Override
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Toast.makeText(MainActivity.this,"你是一个:" +
            adapter.getChild(groupPosition,childPosition),Toast.LENGTH_SHORT).show();
        return true;
      }
    });
    expandableListView.setAdapter(adapter);
  }
}

希望本文所述对大家Android程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_43377749/article/details/84328130

延伸 · 阅读

精彩推荐