2016-01-02 33 views
0

我想創建嵌套的RecyclerViews,問題是隻有父級顯示。RecyclerView中的RecyclerViewVIew

父適配器

public class SectionsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    Context mContext; 
    List<Section> mSections; 

    public SectionsAdapter(Context context, List<Section> sections) { 
     mContext = context; 
     mSections = sections; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_section, parent, false); 
     return new SectionViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int pos) { 
     Section section = mSections.get(pos); 
     if (holder instanceof SectionViewHolder) { 
      SectionViewHolder sectionVH = (SectionViewHolder) holder; 
      sectionVH.mTitle.setText(section.getTitle()); 
      LinearLayoutManager manager = new LinearLayoutManager(mContext); 
      sectionVH.mPreferences.setLayoutManager(manager); 
      PreferencesAdapter preferencesAdapter = new PreferencesAdapter(mContext, section.getPreferences()); 
      sectionVH.mPreferences.setAdapter(preferencesAdapter); 
      sectionVH.mPreferences.getAdapter().notifyDataSetChanged(); 
     } 
    } 

    @Override 
    public int getItemViewType(int pos) { 
     return 0; 
    } 

    @Override 
    public int getItemCount() { 
     return mSections.size(); 
    } 
} 

兒童適配器

public class PreferencesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    public static final int VIEW_BOOLEAN = 0; 
    public static final int VIEW_INT = 1; 

    Context mContext; 
    List<Preference> mPreferences; 

    public PreferencesAdapter(Context context, List<Preference> preferences) { 
     mContext = context; 
     mPreferences = preferences; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view; 
     switch (viewType) { 
      default: 
       view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_preference_boolean, parent, false); 
       return new BooleanPreferenceViewHolder(view); 
     } 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int pos) { 
     Preference preference = mPreferences.get(pos); 
     if (holder instanceof BooleanPreferenceViewHolder) { 
      BooleanPreferenceViewHolder booleanPreferenceVH = (BooleanPreferenceViewHolder) holder; 
      booleanPreferenceVH.mPreferenceTitle.setText(preference.getTitle()); 
      booleanPreferenceVH.mPreferenceExplanation.setText(preference.getExplanation()); 
      booleanPreferenceVH.mBooleanSwitch.setChecked((Boolean) preference.getValue()); 
     } 
    } 

    @Override 
    public int getItemViewType(int pos) { 
     return VIEW_BOOLEAN; 
    } 

    @Override 
    public int getItemCount() { 
     return mPreferences.size(); 
    } 
} 

父視圖持有人

public class SectionViewHolder extends RecyclerView.ViewHolder { 

    public TextView mTitle; 
    public RecyclerView mPreferences; 

    public SectionViewHolder(View v) { 
     super(v); 
     mTitle = (TextView) v.findViewById(R.id.sectionTitle); 
     mPreferences = (RecyclerView) v.findViewById(R.id.preferences); 
    } 

} 

子視圖持有人

public class BooleanPreferenceViewHolder extends RecyclerView.ViewHolder { 

    public TextView mPreferenceTitle; 
    public TextView mPreferenceExplanation; 
    public Switch mBooleanSwitch; 

    public BooleanPreferenceViewHolder(View v) { 
     super(v); 
     mPreferenceTitle = (TextView) v.findViewById(R.id.preferenceTitle); 
     mBooleanSwitch = (Switch) v.findViewById(R.id.booleanSwitch); 
     mPreferenceExplanation = (TextView) v.findViewById(R.id.preferenceExplanation); 
    } 

} 

所以我注意到這隻顯示了父級標題,而其recyclerview是空的。此外,child onBindViewHolder方法未調用。

+0

如何既recyclerviews的佈局個XML是什麼樣子? – migos

+0

反模式:) – TeeTracker

回答

0

只需要一個RecyclerView就可以實現你想要的,正確覆蓋getItemViewType方法。你可以看到一個例子here

下面是使用SectionPreferenceBooleanPreferenceViewHolder類與庫SectionedRecyclerViewAdapter一個例子。

首先創建一個部分類:

class MySection extends StatelessSection { 

    String title; 
    List<Preference> mPreferences; 

    public MySection(String title, List<Preference> preferences) { 
     // call constructor with layout resources for this Section header, footer and items 
     super(R.layout.section_header, R.layout.section_footer, R.layout.section_item); 

     this.title = title; 
     this.mPreferences = preferences; 
    } 

    @Override 
    public int getContentItemsTotal() { 
     return mPreferences.size(); // number of items of this section 
    } 

    @Override 
    public RecyclerView.ViewHolder getItemViewHolder(View view) { 
     // return a custom instance of ViewHolder for the items of this section 
     return new BooleanPreferenceViewHolder(view); 
    } 

    @Override 
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { 
     BooleanPreferenceViewHolder itemHolder = (BooleanPreferenceViewHolder) holder; 

     // bind your view here 
     itemHolder.mPreferenceExplanation.setText(mPreference.get(position).getExplanation()); 
    } 

    @Override 
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) { 
     return new SimpleHeaderViewHolder(view); 
    } 

    @Override 
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { 
     MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; 

     // bind your header view here 
     headerHolder.tvItem.setText(title); 
    } 
} 

然後你設置了RecyclerViewSections

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter(); 

MySection section1 = new MySection(mSections.get(0).getTitle(), mSections.get(0).getPreferences()); 
MySection section2 = new MySection(mSections.get(1).getTitle(), mSections.get(1).getPreferences()); 

// Add your Sections 
sectionAdapter.addSection(section1); 
sectionAdapter.addSection(section2); 

// Set up your RecyclerView with the SectionedRecyclerViewAdapter 
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); 
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
recyclerView.setAdapter(sectionAdapter);