關(guān)于ExpandableListView的列子

喜歡顯示好友QQ那樣的列表,可以展開(kāi),可以收起,在android中,以往用的比較多的是listview,雖然可以實(shí)現(xiàn)列表的展示,但在某些情況下,我們還是希望用到可以分組并實(shí)現(xiàn)收縮的列表,那就要用到android的ExpandableListView,今天研究了一下這個(gè)的用法,也參考了很多資料動(dòng)手寫了一個(gè)小demo,實(shí)現(xiàn)了基本的功能,但界面優(yōu)化方面做得還不夠好,有待改進(jìn),素材采用了Q版三國(guó)殺武將的圖片,很有愛(ài)哈哈,下面直接上效果圖以及源代碼~! 

為企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)站優(yōu)化、營(yíng)銷型網(wǎng)站建設(shè)、競(jìng)價(jià)托管、品牌運(yùn)營(yíng)等營(yíng)銷獲客服務(wù)。成都創(chuàng)新互聯(lián)公司擁有網(wǎng)絡(luò)營(yíng)銷運(yùn)營(yíng)團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營(yíng)銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營(yíng)銷獲客難題,做到“讓獲客更簡(jiǎn)單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營(yíng)銷”三大難題,同時(shí)降低了營(yíng)銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!

關(guān)于 ExpandableListView 的列子         關(guān)于 ExpandableListView 的列子          關(guān)于 ExpandableListView 的列子

 

 

main.xml的布局很簡(jiǎn)單啦,只是一個(gè)ExpandableListView 就OK了

但值得簡(jiǎn)單說(shuō)下的是 android:cacheColorHint="#00000000",這個(gè)設(shè)置可以去除拖動(dòng)view時(shí)背景變成黑色的效果

android:listSelector="#00000000" ,可以去除選中時(shí)的***底色

 

關(guān)于 ExpandableListView 的列子

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6     <ExpandableListView 
 7         android:id="@+id/list"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent"
10         android:background="#ffffff"
11         android:cacheColorHint="#00000000"
12         android:listSelector="#00000000" 
13         >
14     </ExpandableListView> 
15 </LinearLayout>  
16 

關(guān)于 ExpandableListView 的列子

 

java代碼: 

 

關(guān)于 ExpandableListView 的列子

package com.eyu.activity_test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableList extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            //設(shè)置組視圖的圖片
            int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};
            //設(shè)置組視圖的顯示文字
            private String[] generalsTypes = new String[] { "魏", "蜀", "吳" };
            //子視圖顯示文字
            private String[][] generals = new String[][] {
                    { "夏侯惇", "甄姬", "許褚", "郭嘉", "司馬懿", "楊修" },
                    { "馬超", "張飛", "劉備", "諸葛亮", "黃月英", "趙云" },
                    { "呂蒙", "陸遜", "孫權(quán)", "周瑜", "孫尚香" }

            };
            //子視圖圖片
            public int[][] generallogos = new int[][] {
                    { R.drawable.xiahoudun, R.drawable.zhenji,
                            R.drawable.xuchu, R.drawable.guojia,
                            R.drawable.simayi, R.drawable.yangxiu },
                    { R.drawable.machao, R.drawable.zhangfei,
                            R.drawable.liubei, R.drawable.zhugeliang,
                            R.drawable.huangyueying, R.drawable.zhaoyun },
                    { R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,
                            R.drawable.zhouyu, R.drawable.sunshangxiang } };
            
            //自己定義一個(gè)獲得文字信息的方法
            TextView getTextView() {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.FILL_PARENT, 64);
                TextView textView = new TextView(
                        ExpandableList.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setPadding(36, 0, 0, 0);
                textView.setTextSize(20);
                textView.setTextColor(Color.BLACK);
                return textView;
            }

            
            //重寫ExpandableListAdapter中的各個(gè)方法
            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return generalsTypes.length;
            }

            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return generalsTypes[groupPosition];
            }

            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return generals[groupPosition].length;
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return generals[groupPosition][childPosition];
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LinearLayout ll = new LinearLayout(
                        ExpandableList.this);
                ll.setOrientation(0);
                ImageView logo = new ImageView(ExpandableList.this);
                logo.setImageResource(logos[groupPosition]);
                logo.setPadding(50, 0, 0, 0);
                ll.addView(logo);
                TextView textView = getTextView();
                textView.setTextColor(Color.BLACK);
                textView.setText(getGroup(groupPosition).toString());
                ll.addView(textView);

                return ll;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LinearLayout ll = new LinearLayout(
                        ExpandableList.this);
                ll.setOrientation(0);
                ImageView generallogo = new ImageView(
                        ExpandableList.this);
                generallogo
                        .setImageResource(generallogos[groupPosition][childPosition]);
                ll.addView(generallogo);
                TextView textView = getTextView();
                textView.setText(getChild(groupPosition, childPosition)
                        .toString());
                ll.addView(textView);
                return ll;
            }

            @Override
            public boolean isChildSelectable(int groupPosition,
                    int childPosition) {
                // TODO Auto-generated method stub
                return true;
            }

        };

        ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
        expandableListView.setAdapter(adapter);
        
        
        //設(shè)置item點(diǎn)擊的監(jiān)聽(tīng)器
        expandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                Toast.makeText(
                        ExpandableList.this,
                        "你點(diǎn)擊了" + adapter.getChild(groupPosition, childPosition),
                        Toast.LENGTH_SHORT).show();

                return false;
            }
        });
    }
}

分享名稱:關(guān)于ExpandableListView的列子
新聞來(lái)源:http://muchs.cn/article42/pipghc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管域名注冊(cè)、企業(yè)網(wǎng)站制作網(wǎng)站建設(shè)、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作