我试图在片段中实现ExpandableListView。更新的测试列表未在布局视图中显示。我尝试了与新项目相同的代码(在活动中似乎有效),但我无法在片段中找出问题。 视图没有生成。我已经粘贴了我一直在尝试的代码。这可能是什么问题?请告诉我片段出错的地方。
编辑(2/2/2017) 我发现的实际问题是,
我一直在ScrollView中使用ExpandableListView,
<ScrollView
android:id="@+id/scrollFragHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
...........other layouts ......
<ExpandableListView
android:id="@+id/resHubExpandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:divider="@null"
android:groupIndicator="@null"
android:nestedScrollingEnabled="true" />
</ScrollView>
我必须给列表视图固定高度的layout_width以查看它的所有视图。 我在开始时使用了wrap_content,但它并没有按原样扩展listview。
我现在使用LayoutParam来设置列表视图的高度。
设置适配器后是否还有其他默认方式显示所有元素?
谢谢。
header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/resourceHubTitleHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/login"
android:ellipsize="end"
android:maxLines="1"
android:padding="15dp"
android:text="Franchisee Emails"
android:textColor="@color/primaryColor"
android:textStyle="bold" />
</LinearLayout>
layout_list_item.xml
<!--resource hub title/date-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/resourceTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:text="Franchisee Agreement"
android:textColor="@color/primaryColor"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: "
android:textColor="@color/primaryColor" />
<TextView
android:id="@+id/resourceDateUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DD/MMM/YYYY"
android:textColor="@color/primaryColor" />
</LinearLayout>
</LinearLayout>
ExpandableListView适配器:
public class ResHubExpandableListAdapter extends BaseExpandableListAdapter {
HashMap<String, ArrayList<String>> mResHubList;
ArrayList<String> mResHubHeaderList;
Context mContext;
public ResHubExpandableListAdapter(Context context, HashMap<String, ArrayList<String>> mResHubFiles
, ArrayList<String> resHubHeader) {
this.mResHubList = mResHubFiles;
this.mResHubHeaderList = resHubHeader;
this.mContext = context;
}
@Override
public int getGroupCount() {
return mResHubHeaderList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mResHubList.get(mResHubHeaderList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return mResHubHeaderList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mResHubList.get(mResHubHeaderList.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) {
View row = convertView;
LayoutInflater inflater;
String headerTitle = (String) getGroup(groupPosition);
if (row == null || row.getTag() == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.resource_hub_list_header, null);
GeneralUtil.Log("rzenks getGroupView", "getGroupView at position: " + groupPosition);
}
TextView resourceHubTitleHeader = (TextView) row.findViewById(R.id.resourceHubTitleHeader);
resourceHubTitleHeader.setText(headerTitle);
return row;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater;
final String childText = (String) getChild(groupPosition, childPosition);
if (row == null || row.getTag() == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.resource_hub_list_item, null);
GeneralUtil.Log("rzenks getChildView", "getChildView at position: " + groupPosition);
}
TextView resourceTitle = (TextView) row.findViewById(R.id.resourceTitle);
resourceTitle.setText(childText);
return row;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}}
我的片段
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
//new tab addition
mResourceHubMain = (LinearLayout) v.findViewById(R.id.resourceHubMain);
mResHubExpandableListView = (ExpandableListView) v.findViewById(R.id.resHubExpandableListView);
// New Tab list data view
prepareExpandListData();
mExpandableListAdapter = new ResHubExpandableListAdapter(getActivity(), resHubList, resHubHeaderList);
mResHubExpandableListView.setAdapter(mExpandableListAdapter);
}