Android中的二級列表(用數(shù)組方法實(shí)現(xiàn))

二級列表.png

1.在activity_main.xml文件中定義ExpandableListView控件

    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

2.定義組視圖layout_group.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <TextView
        android:id="@+id/tv_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="50dp"
        android:text="name"
        android:textSize="18sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:text="4/18"
        android:textSize="16sp" />
</RelativeLayout>

3.定義子視圖layout_child.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_centerVertical="true"
    android:padding="8dp"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="40dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textSize="16sp" />

    </LinearLayout>
</LinearLayout>

4.在MainActivity中的代碼

    private ExpandableListView lv;
    public String[] groupArray = new String[]{"武俠類", "驚悚/恐怖類", "影視小說"};
    public String[][] childArray = new String[][]{
            {"天龍八部", "倚天屠龍記", "神雕俠侶", "鹿鼎記"},
            {"盜墓筆記", "摸金校尉", "鬼吹燈"},
            {"花千骨", "步步驚心", "瑯琊榜", "追風(fēng)箏的人"}};
    public int[][] imgs = {
            {R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round},
            {R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round},
            {R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round,R.mipmap.ic_launcher_round}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        lv = (ExpandableListView) findViewById(R.id.elv);

        MyExpandListViewAdapter adapter = new MyExpandListViewAdapter(MainActivity.this, groupArray, childArray, imgs);
        lv.setAdapter(adapter);

        //二級列表子條目點(diǎn)擊事件
        lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this,"你點(diǎn)的是:"+childArray[groupPosition][childPosition],Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

5.適配器

public class MyExpandListViewAdapter extends BaseExpandableListAdapter {

    private Context context;
    private String[] groupArray;
    private String[][] childArray;
    private int[][] imgs;

    public MyExpandListViewAdapter(Context context, String[] groupArray, String[][] childArray, int[][] imgs) {
        this.context = context;
        this.groupArray = groupArray;
        this.childArray = childArray;
        this.imgs = imgs;
    }

    @Override
    public int getGroupCount() {
        return groupArray.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childArray[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(context).inflate( R.layout.layout_group, null);
        TextView groupName = view.findViewById(R.id.tv_group);
        groupName.setText(groupArray[groupPosition]);

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(context).inflate(R.layout.layout_child,null);
        TextView tv_name = view.findViewById(R.id.tv_name);
        ImageView img = view.findViewById(R.id.img);
        tv_name.setText(childArray[groupPosition][childPosition]);
        img.setImageResource(imgs[groupPosition][childPosition]);

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市豹障,隨后出現(xiàn)的幾起案子滴铅,更是在濱河造成了極大的恐慌,老刑警劉巖蒙兰,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件想邦,死亡現(xiàn)場離奇詭異雅采,居然都是意外死亡砾省,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門场勤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來戈锻,“玉大人,你說我怎么就攤上這事和媳「裨猓” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵窗价,是天一觀的道長如庭。 經(jīng)常有香客問我,道長撼港,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任骤竹,我火速辦了婚禮帝牡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蒙揣。我一直安慰自己靶溜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布懒震。 她就那樣靜靜地躺著罩息,像睡著了一般。 火紅的嫁衣襯著肌膚如雪个扰。 梳的紋絲不亂的頭發(fā)上瓷炮,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天,我揣著相機(jī)與錄音递宅,去河邊找鬼娘香。 笑死,一個胖子當(dāng)著我的面吹牛办龄,可吹牛的內(nèi)容都是我干的烘绽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼俐填,長吁一口氣:“原來是場噩夢啊……” “哼安接!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起英融,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤盏檐,失蹤者是張志新(化名)和其女友劉穎呀打,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糯笙,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贬丛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了给涕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豺憔。...
    茶點(diǎn)故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖够庙,靈堂內(nèi)的尸體忽然破棺而出恭应,到底是詐尸還是另有隱情,我是刑警寧澤耘眨,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布昼榛,位于F島的核電站,受9級特大地震影響剔难,放射性物質(zhì)發(fā)生泄漏胆屿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一偶宫、第九天 我趴在偏房一處隱蔽的房頂上張望非迹。 院中可真熱鬧,春花似錦纯趋、人聲如沸憎兽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纯命。三九已至,卻和暖如春痹栖,著一層夾襖步出監(jiān)牢的瞬間亿汞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工结耀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留留夜,地道東北人。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓图甜,卻偏偏與公主長得像碍粥,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子黑毅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容