2016-09-13 21 views
0

我還是傳統內容做出了ExpandableListView:6groups 6children的Android - ExpandableListView問題與子顯示

每個小組只有一個孩子,因此指標組/孩子都是一樣的

在組顯示正常,但時我選擇了仍然有記憶中最後一個的孩子。意味着我必須點擊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

只是嘗試reinstan tiate每一個孩子和createHtmlContent方法發送childPosition作爲參數

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

「childPosition」將始終爲0的孩子列表中的孩子的位置是一樣的,在組列表中選擇組。是的,如果我每次重新爲每個孩子持有者修復bug。但是名單變得非常滯後,無法使用 –