会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 57自学网———旗下安卓自学网站专注Android系统开发教程!
当前位置:主页 > 界面设计 > ListView > 正文

android学习笔记22:可展开的ListView

时间:2016-08-02 10:31 来源:网络整理 作者:安卓自学网 阅读:

有时在写程序时,我们希望一个listview能展开其下的子类目,在android中可以通过使用ExpandAbleListView来实现,只需要在代码里为ExpandAbleListView设置一个ExpandAbleLIstAdapter的数据源即可。

\



 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ExpandableListView  
    android:id="@+id/list"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:childIndicator="@drawable/icon"  
    ></ExpandableListView> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<ExpandableListView
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:childIndicator="@drawable/icon"
 ></ExpandableListView>
</LinearLayout>


public class ExpandableListViewTest extends Activity 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        //创建一个BaseExpandableListAdapter对象  
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() 
        { 
            int[] logos = new int[] 
            { 
                R.drawable.sunyz_1,  
                R.drawable.sunyz_6, 
                R.drawable.sunyz_7 
            }; 
            private String[] CDs = new String[] 
                { "风筝", "完美的一天", "是时候"}; 
            private String[][] songs = new String[][] 
            {  
                { "绿光", "不是真的爱我", "爱情字典", "练习" }, 
                { "Honey Honey", "心愿", "明天晴天", "隐形人" }, 
                { "愚人的国度", "是时候" , "世说心语" } 
            }; 
            //获取指定组位置、指定子列表项处的子列表项数据  
            @Override 
            public Object getChild(int groupPosition, int childPosition) 
            { 
                return songs[groupPosition][childPosition]; 
            } 
            @Override 
            public long getChildId(int groupPosition, int childPosition) 
            { 
                return childPosition; 
            } 
            @Override 
            public int getChildrenCount(int groupPosition) 
            { 
                return songs[groupPosition].length; 
            } 
            private TextView getTextView() 
            { 
                //用于实现条目的虚拟列表的基类. 这里的列表没有空间的定义。 例如,该类的子类可以以网格的形式、走马灯的形式显示,或者作为堆栈等等  
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams( 
                        ViewGroup.LayoutParams.FILL_PARENT, 64);  //设置宽和高   
                TextView textView = new TextView(ExpandableListViewTest.this); 
                textView.setLayoutParams(lp); 
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
                textView.setPadding(36, 0, 0, 0); 
                textView.setTextSize(20); 
                return textView; 
            } 
            //该方法决定每个子选项的外观  
            @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 Object getGroup(int groupPosition) 
            { 
                return CDs[groupPosition]; 
            } 
            @Override 
            public int getGroupCount() 
            { 
                return CDs.length; 
            } 
            @Override 
            public long getGroupId(int groupPosition) 
            { 
                return groupPosition; 
            } 
            //该方法决定每个组选项的外观  
            @Override 
            public View getGroupView(int groupPosition, boolean isExpanded, 
                    View convertView, ViewGroup parent) 
            { 
                LinearLayout ll = new LinearLayout(ExpandableListViewTest.this); 
                ll.setOrientation(0); 
                ImageView logo = new ImageView(ExpandableListViewTest.this); 
                logo.setImageResource(logos[groupPosition]); 
                ll.addView(logo); 
                TextView textView = getTextView(); 
                textView.setText(getGroup(groupPosition).toString());                
                ll.addView(textView);            
                return ll; 
            } 
            @Override 
            public boolean isChildSelectable(int groupPosition, int childPosition) 
            { 
                return true; 
            } 
            @Override 
            public boolean hasStableIds() 
            { 
                return true; 
            } 
        }; 
        ExpandableListView expandListView = (ExpandableListView) 
            findViewById(R.id.list); 
        expandListView.setAdapter(adapter); 
    } 


摘自 snoopy的专栏

(责任编辑:www.57zxw.com)

顶一下
(1)
100%
踩一下
(0)
0%