![](/img/trans.png)
[英]How to handle child click event in Expandablelistview in Android?
[英]Android - ExpandableListView issue with child display
我制作了一個具有fixe內容的ExpandableListView:6groups 6children
每個組只有一個孩子,因此索引組/孩子是相同的
這些組可以正確顯示,但是當我選擇子組時,它仍會保留最后一個。 意味着我必須單擊2次才能加載孩子:
這是ClassAdapter類:
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);
}
}
}
只需嘗試重新實例化每個孩子,並將childPosition作為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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.