ExpandableLinearLayout列表展开和收起功能

之前接触过几个类似的功能,展开和收起,都是自己在适配器里面判断,然后一步一步的来搞,特别容易出错和移位问题,今天看了这个ExpandableLinearLayout,把我们需要的功能全部包含进去了,所以自己收藏一下,以后遇到就直接使用:

首先,列表布局:

item_product.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/name" android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:paddingLeft="10dp"
        android:text="20" android:textSize="20dp"
        android:textColor="#000000"/>
</LinearLayout>

 然后就是主布局main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.expandable.android.MainActivity">

    <com.chaychan.library.ExpandableLinearLayout
       android:id="@+id/ell_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="vertical"
        app:defaultItemCount="2"
        app:useDefaultBottom="true"
        app:expandText="点击展开"
        app:hideText="点击收起"
        />


</android.support.constraint.ConstraintLayout>

 ,然后就是productBean文件:

public class ProductBean implements Serializable{
    private String img;
    private String name;

    public ProductBean(String imgs,String names){
    this.img =imgs;
        this.name =names;
    }


    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

最后主要功能界面实现MainActivity.java:

package com.expandable.android;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chaychan.library.ExpandableLinearLayout;

public class MainActivity extends AppCompatActivity {

    ExpandableLinearLayout ellProduct;
    private String[] imgs={"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3625142282,583054845&fm=117&gp=0.jpg",
    "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3559932268,3141934062&fm=11&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2222097050,638162430&fm=11&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=900168625,2024714394&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3007519245,1700294731&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4178855678,1297287114&fm=26&gp=0.jpg"};
    private String[] names={"名称01","名称02","名称03","名称04","名称05","名称06"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ellProduct = (ExpandableLinearLayout) findViewById(R.id.ell_product);
        ellProduct.removeAllViews();

        for (int i=0;i<5;i++){
            View view = View.inflate(this,R.layout.item_product,null);
        ProductBean productBean = new ProductBean(imgs[i],names[i]);
            ViewHolder viewHolder = new ViewHolder(view,productBean);
            viewHolder.refreshUI();
            ellProduct.addItem(view);
        }
    }

    class ViewHolder{
        ImageView img;
        TextView name;

        ProductBean productBean;

        public ViewHolder(View view,ProductBean productBean){
            this.productBean =productBean;
            img = (ImageView) view.findViewById(R.id.img);
            name = (TextView) view.findViewById(R.id.name);
        }

        private void refreshUI(){
            Glide.with(MainActivity.this).load(productBean.getImg()).placeholder(R.mipmap.ic_launcher)
                    .into(img);
            name.setText(productBean.getName());

        }
    }
}

 大功告成,就这么简单,这是我自己仿照别人写的一个,

行数、文字、图标都可以修改

最后就是调用jar包:

在项目最下面的build.gradle中添加:

allprojects {
    repositories {
        jcenter()
        maven{url 'https://jitpack.io'}
    }
}

在APP的build.gradle中添加:

compile 'com.github.chaychan:ExpandableLinearLayout:1.0.0'
compile 'com.github.bumptech.glide:glide:3.5.2'

相关推荐