可扩展的列表组件

ExpandableListView(可扩展列表组件).ExpandableListView所显示的列表项应该有由ExpandableListAdapter提供。
实现ExpandableListAdapter的三种常见方式:

  • 扩展BaseExpandableListAdapter 实现ExpandableListAdapter。
  • 使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter。
  • 使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter。
    布局如下:
布局简单,后面的LinearLayout,TextView和ImageView是直接在代码中获取的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.expandablelistview.MainActivity">
   <ExpandableListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:childDivider="@color/colorAccent"
       android:id="@+id/elistview"
       android:dividerHeight="2dp">

   </ExpandableListView>

</LinearLayout>
childDivider是指定各子列表项之间的分隔条

这里写图片描述

package com.example.expandablelistview;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MyExpandableListView extends BaseExpandableListAdapter {
//这里的Context是为了给MainActivity传数据的
    private Context context;

    public MyExpandableListView(Context context) {
        this.context = context;
    }

    int[] image = new int[]{R.mipmap.kemic , R.mipmap.d, R.mipmap.abc};
    String[] armTypes = new String[]{"科比", "霍祁君", "全智贤"};
    String[][] arms = new String[][]{
            {"凌晨四点的洛杉矶", "你的后仰没有酒", "我却醉的像条狗", "我的青春"},
            {"羊驼王子", "霍霍", "需要人陪", "一首简单的歌"},
            {"人生大赢家","气质女神","柔和","灵气"}
    };
    //获取指定组位置,指定子列表项处的子列表项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arms[groupPosition][childPosition];
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return arms[groupPosition].length;
    }


    private TextView getTextView(){
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
        //注意这里context和this的区别
        TextView textView = new TextView(context);
        textView.setLayoutParams(layoutParams);
        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 armTypes[groupPosition];
    }
    @Override
    public int getGroupCount() {
        return armTypes.length;
    }

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




    //该方法决定每个组选项的外观
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout linearLayout=new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        ImageView  images = new ImageView(context);
        images.setImageResource(image[groupPosition]);
        images.setMaxWidth(100);
        linearLayout.addView(images);
        TextView textView = getTextView();
        textView.setText(getGroup(groupPosition).toString());
        linearLayout.addView(textView);

        return linearLayout;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}
package com.example.expandablelistview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView= (ExpandableListView) findViewById(R.id.elistview);
        MyExpandableListView expandableListViewAdapter=new MyExpandableListView(this);
        expandableListView.setAdapter(expandableListViewAdapter);
    }
}

效果图如下:
这里写图片描述

这里写图片描述

所谓的可扩展就是点击图片时会显示刚开始隐藏在图片下的文字信息。当点击第一张图片时:
这里写图片描述

点击第二张:
这里写图片描述

点击第三张:
这里写图片描述

注意:context,this,MainActivity.this,getActivity()之间的用法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值