美文网首页
Android中的二级列表(用数组方法实现)

Android中的二级列表(用数组方法实现)

作者: 我挺平凡 | 来源:发表于2019-03-16 22:28 被阅读0次
二级列表.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[][]{
            {"天龙八部", "倚天屠龙记", "神雕侠侣", "鹿鼎记"},
            {"盗墓笔记", "摸金校尉", "鬼吹灯"},
            {"花千骨", "步步惊心", "琅琊榜", "追风筝的人"}};
    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;
    }

相关文章

网友评论

      本文标题:Android中的二级列表(用数组方法实现)

      本文链接:https://www.haomeiwen.com/subject/rmpemqtx.html