我已经阅读了很多关于如何使用expandablelistview的教程和答案;我试了好几个小时,换了代码,但每次点击我列表中的gruopitem它都不会扩展,只会更改"图片"显示该组的标题旁边应该展开。我还检查了列表中的数据的完整性。请帮助我,谢谢!
这是代码
xml groupItem
<?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="match_parent"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingLeft="40dp">
<TextView
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="@+id/textTitleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
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="match_parent"
android:orientation="vertical"
android:paddingLeft="50dp"
android:paddingRight="10dp" >
<TextView
android:id="@+id/textTitleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:textColor="#FFFFFF" />
</LinearLayout>
带有一些对象的适配器
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpAdapter extends BaseExpandableListAdapter {
private Context context;
private List<GroupItem> deptList;
public ExpAdapter(Context context, List<GroupItem> dept) {
this.context = context;
this.deptList = dept;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List<ChildItem> productList = deptList.get(groupPosition).getChilds();
return productList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
ChildItem childItem = (ChildItem) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.list_item, parent, false);
}
TextView sequence = (TextView) view.findViewById(R.id.textTitleItem);
sequence.setText(childItem.getTitle().trim() + ") ");
// TextView childItem = (TextView) view.findViewById(R.id.tex);
// childItem.setText(ChildItem.getName().trim());
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
List<ChildItem> productList = deptList.get(groupPosition).getChilds();
return productList.size();
}
@Override
public Object getGroup(int groupPosition) {
return deptList.get(groupPosition);
}
@Override
public int getGroupCount() {
return deptList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
GroupItem headerInfo = (GroupItem) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_item, parent, false);
}
TextView heading = (TextView) view.findViewById(R.id.textTitleGroup);
heading.setText(headerInfo.getTitle().trim());
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public static class GroupItem {
String title;
public List<ChildItem> items = new ArrayList<ChildItem>();
public void setTitle(String t){
title= t;
}
public List<ChildItem> getChilds(){
return items;
}
public void setChilds(List<ChildItem> l){
items=l;
}
public String getTitle(){
return title;
}
}
public static class ChildItem {
String title;
String hint;
public void setTitle(String t){
title= t;
}
public String getTitle(){
return title;
}
}
public static class ChildHolder {
TextView title;
}
public static class GroupHolder {
TextView title;
}
}
代码中有用的部分:这里我尝试使用onGroupExpandListener并使用onGroupClickListener;我也没试过,因为我已经读过expandablelistview单独处理点击,但没有任何作用
public class ArtFragment extends SherlockFragment implements OnGroupExpandListener,
OnGroupCollapseListener {
FragmentTransaction transaction;
EntryArte lh;
EntryGestore eg;
String tagpo="hoteldescr";
TextView nome;
Button telefono;
TextView testo;
TextView via_tit;
TextView via;
ImageButton navigazione;
ImageButton phone;
ImageButton web;
ImageButton mail;
AspectRatioImageView hotel_im;
String[] details;
String hotel_telefono;
String hotel_web;
String hotel_mail;
LatLng place;
ExpAdapter listAdapter;
ExpandableListView expListView;
List<GroupItem> listDataHeader;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.art_fragment, container, false);
if(savedInstanceState!=null)
lh= savedInstanceState.getParcelable("entry");
nome=(TextView)view.findViewById(R.id.art_name);
testo=(TextView)view.findViewById(R.id.art_text_text);
via_tit=(TextView)view.findViewById(R.id.art_via_text);
via=(TextView)view.findViewById(R.id.art_via_secondo);
phone= (ImageButton)view.findViewById(R.id.risto_phone);
web= (ImageButton)view.findViewById(R.id.risto_web);
navigazione=(ImageButton)view.findViewById(R.id.risto_drive);
mail=(ImageButton)view.findViewById(R.id.risto_email);
nome.setText(lh.getName());
//Log.v("TEXT", lh.getDescription());
testo.setText(lh.getInfo());
//with this method I populate the list of group and relatives childs(already checked, it works well)
populateExpandableList();
expListView = (ExpandableListView) view.findViewById(R.id.list_expandable);
listAdapter = new ExpAdapter(getActivity(), listDataHeader);
// setting list adapter
expListView.setAdapter(listAdapter);
答案 0 :(得分:0)
查看代码几乎所有内容都很好看。您可以尝试以下方法来解决问题。
1)在getChildView()/ getChildCount()中添加日志,以确定是否存在所提供数据的问题以及我们是否按预期获得框架的回调。
2)还有一件事是你可以尝试从getChildView()返回一个简单的TextView,而不是从布局中膨胀。我怀疑是否有布局:height =&#34; match_parent&#34;线性布局可能会导致任何问题。