Android 带有图像的扩展列表视图

Android Expendable listview with image

我有可扩展的列表项我想添加带有列表的图像 我有每个子列表项的 3 个值 (title,image_link,id)

这是显示列表标题和子列表标题的代码 我想在 list 中显示 image_link 和 title ,在子列表中显示 image_link, title, id 如何在适配器中传递 id、图像? 请帮忙

抱歉我的英语不好。

expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListDetail = ExpandableListViewData.getData();
        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
        expandableListAdapter = new CustomExpandableListview(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);


public class CustomExpandableListview extends BaseExpandableListAdapter {

        private Context context;
        private List<String> expandableListTitle;
        private HashMap<String, List<String>> expandableListDetail;

        public CustomExpandableListview(Context context, List<String>
                expandableListTitle,HashMap<String, List<String>> expandableListDetail) {
            this.context = context;
            this.expandableListTitle = expandableListTitle;
            this.expandableListDetail = expandableListDetail;
        }

        @Override
        public Object getChild(int listPosition, int expandedListPosition) {
            return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                    .get(expandedListPosition);
        }

        @Override
        public long getChildId(int listPosition, int expandedListPosition) {
            return expandedListPosition;
        }

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

        @Override
        public View getChildView(int listPosition, final int expandedListPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            final String expandedListText = (String) getChild(listPosition, expandedListPosition);
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.list_row, null);
            }
            TextView expandedListTextView = (TextView) convertView
                    .findViewById(R.id.title);
            expandedListTextView.setText(expandedListText);
            return convertView;
        }

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

        @Override
        public int getChildrenCount(int listPosition) {
            return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                    .size();
        }

        @Override
        public Object getGroup(int listPosition) {
            return this.expandableListTitle.get(listPosition);
        }

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

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

        @Override
        public View getGroupView(int listPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            String listTitle = (String) getGroup(listPosition);
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.context.
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.list_group, null);
            }
            TextView listTitleTextView = (TextView) convertView
                    .findViewById(R.id.listTitle);
            listTitleTextView.setTypeface(null, Typeface.BOLD);
            listTitleTextView.setText(listTitle);
            return convertView;
        }
    }

    public static class ExpandableListViewData {
        public static HashMap<String, List<String>> getData() {
            HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
            List<String> Xbox = new ArrayList<String>();
            Xbox.add("Halo");
            Xbox.add("Gears of War");
            List<String> Ps4 = new ArrayList<String>();
            Ps4.add("God of War");
            Ps4.add("Spiderman");

            List<String> Switch = new ArrayList<String>();
            Switch.add("Let's Go Pikachu/Eevee");
            Switch.add("Zelda Breath of the Wild");
            Switch.add("Splatoon");

            expandableListDetail.put("Xbox Exclusives", Xbox);
            expandableListDetail.put("Ps4 Exclusives", Ps4);
            expandableListDetail.put("Switch Exclusives", Switch);
            return expandableListDetail;
        }
    }


list_group.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"/>
    <TextView
        android:id="@+id/listTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="@android:color/black"
        android:paddingTop="10dp"
        android:paddingBottom="10dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


list_row.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingStart="8dp"
    android:background="@drawable/list_row"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView
    android:focusable="false"
        android:id="@+id/list_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:srcCompat="@mipmap/logo" />

    <TextView
    android:focusable="false"
    android:layout_gravity="center"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#fff"
        android:layout_marginLeft="10dp"/>
        </LinearLayout>



创建自定义对象并将其传递给适配器

ListItem.java

public class ListItem {
private String title;
private String imageUrl;
private ArrayList<Items> items;

public ListItem(String title, String imageUrl, ArrayList<Items> items) {
    this.title = title;
    this.imageUrl = imageUrl;
    this.items = items;
}

public String getTitle() {
    return title;
}

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

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public ArrayList<Items> getItems() {
    return items;
}

public void setItems(ArrayList<Items> items) {
    this.items = items;
}

}

Items.java

public class Items {
private String title;
private String imageUrl;
private Integer id;

public Items(String title, String imageUrl, Integer id) {
    this.title = title;
    this.imageUrl = imageUrl;
    this.id = id;
}

public String getTitle() {
    return title;
}

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

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}
}

CustomExpandableListView.java

public class CustomExpandableListView extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ListItem> list;

public CustomExpandableListView(Context context, ArrayList<ListItem> list) {
    this.context = context;
    this.list = list;
}

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

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

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.list.get(groupPosition).getItems().get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ListItem item = (ListItem) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert layoutInflater != null;
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = convertView
            .findViewById(R.id.listTitle);
    ImageView listImageView = convertView.findViewById(R.id.groupImageView);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(item.getTitle());
    Glide.with(context)
            .load(item.getImageUrl())
            .into(listImageView);
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final Items item = (Items) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert layoutInflater != null;
        convertView = layoutInflater.inflate(R.layout.list_row, null);
    }
    TextView expandedListTextView = convertView
            .findViewById(R.id.title);
    ImageView imageView = convertView.findViewById(R.id.child_image);
    expandedListTextView.setText(item.getTitle());
    Glide.with(context)
            .load(item.getImageUrl())
            .into(imageView);
    return convertView;
}

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

ExpandableListView.java

public class ExpandableListViewData {
public static ArrayList<ListItem> getData() {

    ArrayList<ListItem> list = new ArrayList<>();
    ArrayList<Items> xBoxItems = new ArrayList<>();
    ArrayList<Items> ps4Items = new ArrayList<>();
    ArrayList<Items> switchItems = new ArrayList<>();
    xBoxItems.add(
            new Items(
                    "Halo",
                    "https://compass-ssl.xbox.com/assets/a5/f4/a5f4cc7b-974b-48e4-b976-f57b3bb97665.jpg?n=Halo-Franchise-2019_Super-Hero-1400_MCC_1920x1080_02.jpg",
                    1
            )
    );
    xBoxItems.add(new Items(
            "Gods of War",
            "https://i.pinimg.com/474x/39/c0/b3/39c0b3954de638a885b28180aeaedf3d.jpg",
            1
    ));
    list.add(
            new ListItem(
                    "Xbox Exclusive",
                    "https://mylatestnews.org/wp-content/uploads/2018/09/XboxOneS_CnsleCntrllr_Hrz_FrntTlt_TransBG_RGB.0.jpg",
                    xBoxItems
            )
    );
    ps4Items.add(

            new Items(
                    "God of War",
                    "https://i.pinimg.com/474x/39/c0/b3/39c0b3954de638a885b28180aeaedf3d.jpg",
                    2
            )
    );
    ps4Items.add(new Items(
            "Spiderman",
            "https://images.theconversation.com/files/175539/original/file-20170626-315-1h7k01d.jpg",
            3
    ));
    list.add(
            new ListItem(
                    "Ps4 Exclusives",
                    "https://specials-images.forbesimg.com/imageserve/5e054fc425ab5d0007cf52ca/960x0.jpg",
                    ps4Items
            )
    );

    switchItems.add(
            new Items(
                    "Let's Go Pikachu/Eevee",
                    "https://i.pinimg.com/474x/39/c0/b3/39c0b3954de638a885b28180aeaedf3d.jpg",
                    1
            )

    );
    switchItems.add(new Items(
            "Zelda Breath of the Wild",
            "https://images.theconversation.com/files/175539/original/file-20170626-315-1h7k01d.jpg",
            1
    ));

    switchItems.add(
            new Items(
                    "Splatoon",
                    "https://images.theconversation.com/files/175539/original/file-20170626-315-1h7k01d.jpg",
                    1
            ));

    list.add(
            new ListItem(
                    "Switch Exclusives",
                    "https://www.nintendo.com/content/dam/noa/en_US/hardware/switch/nintendo-switch-new-package/gallery/bundle_color_portable%20(1).jpg",
                    switchItems
            )
    );
    return list;
}
}

MainActivity.java

一次只能扩展一个
public class MainActivity extends AppCompatActivity {
private int lastPosition = -1;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ExpandableListView expandableListView = findViewById(R.id.expandableListView);
    ArrayList<ListItem> list = ExpandableListViewData.getData();
    CustomExpandableListView expandableListAdapter = new CustomExpandableListView(this, list);
    expandableListView.setAdapter(expandableListAdapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            if (lastPosition != -1
                    && groupPosition != lastPosition) {
                expandableListView.collapseGroup(lastPosition);
            }
            lastPosition = groupPosition;
        }
    });

}
}

list_group.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="wrap_content"
android:orientation="horizontal">

<ImageView
 android:id="@+id/groupImageView"
 android:layout_width="30dp"
 android:layout_height="30dp"
 android:layout_marginStart="40dp"
 android:layout_marginLeft="40dp"
 tools:src="@mipmap/ic_launcher" />

<TextView
 android:id="@+id/listTitle"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:gravity="center_vertical"
 tools:text="Title"
 android:paddingStart="20dp"
 android:paddingEnd="20dp"
 android:textColor="@android:color/black"
 tools:ignore="RtlSymmetry" />
</LinearLayout>

list_row.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="35dp"
android:orientation="horizontal"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
tools:ignore="RtlSymmetry">

<ImageView
android:id="@+id/child_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:focusable="false"
tools:srcCompat="@mipmap/ic_launcher_round" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:focusable="false"
android:textColor="#000000"
tools:text="Title" />
</LinearLayout>

build.gradle(app)

中添加
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

AndroidManifest.xml

中添加这个
<uses-permission android:name="android.permission.INTERNET"/>