2017-11-11 159 views
0

所以現在已經有一個月了,我還沒有解決這個簡單的問題。如何在Recyclerview中使用Expandable ListView?

目標: 我想有一個可擴展的列表,其中parent是textView,而child是包含2(現在)無線電的radiogroup。

我有一切工作,但後來我得知,Expandable ListView重用該對象建議使用recyclerview或viewholder。我嘗試了查看者,但發生同樣的事情: 如果我展開組1並檢查無線電位置1,請展開組1並展開組2,檢查無線電位置1。這是爲什麼發生?

下面是myClassList代碼擴展BaseExapandableListAdapter:

public int getGroupCount() { 
    return titles.size(); 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return 1; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return titles.get(groupPosition); 
} 

@Override 
public List<String> getChild(int groupPosition, int childPosition) { 
    return colorsMap.get(titles.get(groupPosition)); 
} 

@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) { 
    String title = (String) this.getGroup(groupPosition); 
    if(convertView == null){ 
     LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     assert (layoutInflater != null); 
     convertView = layoutInflater.inflate(R.layout.parent_item_s, parent, false); 
    } 
    TextView textView = (TextView) convertView.findViewById(R.id.titleContent); 
    textView.setTypeface(null, Typeface.BOLD); 
    textView.setText(title); 
    return convertView; 
} 
class ViewHolder{ 
    RadioButton radioButton; 
    RadioButton radioButton1; 

    ViewHolder(View view){ 
      radioButton = (RadioButton) view.findViewById(R.id.rd1); 
      radioButton1 = (RadioButton) view.findViewById(R.id.rd2); 
    } 


} 

@Override 
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 

    List<String> color = colorsMap.get(titles.get(groupPosition)); 
    ViewHolder holder = null; 
    if(convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     assert (layoutInflater != null); 
     convertView = layoutInflater.inflate(R.layout.child_item_s, parent, false); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
    } 

    holder = (ViewHolder) convertView.getTag(); 
    holder.radioButton.setText(color.get(0)); 
    holder.radioButton1.setText(color.get(1)); 

    return convertView; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

如何確保每個孩子都有自己的對象?

+0

該位置可能是RecyclerViews,ListViews等的最大禮物。隨着視圖的回收,所提供的位置總是相同的。所以例如。 SparseArray可以用來在任何位置放置選擇的收音機。然後你檢索並使用,如果它沒有null,並刪除持有的位置,如果它不再被使用等等。有很多方法來保存在什麼位置發生的事情。只要列表更改,請務必更新所有持有人。 – CmosBattery

+0

我會查看SparseArryay,謝謝。另外,在RecyclerView成爲事物之前,人們是如何處理這個問題的?肯定有人試圖在2012年或更早的時候實施我想要的同樣的事情? – rated2016

回答

0

有些人已經遇到過你的問題併爲他們創造瞭解決方案。

看看afollestad's sectioned-recyclerviewdavideas's FlexibleAdapter。他們都有關於如何使用他們的圖書館的指南。

+0

我想避免使用lib(最初),我寧願通過查看第一個鏈接提供的示例進行編碼,所以非常感謝您提供該示例。 – rated2016

+0

我也和你一樣,但其他人已經在你身邊,爲他們自己創造了自己的解決方案。您可以隨時對其進行編輯並根據您的需求量身定製,我極力推薦。無需重新發明輪子:) –