android secondary list secondary directory

New Main Interface 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/expandablelistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ExpandableListView>

</LinearLayout>

New Level 1 Directory 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"
    android:background="#FFFFFF">
    <TextView
        android:layout_width="48dp"
        android:layout_height="16dp"
        android:text="First level"
        android:textColor="#ff777777"
        android:textSize="16dp"
        />
</LinearLayout>

New secondary directory 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"
    android:gravity="center_vertical">
    <ImageView
        android:layout_width="29.2dp"
        android:layout_height="25.4dp"
        android:src="@mipmap/z_dialog1_dianshi"/>
    <TextView
        android:layout_marginLeft="10dp"
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="content"
        android:textSize="14dp"
        android:gravity="center_vertical"
        android:textColor="#ff777777"
        />

</LinearLayout>

Writing adapters

package com.rhkj.zhihuixue.activity.zhijizhiyi.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.rhkj.zhihuixue.R;

import java.util.List;

/**
 * @ClassName HotelEntityAdapter
 * @Description TODO
 * @Author ZhangXueTao
 * @Date 2020/10/27 14:44
 * @Version 1.0 Template
 */
public class HotelEntityAdapter  extends BaseExpandableListAdapter {
    List<String> mGroupList;//Class A List
    List<List<String>> mChildList;//second level List Be careful!Here is List There's one inside List<String>,You can write one for the actual project pojo Class to manage two levels of data


    public HotelEntityAdapter(List<String> groupList, List<List<String>> childList){
        mGroupList = groupList;
        mChildList = childList;

    }

    @Override
    public int getGroupCount() {//Return to first level List length
        return mGroupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {//Returns the specified groupPosition Second level List length
        return mChildList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {//Return to Level 1 List Contents in
        return mGroupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {//Return to Level 2 List Contents
        return mChildList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {//Return to Level 1 View Of id ensure id Only
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {//Return to Level 2 View Of id ensure id Only
        return groupPosition + childPosition;
    }

    /**
     * Indicates whether the subID and group ID are stable when changes are made to the underlying data
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     *  Return to first-level parent View
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.z_exlist_one_item, parent,false);
//        ((TextView)convertView).setText((String)getGroup(groupPosition));
        return convertView;
    }

    /**
     *  Return to Level 2 SubView
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_item, parent,false);
//        ((TextView)convertView).setText((String)getChild(groupPosition,childPosition));
        return convertView;
    }

    /**
     *  Is the subitem of the specified location optional
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

Activity

package com.rhkj.zhihuixue.activity.zhijizhiyi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.rhkj.zhihuixue.R;
import com.rhkj.zhihuixue.activity.zhijizhiyi.adapter.HotelEntityAdapter;
import com.rhkj.zhihuixue.base.app.ZBaseLandscapeActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity2 extends ZBaseLandscapeActivity {

    @BindView(R.id.expandablelistview)
     ExpandableListView expandablelistview;
    private HotelEntityAdapter mAdapter;



    @Override
    public int getLayoutId() {
        return R.layout.activity_main2;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        List<String> groupList = new ArrayList<>();
        groupList.add("one");
        groupList.add("two");
        groupList.add("three");

        List<List<String>> childList = new ArrayList<>();
        List<String> childList1 = new ArrayList<>();
        childList1.add("1");
        childList1.add("1");
        childList1.add("1");
        List<String> childList2 = new ArrayList<>();
        childList2.add("2");
        childList2.add("2");
        childList2.add("2");
        List<String> childList3 = new ArrayList<>();
        childList3.add("3");
        childList3.add("3");
        childList3.add("3");

        childList.add(childList1);
        childList.add(childList2);
        childList.add(childList3);

        mAdapter = new HotelEntityAdapter(groupList, childList);
        expandablelistview.setAdapter(mAdapter);

        expandablelistview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {//Level 1 Click Listen
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                //If you processed and consumed Click Back true,This is a basic precaution onTouch Return mechanism for event down or up delivery
                return false;
            }
        });

        expandablelistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {//Level 2 Click Listen
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                //If you processed and consumed Click Back true
                return false;
            }
        });
    }


}

Other Xml Properties

android:dividerHeight="20dp" sets the height of the item spacing, note that setting this spacing includes one and two levels

android:divider="@color/colorRed1" sets the first-level spacing color

android:childDivider="@color/colorGreen" sets the secondary spacing color

 

Android:child Indicator: A Drawable object that appears next to a sublist and can be an image

android:childIndicatorEnd: End constraint position for sublist item indicator

android:childIndicatorLeft: Left constraint position for sublist item indicator

android:childIndicatorRight: Constraint position to the right of sublist item indicator

android:childIndicatorStart: Start constraint position for sublist item indicator

 

android:groupIndicator: The Drawable object displayed next to the list of groups, which can be an image

android:indicatorEnd: End constraint position for group list item indicator

android:indicatorLeft: Left constraint position for group list item indicator

android:indicatorRight: Right constraint position for group list item indicator

android:indicatorStart: Start constraint position for group list item indicator

Expandable ListView 3 Adapter s Implementable

1. Extend BaseExpandableListAdpter to implement ExpandableAdapter.

2. Use SimpleExpandableListAdpater to wrap two List collections into ExpandableAdapter

3. Use the simpleCursorTreeAdapter

 

Some API details for ExpandableListView

mExpandableListView.collapseGroup(position); Collect secondary list of specified location groups

mExpandableListView.expandGroup(position); Expand the secondary list of specified location groups

mExpandableListView.isGroupExpanded(position); Whether the group at the specified location is expanded

mExpandableListView.setSelectedGroup(position); Set group at specified location to top

Change the position of the direction Icon

 

int width = getResources().getDisplayMetrics().widthPixels;
mExpandableListView.setIndicatorBounds(width - UnitConversionUtil.dip2px(this,40)
                , width - UnitConversionUtil.dip2px(this,15));//Set icon position

 

Some pits about click events

If you implement a Group click event in the adapter and want to call it back using a callback method (code below), then you will encounter a pit where the Group cannot expand and collapse, simply because the click event is consumed here and the click is not continuing to pass down, so the expansion and collapse of the underlying implementation will not execute

  /**
     *  Return to first-level parent View
     */
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        return convertView;
    }

So how can I solve it?

Method One

Instead of setOnClickListener(), because this consumes events, we use setOnTouchListener instead, with the following code:

      convertView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN){
                  //Implement your own interface callbacks
                    }
                    return false;
                }
            });        

Here's a little bit to watch for ACTION_DOWN needs to return false because the bottom layer is consumer ACTION_DOWN to unfold and retrieve...

Mode 2

Use collapseGroup() or expandGroup() methods to implement a groupPosition callback outside.

Posted by darrensw on Mon, 09 May 2022 19:18:09 +0300