header 中包含多个图像和文本的 ExpandableListView

ExpandableListView with multiple images and text in header

我正尝试按照以下教程在我的应用程序上使用 ExpandableListView

https://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

但是,在我的例子中,我的每行 header 将有 2 个文本视图和 2 个图像视图,所以,查看教程中使用的 ExpandableListAdapter class,有这个方法:

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

并且在 getGroupView 方法中调用此方法,如下所示:

@Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        // rest of the code

在示例中,header 上只有一个 TextView,但在我的例子中,我有 2 个 TextView 和 2 个 ImageView,因此在这一行中:

String headerTitle = (String) getGroup(groupPosition);

我怎么知道哪个文字是哪个?图片也一样,如何区分一张图片或另一张图片??

您需要创建 POJO class 并创建该 pojo 的 ArrayList class。像这样

public class HeaderData {

    String title;
    String strImageURL;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getStrImageURL() {
        return strImageURL;
    }

    public void setStrImageURL(String strImageURL) {
        this.strImageURL = strImageURL;
    }
}

现在像这样创建列表private List<HeaderData> _listDataHeader;所以现在你只需要设置标题和图像URL你可以在绑定时轻松获得header。