简体   繁体   中英

Android - ExpandableListView issue with child display

I made an ExpandableListView with fixe content : 6groups 6children

Each group has only one child so the indexes group/child are the same

The groups are displayed correctly but when I select children it still have the last one in memory. Means I have to click 2 times to load a child :

  • Open group -> Load the last child loaded in another group
  • Close/open same group -> Load the good child of this group

Here is the 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);
    }
}

}

just try to reinstantiate every child and send childPosition as parameter in createHtmlContent method

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM