多级列表展开(Expandabld)

2018-07-25  本文已影响0人  Summer_27d1

效果图:


expandable 需要继承BaseExpandableListView

首先布局中定义expandablelistView
xml
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wangye.expandabldlistviewtest.MainActivity">

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

</LinearLayout>

···
MainActivity中
...
package com.example.wangye.expandabldlistviewtest;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
expandableListView = (ExpandableListView) findViewById(R.id.exlist);
for (int i = 0; i < 5; i++) {
HashMap<String,String> map = new HashMap<>();
map.put("name","张三"+i);
map.put("age",(18+i)+"");
map.put("sex","男"+i);
map.put("phone","1800000000"+i);
arrayList.add(map);
}
expandableListView.setAdapter(new MyAdapter());
}

class MyAdapter extends BaseExpandableListAdapter{

    @Override
    public int getGroupCount() {
        return arrayList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return arrayList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return arrayList.get(i);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        TextView tx = new TextView(MainActivity.this);
        tx.setTextSize(20);
        tx.setTextColor(Color.RED);
        tx.setText(arrayList.get(i).get("name"));
        return tx;
    }

    @Override
    public View getChildView(final int i, int i1, boolean b, View view, ViewGroup viewGroup) {

        LinearLayout lin = new LinearLayout(MainActivity.this);
        lin.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        ));
        lin.setOrientation(LinearLayout.VERTICAL);

        TextView txAge = new TextView(MainActivity.this);
        txAge.setTextSize(16);
        txAge.setTextColor(Color.GREEN);
        txAge.setText(arrayList.get(i).get("age"));
        txAge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,arrayList.get(i).get("age"), Toast.LENGTH_SHORT).show();
            }
        });

        TextView txSex = new TextView(MainActivity.this);
        txSex.setTextSize(16);
        txSex.setTextColor(Color.GREEN);
        txSex.setText(arrayList.get(i).get("sex"));
        txSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,arrayList.get(i).get("sex"), Toast.LENGTH_SHORT).show();
            }
        });

        TextView txPhone = new TextView(MainActivity.this);
        txPhone.setTextSize(16);
        txPhone.setTextColor(Color.GREEN);
        txPhone.setText(arrayList.get(i).get("phone"));
        txPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,arrayList.get(i).get("phone"), Toast.LENGTH_SHORT).show();
            }
        });

        lin.addView(txAge);
        lin.addView(txSex);
        lin.addView(txPhone);

        return lin;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

}

...

上一篇下一篇

猜你喜欢

热点阅读