我要实现的是将GridView放在Expandable List中。 当前,我的代码有两个适配器,分别称为“ CustomListAdapter”和“ SiteAdapter”,它们将GridView连接到可扩展列表,而“ SiteAdapter”将数据连接到GridView。 到目前为止,我们已经构建的代码已经很好地连接到GridView的Expandable List。但是,gridView内部的数据似乎已连接到gridView(查看gridview中的项目并单击适当的链接),但是组成gridview的内部设计的图像和文本没有出现在屏幕上。 我已经在名为“ siteAdapter”的适配器代码中使用了setImage和setText,但是我不知道为什么在屏幕上未正确配置它。
1] SiteAdapter.java(链接GridView和项目的siteAdapter代码)
public class SiteAdapter extends ArrayAdapter<Site> {
private Site currentSite;
public SiteAdapter(Context context, ArrayList<Site> sites) {
super(context, 0, sites);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
currentSite = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setBackground(new ShapeDrawable(new OvalShape()));
imageView.setClipToOutline(true);
imageView.setImageBitmap(bitmap);
TextView name = (TextView) listItemView.findViewById(R.id.name);
name.setText(currentSite.getMsite_name());
return listItemView;
}
2] CustomListAdapter.java(链接ExpandableList和GridView)
public class CustomListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<String> arrayGroup;
private HashMap<String,ArrayList<Site>> arrayChild;
public CustomListAdapter(Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<Site>> arrayChild){
super();
this.mContext = context;
this.arrayGroup = arrayGroup;
this.arrayChild = arrayChild;
}
@Override
public int getGroupCount() {
return arrayGroup.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return arrayChild.get(arrayGroup.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return arrayGroup.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return arrayChild.get(arrayGroup.get(groupPosition)).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) {
String groupName = arrayGroup.get(groupPosition);
View v = convertView;
if(v==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.parent_listview_title, null);
}
TextView textGroup = (TextView) v.findViewById(R.id.title_site_list);
textGroup.setText(groupName);
return v;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
String groupName = arrayGroup.get(groupPosition);
View v = convertView;
if(v == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.child_gridview_item, null);
}
SiteAdapter siteAdapter = new SiteAdapter(mContext,arrayChild.get(groupName));
GridView gridView = (GridView) v.findViewById(R.id.gridView);
gridView.setAdapter(siteAdapter);
return v;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
3] site_list.xml
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/parentList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
4] parent_listview_title.xml(设计可扩展列表的父列表的代码)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title_site_list"
android:layout_width="wrap_content"
android:textSize="40sp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:textColor="#000000"
android:layout_marginLeft="16dp"
android:fontFamily="@font/bm_dohyeon"
android:textStyle="bold"/>
</LinearLayout>
5] child_gridview_item.xml(设计可扩展列表的子列表的代码)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<ViewSwitcher
android:id="@+id/visibility"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="20dp">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#FFFFFF"
/>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FFFFFF"
tools:context=".home"
android:id="@+id/gridView"
android:columnWidth="96dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:horizontalSpacing="4dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</ViewSwitcher>
</LinearLayout>
6] list_item.xml(设计gridview内容的代码,即Expandable List的子列表)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/image"
android:scaleType="centerCrop"
tools:src="@mipmap/ic_launcher"
android:layout_gravity="center"
/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:id="@+id/name"
tools:text="Gistsite"
android:textAppearance="?android:textAppearanceMedium"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textSize="4sp"
/>
</LinearLayout>
7] Site.java
public class Site {
private String msite_name;
private String msite_url;
private String msite_urlF;
private String msite_urlY;
private String msite_urlB;
private String msite_urlW;
private String msite_urlI;
private int msite_imagesource;
public Site(String name, String url, int imagesource){
msite_name = name;
msite_url = url;
msite_imagesource = imagesource;
}
public Site(String name, String urlf, String urly, int imagesource){
msite_name = name;
msite_urlF = urlf;
msite_urlY = urly;
msite_imagesource = imagesource;
}
public Site(String name, String urlw, String urlb, String urlf, String urli,int imagesource){
msite_name = name;
msite_urlW = urlw;
msite_urlB = urlb;
msite_urlF = urlf;
msite_urlI = urli;
msite_imagesource = imagesource;
}
public String getMsite_name() {
return this.msite_name;
}
public String getMsite_url(){
return this.msite_url;
}
public String getMsite_urlW(){
return this.msite_urlW;
}
public String getMsite_urlB(){ return this.msite_urlB; }
public String getMsite_urlF(){
return this.msite_urlF;
}
public String getMsite_urlY(){
return this.msite_urlY;
}
public String getMsite_urlI(){
return this.msite_urlI;
}
public int getMsite_imagesource(){
return this.msite_imagesource;
}
总而言之,问题在于在list_item.xml中设计的图像视图和文本视图的内容不可用。