可扩展列表适配器 Imageview/Textview

ExpandableListAdapter Imageview/Textview

大家早上好,

我正在尝试在我的应用程序中设置一个 ExpandableListView。

我正在尝试制作下图:

我承认我很难做到这一点。

我尝试改编互联网教程但没有成功。

我的问题是:当我点击父 ImageView 时,没有任何反应。

MainActivity.java

package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;


public class MainActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;

public List<Integer> listDataHeader;
public HashMap<Integer, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jeux2);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.ExpandableListView);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
}

/*
 * Preparing the list data
 */
private void prepareListData() {
    listDataHeader = new ArrayList<Integer>();
    // Adding group data
    listDataHeader.add(R.drawable.logo_1);
    listDataHeader.add(R.drawable.logo_2);
    listDataHeader.add(R.drawable.logo_3);


    // Adding child data
    List<String> logo1 = new ArrayList<>();
    logo1.add("TEST 1");

    List<String> logo2 = new ArrayList<>();
    logo2.add("TEST 2");

    List<String> logo3 = new ArrayList<>();
    logo3.add("TEST 3");



    //List<Integer> noel_niv = new ArrayList<Integer>();
    //noel_niv.add(R.drawable.divers_niv2_cadenas);


    //List<Integer> hacker_niv = new ArrayList<Integer>();
    //hacker_niv.add(R.drawable.divers_niv2_cadenas);


    listDataChild = new HashMap<Integer, List<String>>();

    listDataChild.put(listDataHeader.get(0), logo1);
    listDataChild.put(listDataHeader.get(1), logo2);
    listDataChild.put(listDataHeader.get(2), logo3);

}
}

ExpandableListAdapter.java :

package com.evo.tab2escape;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<Integer> _listDataHeader; // Images
// child data in format of header , child
private HashMap<Integer, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<Integer> listDataHeader,
                             HashMap<Integer, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    String txt = (String) getChild(groupPosition, childPosition);


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView explications = (TextView) convertView.findViewById(R.id.explications);
    ImageView niveau = (ImageView) convertView.findViewById(R.id.niveau);


    if (txt.equals("TEST1"))
    {
        niveau.setImageResource(R.drawable.niv1);
        explications.setText("blablabalbabla 1");
    }
    if (txt.equals("TEST1"))
    {
        niveau.setImageResource(R.drawable.niv2);
        explications.setText("blablabalbabla 2");
    }
    if (txt.equals("TEST1"))
    {
        niveau.setImageResource(R.drawable.niv3);
        explications.setText("blablabalbabla 3");
    }



    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
    int imageId = this._listDataHeader.get(groupPosition);
    logo.setImageResource(imageId);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fond_main">
<TextView
    android:id="@+id/histo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\nVoir l'historique des parties     \n"
    android:gravity="bottom"
    android:layout_gravity="right"
    android:textStyle="bold">
</TextView>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#f3f3f3"/>
<ExpandableListView
    android:id="@+id/ExpandableListView"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
</ExpandableListView>
</LinearLayout>

list_group.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="wrap_content"
android:orientation="vertical">
<Space
    android:layout_width="match_parent"
    android:layout_height="20dp">
</Space>
<ImageView
    android:id="@+id/logo"
    android:layout_width="match_parent"
    android:scaleType="fitXY"
    android:layout_height="100dp"/>
</LinearLayout>

list_item.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="wrap_content"
android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:id="@+id/divers">
    <TextView
        android:id="@+id/explications"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1">
    </TextView>
    <ImageView
        android:id="@+id/niveau"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="3">
    </ImageView>
</LinearLayout>
</LinearLayout>

有人能告诉我为什么它不起作用吗? 提前致谢!

请使用 ExpandableListViewsetOnGroupClickListener 方法进行群组和
对 Childs 使用 ExpandableListViewsetOnChildClickListener 方法,因此当您单击 imageview 时,它会显示其 child.

你可以参考这个 kotlin 代码

private var previousGroupPosition : Int = -1
 expListView.setOnGroupClickListener { parent, _, groupPosition, _ ->
            parent.smoothScrollToPosition(groupPosition)
            if(expListView.isGroupExpanded(groupPosition)){
                expListView.collapseGroup(groupPosition)
                previousGroupPosition = -1
            }else{
                expListView.expandGroup(groupPosition)
                if(previousGroupPosition!=-1){
                    expListView.collapseGroup(previousGroupPosition)
                }
                previousGroupPosition=groupPosition
            }
            true
        }

expListView.setOnChildClickListener { _, _, groupPosition, childPosition, _ ->

            when (groupPosition) {
                when(childPosition){
                  //place your code according to child position
                }
         }
        false
}