2016-07-21 89 views
0

我有一種情況,我需要創建一個RecyclerView 3個部分
第一部分命名爲「第一」應該顯示3行包含的TextView和EditText上的每一行
次節命名爲「第二」應該包含2行,每行顯示一個的TextView
剖開RecyclerView有不同的佈局

第三部分命名爲‘第三’應包含4行,每行顯示一個TextView與圖像
。有沒有一種方法可以使用RecyclerView實現這一目標?
任何一個可以分享我的鏈接或示例代碼片段來實現這一功能提前

回答

2


感謝您可以使用庫SectionedRecyclerViewAdapter,以便將數據段。

首先創建一個科類:

class MyFirstSection extends StatelessSection { 

    String title; 
    List<String> list; 

    public MyFirstSection(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); 
    } 
} 

然後你建立你的節RecyclerView:

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

// Create your sections with the list of data for each year 
MyFirstSection section1 = new MyFirstSection("First", firstDataList); 
MySecondSection section2 = new MySecondSection("Second", secondDataList); 
MyThirdSection section3 = new MyThirdSection("Third", thirdDataList); 

// Add your Sections to the adapter 
sectionAdapter.addSection(section1); 
sectionAdapter.addSection(section2); 
sectionAdapter.addSection(section3); 

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

將檢查庫 –