2016-06-17 5 views
0

RecyclerViewに渡すリストがあります。 WifiNetworkオブジェクトには、ブール値 "inRange"が保持されます。私RecyclerViewでRecyclerViewアダプタ、データセットのフィールドに基づく2つのセクション

、私はこのような構造にしたい: - ヘッダー:RANGE 、IN - ヘッダー - ブールINRANGE ==真 を持つアイテム:NOT RANGE 、IN - ブールINRANGE == falseを

Iとの項目をこれを行う簡単な方法を見つけることができないようです。私が試した何

: - ストレート私の活動の中に「レンジで」&「いいえ範囲内」のラベルを作成し、(醜いです)2 RecyclerViewsを使用 - 区分-recyclerview afollestadによって(私にとっては少し不明でした)

これは非常に一般的なものでなければなりません。あなたはどうやってこれに対処しましたか?

答えて

0

ライブラリSectionedRecyclerViewAdapterを使用して、データをセクションにグループ化することができます。

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

// Create your sections with the list of data for each year 
MySection section1 = new MySection("IN RANGE", inRangeDataList); 
MySection section2 = new MySection("NOT IN RANGE", notInRangeDataList); 

// Add your Sections to the adapter 
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); 
:あなたのセクションでRecyclerViewを設定すると

class MySection extends StatelessSection { 

    String title; 
    List<String> list; 

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

     this.title = title; 
     this.list = list; 
    } 

    @Override 
    public int getContentItemsTotal() { 
     return list.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 MyItemViewHolder(view); 
    } 

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

     // bind your view here 
     itemHolder.tvItem.setText(list.get(position)); 
    } 

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

第一部クラスを作成します