ExpandableLinearLayout list expand and collapse functions

I have been exposed to several similar functions before. Expanding and collapsing are all judged by myself in the adapter, and then I do it step by step, which is especially prone to errors and displacement problems. I saw this ExpandableLinearLayout today and include all the functions we need. Go in, so collect it yourself, and use it directly when you encounter it later:

First, the list layout:

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>

 Then there is the main layout 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="Click to expand"
        app:hideText="Click to hide"
        />


</android.support.constraint.ConstraintLayout>

 , and then the productBean file:

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;
    }
}

 

Finally, the main functional interface implements 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={"Name 01","Name 02","Name 03","Name 04","Name 05","Name 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());

        }
    }
}

 You're done, it's as simple as that, this is one I wrote by imitating others,

The number of lines, text, and icons can be modified

 

Finally, call the jar package:

Add in build.gradle at the bottom of the project:

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

 

Add in APP's build.gradle:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326187188&siteId=291194637