【发布时间】:2020-05-25 16:14:03
【问题描述】:
我有可展开的列表项,我想添加带有列表的图像 我有每个子列表项的 3 个值(标题、图像链接、id)
这是代码显示列表标题和子列表标题 我想在 list 中显示 image_link 和 title,在 sublist 中显示 image_link、title、id 如何在适配器中传递 id、image? 请帮忙。
对不起,我的英语不好。
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>
【问题讨论】:
标签: android expandablelistview