美文网首页
2020-05-20 二级列表条目显示功能Expandabl

2020-05-20 二级列表条目显示功能Expandabl

作者: xiaohuage | 来源:发表于2020-05-20 11:00 被阅读0次

    android系统控件ExpandableListView默认显示效果的实现:

    效果图见这个链接:系统默认显示效果

    主页面:

    package www.yalong.com.expandablelistview;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.widget.ExpandableListAdapter;

    import android.widget.ExpandableListView;

    public class MainActivity extends AppCompatActivity {

    ExpandableListView expanList;

        ExpandableListAdapter myExpanListAdapter;

        String[] groups =new String[]{"生活基本", "生活拓展", "其他"};

        String[][] childs =new String[][]{{"衣", "食", "住", "行"}, {"吃", "喝", "玩", "乐"}, {"读书", "写字"}};

        @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            initView();

        }

    private void initView() {

    expanList = findViewById(R.id.expand_list);

            myExpanListAdapter =new MyExpandableListAdapter(MainActivity.this, groups, childs);

            expanList.setAdapter(myExpanListAdapter);

            //  expanList.setGroupIndicator(null);//隐藏系统默认的左边的向下小图标

        }

    }

    适配器类:

    package www.yalong.com.expandablelistview;

    import android.content.Context;

    import android.database.DataSetObserver;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseExpandableListAdapter;

    import android.widget.ImageView;

    import android.widget.TextView;

    import java.util.zip.Inflater;

    /**

    * Created by Administrator on 2020/5/19 0019.

    */

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private    Contextcontext;

      private  String[]groups;

      private  String[][]childs;

        public MyExpandableListAdapter(Context context,String[] groups, String[][] childs) {

    this.context = context;

            this.groups = groups;

            this.childs = childs;

        }

    @Override

        public int getGroupCount() {

    return groups.length;

        }

    @Override

        public int getChildrenCount(int i) {

    return childs[i].length;

        }

    @Override

        public ObjectgetGroup(int i) {

    return groups[i];

        }

    @Override

        public ObjectgetChild(int i, int i1) {

    return childs[i][i1];

        }

    @Override

        public long getGroupId(int i) {

    return i;

        }

    @Override

        public long getChildId(int i, int i1) {

    return i1;

        }

    @Override

        public boolean hasStableIds() {

    return true;

        }

    @Override

        public ViewgetGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    GroupViewHolder groupViewHolder;

            if (view ==null) {

    view = LayoutInflater.from(context).inflate(R.layout.layout_groups, viewGroup, false);

                groupViewHolder =new GroupViewHolder();

                groupViewHolder.imageView=view.findViewById(R.id.parent_img);

                groupViewHolder.textView = view.findViewById(R.id.parent_txt);

                view.setTag(groupViewHolder);

            }else {

    groupViewHolder = (GroupViewHolder) view.getTag();

            }

    groupViewHolder.textView.setText(groups[i]);

    //以下注释掉了代码可以实现ExpandableListView展开/折叠两种状态不同的图片显示

    //        if (b){

    //

    //            groupViewHolder.imageView.setImageResource(R.drawable.icon_arrow_up);

    //

    //

    //

    //        }else {

    //

    //            groupViewHolder.imageView.setImageResource(R.drawable.icon_arrow_right);

    //

    //

    //        }

            return view;

        }

    @Override

        public ViewgetChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

    //b 为true :选中

            ChildViewHolder childViewHolder;

            if (view ==null) {

    view = LayoutInflater.from(context).inflate(R.layout.layout_childs, viewGroup, false);

                childViewHolder =new ChildViewHolder();

                childViewHolder.textView = view.findViewById(R.id.child_txt);

                view.setTag(childViewHolder);

            }else {

    childViewHolder = (ChildViewHolder) view.getTag();

            }

    childViewHolder.textView.setText(childs[i][i1]);

            return view;

        }

    @Override

        public boolean isChildSelectable(int i, int i1) {

    return true;

        }

    static  class GroupViewHolder {

    ImageView imageView;

            TextView textView;

        }

    static  class ChildViewHolder {

    TextView textView;

        }

    }

    xml文件:

    activity_main.xml

    <?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"

        tools:context="www.yalong.com.expandablelistview.MainActivity">

    <ExpandableListView

        android:id="@+id/expand_list"

        android:childDivider="@color/colorPrimaryDark"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

    </LinearLayout>

    layout_groups.xml

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent">

    <LinearLayout

        android:layout_marginTop="10dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center_vertical">

    <TextView

            android:id="@+id/parent_txt"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:gravity="start"

            android:paddingLeft="100dp"

            android:textColor="@color/colorAccent"

            android:textSize="20sp" />

    <android.support.v4.widget.Space

        android:layout_width="0dp"

        android:layout_height="1dp"

        android:layout_weight="1"

        />

    <ImageView

            android:id="@+id/parent_img"

            android:layout_width="30dp"

            android:layout_height="30dp"

            android:layout_marginLeft="20dp"

            android:layout_marginRight="40dp"

            />

    </LinearLayout>

    </LinearLayout>


    layout_childs.xml

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent">

    <TextView

            android:id="@+id/child_txt"

            android:textColor="@color/colorPrimary"

            android:textSize="15sp"

            android:gravity="start"

            android:paddingLeft="100dp"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />

    </LinearLayout>

    目标:自定义实现去掉系统控件左边的向下小图标,右边显示自己设置的图标,默认不展开显示向右图标,展开显示向上图标

    放开以上被注解的代码即可,效果视屏打开链接可感受:自定义显示效果

    相关文章

      网友评论

          本文标题:2020-05-20 二级列表条目显示功能Expandabl

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