20

RecyclerView已與Android L一起引入,並且是AppCompat v7庫的一部分。然後我決定用這個全新的RecyclerView更新我的應用程序來替換我的ListView。 爲此,當爲RecyclerView設置LayoutManager時,我使用的是LinearLayoutManager,它工作正常。如何使用LinearLayoutManager實現Expandable項目(與Android L一起推出)

這裏是困難的東西:當我想改變我的ExpandableListViewRecyclerView。 由於谷歌還沒有創建「ExpandableLayoutManager」,這是非常棘手的,我不能實現這一點。

documentation提到孩子,但它似乎是RecyclerView的根視圖的子女,而不是兒童本身。

有人有解決方法或一些線索嗎? 或至少是從哪裏開始的信息,以便我可以實現我自己的LayoutManager

+0

將需要自定義視圖,現在一個工作,將更新當它完成時。 – Flynn81 2014-12-09 18:35:11

+0

當然,我開始了很多次,但它很多工作,我總是堅持LayoutManager ... – 2014-12-10 11:57:40

回答

1

library可幫助您將項目分組爲「部分」,然後您可以實現此example中的展開/收縮功能。

首先創建你的部分類:

class MySection extends StatelessSection { 

    String header; 
    List<String> list; 
    boolean expanded = true; 

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

    @Override 
    public int getContentItemsTotal() { 
     return expanded? list.size() : 0; 
    } 

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

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

     headerHolder.tvTitle.setText(title); 

     headerHolder.rootView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       expanded = !expanded; 
       headerHolder.imgArrow.setImageResource(
         expanded ? R.drawable.ic_keyboard_arrow_up_black_18dp : R.drawable.ic_keyboard_arrow_down_black_18dp 
       ); 
       sectionAdapter.notifyDataSetChanged(); 
      } 
     }); 
    } 

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

然後創建您的部分實例,並設置你的適配器:

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

// Add your Sections 
sectionAdapter.addSection(new MySection("Section 1", Arrays.asList(new String[] {"Item 1", "Item 2", "Item 3", "Item 4" }))); 
sectionAdapter.addSection(new MySection("Section 2", Arrays.asList(new String[] {"Item 1", "Item 2" }))); 

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