我制作了一个包含fixe内容的ExpandableListView:6groups 6children
每个组只有一个子组,因此索引组/子组是相同的
组正确显示但是当我选择子组时,它仍然在内存中有最后一组。意味着我必须单击2次才能加载孩子:
这是classe BaseAdapter:
public class CmsAdapter extends BaseExpandableListAdapter {
private Cms mCms;
private List<String> mGroups = new ArrayList<>();
private List<String> mChilds = new ArrayList<>();
private WeakReference<Context> mContext;
public CmsAdapter(@NonNull Context context, Cms cms) {
mCms = cms;
mContext = new WeakReference<>(context);
// Construct the lists of data
for (Section section : mCms.getSections()) {
mGroups.add(section.getTitle());
mChilds.add(section.getContent());
}
}
@Override
public int getGroupCount() {
return mGroups.size();
}
@Override
public int getChildrenCount(int i) {
return 1;
}
@Override
public Object getGroup(int i) {
return mGroups.get(i);
}
@Override
public Object getChild(int i, int i1) {
return mChilds.get(i);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int position, boolean b, View view, ViewGroup viewGroup) {
CmsHeaderHolder headerHolder;
if (view == null) {
// inflate the layout
LayoutInflater inflater = LayoutInflater.from(mContext.get());
view = inflater.inflate(R.layout.item_cms_group, viewGroup, false);
headerHolder = new CmsHeaderHolder(view);
view.setTag(headerHolder);
} else {
headerHolder = (CmsHeaderHolder) view.getTag();
}
headerHolder.mTextView.setText(mGroups.get(position));
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
CmsContentHolder contentHolder;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(mContext.get());
view = inflater.inflate(R.layout.item_cms_child, viewGroup, false);
contentHolder = new CmsContentHolder(view);
view.setTag(contentHolder);
} else {
contentHolder = (CmsContentHolder) view.getTag();
}
// Need to add charset for accented and special characters
contentHolder.mWebView.loadData(createHtmlContent(groupPosition), "text/html; charset=utf-8", "utf-8");
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
private String createHtmlContent(int position) {
String cssString = SharedPreferencesHelper.getStringPref(Constants.SharedPreferenceTags.CSS_STYLE);
if (cssString == null || cssString.isEmpty()) {
cssString = Constants.Css.defaultCss;
}
// Construct the html content with css string
StringBuilder html = new StringBuilder();
html.append("<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN\"\n" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>");
html.append(cssString);
html.append("</head><body>");
html.append(mChilds.get(position));
html.append("</body></html>");
return html.toString();
}
public class CmsContentHolder {
public WebView mWebView;
public CmsContentHolder(View itemView) {
mWebView = (WebView) itemView.findViewById(R.id.wv_cms);
// set default encoding just in case.
WebSettings webSettings = mWebView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
mWebView.setWebChromeClient(new WebChromeClient());
}
}
public class CmsHeaderHolder {
public TextView mTextView;
public CmsHeaderHolder(View itemView) {
mTextView = (TextView) itemView.findViewById(R.id.tv_cms);
}
}
}
答案 0 :(得分:0)
尝试重新实例化每个子节点并将createPosition作为参数发送到createHtmlContent方法
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
CmsContentHolder contentHolder;
LayoutInflater inflater = LayoutInflater.from(mContext.get());
view = inflater.inflate(R.layout.item_cms_child, viewGroup, false);
contentHolder = new CmsContentHolder(view);
// Need to add charset for accented and special characters
contentHolder.mWebView.loadData(createHtmlContent(childPosition), "text/html; charset=utf-8", "utf-8");
return view;
}