Android支持展开/收缩功能的列表控件

最近在做一个Rss阅读器,看了下别人做的阅读器中的列表控件可以展开、收缩,我就在网上搜索了一下。下面就我找到的一个小例子,给大家分享一下。

ActivityMain .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
package  study.com.android;
 
import  android.app.ExpandableListActivity;
import  android.os.Bundle;
import  android.view.ContextMenu;
import  android.view.MenuItem;
import  android.view.View;
import  android.view.ContextMenu.ContextMenuInfo;
import  android.widget.ExpandableListAdapter;
import  android.widget.ExpandableListView;
import  android.widget.TextView;
import  android.widget.Toast;
import  android.widget.ExpandableListView.ExpandableListContextMenuInfo;
 
public  class  ActivityMain  extends  ExpandableListActivity {
 
     private  ExpandableListAdapter mAdapter;
 
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         this .setTitle( "ExpandableList" );
         mAdapter =  new  MyExpandableListAdapter( this );
         setListAdapter(mAdapter);
         registerForContextMenu( this .getExpandableListView());
     }
 
     // 为列表的每一项创建上下文菜单(即长按后 呼出的菜单)
     @Override
     public  void  onCreateContextMenu(ContextMenu menu, View v,
             ContextMenuInfo menuInfo) {
         menu.setHeaderTitle( "ContexMenu" );
         menu.add( 0 0 0 "ContextMenu" );
     }
 
     // 单击上下文菜单后的逻辑
     @Override
     public  boolean  onContextItemSelected(MenuItem item) {
 
         ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
                 .getMenuInfo();
         String title = ((TextView) info.targetView).getText().toString();
 
         int  type = ExpandableListView
                 .getPackedPositionType(info.packedPosition);
         if  (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
 
             int  groupPos = ExpandableListView
                     .getPackedPositionGroup(info.packedPosition);
             int  childPos = ExpandableListView
                     .getPackedPositionChild(info.packedPosition);
             Toast.makeText(
                     this ,
                     title +  "-Group Index"  + groupPos +  "Child Index:"
                             + childPos, Toast.LENGTH_SHORT).show();
             return  true ;
         }
         return  false ;
     }
}

MyExpandableListAdapter.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
package  study.com.android;
 
import  android.content.Context;
import  android.view.Gravity;
import  android.view.View;
import  android.view.ViewGroup;
import  android.widget.AbsListView;
import  android.widget.BaseExpandableListAdapter;
import  android.widget.TextView;
 
public  class  MyExpandableListAdapter  extends  BaseExpandableListAdapter {
 
     private  Context mContext;
 
     // 父列表数据
     private  String[] groups = {  "group1" "group2" "group3" "group4" "" };
 
     // 子列表数据
     private  String[][] children = { {  "child1"  }, {  "child1" "child2"  },
             "child1" "child2" "child3"  },
             "child1" "child2" "child3" "child4"  }, };
 
     MyExpandableListAdapter(Context context) {
         mContext = context;
     }
 
     @Override
     public  Object getChild( int  groupPosition,  int  childPosition) {
         // TODO Auto-generated method stub
         return  children[groupPosition][childPosition];
     }
 
     @Override
     public  long  getChildId( int  groupPosition,  int  childPosition) {
         // TODO Auto-generated method stub
         return  childPosition;
     }
 
     // 取子列表中的某一项的view
     @Override
     public  View getChildView( int  groupPosition,  int  childPosition,
             boolean  isLastChild, View convertView, ViewGroup parent) {
         TextView textView = getGenericView();
         ;
         textView.setText(getChild(groupPosition, childPosition).toString());
         return  textView;
     }
 
     @Override
     public  int  getChildrenCount( int  groupPosition) {
         // TODO Auto-generated method stub
         return  children[groupPosition].length;
     }
 
     @Override
     public  Object getGroup( int  groupPosition) {
         return  groups[groupPosition];
     }
 
     @Override
     public  int  getGroupCount() {
         // TODO Auto-generated method stub
         return  groups.length;
     }
 
     @Override
     public  long  getGroupId( int  groupPosition) {
         // TODO Auto-generated method stub
         return  groupPosition;
     }
 
     @Override
     public  View getGroupView( int  groupPosition,  boolean  isExpanded,
             View convertView, ViewGroup parent) {
         TextView textView = getGenericView();
         textView.setText(getGroup(groupPosition).toString());
 
         return  textView;
     }
 
     @Override
     public  boolean  hasStableIds() {
         // TODO Auto-generated method stub
         return  true ;
     }
 
     @Override
     public  boolean  isChildSelectable( int  groupPosition,  int  childPosition) {
         // TODO Auto-generated method stub
         return  true ;
     }
 
     // 获取某一项的view的逻辑
     private  TextView getGenericView() {
         AbsListView.LayoutParams lp =  new  AbsListView.LayoutParams(
                 ViewGroup.LayoutParams.FILL_PARENT,  48 );
         TextView textView =  new  TextView(mContext);
         textView.setLayoutParams(lp);
         textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
         textView.setPadding( 32 0 0 0 );
         return  textView;
     }
 
}

运行的结果如下:

列表收缩

列表展开

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值