Android中的二級列表(用數(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);

        //二級列表子條目點擊事件
        lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this,"你點的是:"+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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容