0

私はRecyclerViewのアダプタとしてluizgrp/SectionedRecyclerViewAdapterからSectionedRecyclerViewAdapterを使用しています。 RecyclerViewAdapterでAndroid SectionedRecyclerViewAdapter implement getItemId

、私は各行を一意に識別したい場合、私はこの方法でオーバーライドします:

@Override 
public long getItemId(int position) { 
    return this.dataList.get(position).getId(); 
} 

をしかし、私はSectionedRecyclerViewAdapterとどのようにそれを行うのですか?私はSectionedRecyclerViewAdapter &オーバーライドgetItemId()を拡張するものだと思う

public class Section1 extends Section { 
     ..... 

     public long getItemId(int position) { 
     if (position == 0) { 
      return 0; 
     } 

     return this.openPosList.get(position - 1).getId(); 
     } 
} 

:私はのgetId()メソッドを追加して、以下のようなセクションのコードを持っています。しかし、ここではpositionをセクションの行の位置に変換する際に問題があります。

public class PositionRecylerViewAdapter extends SectionedRecyclerViewAdapter { 
    ...... 

    @Override 
    public long getItemId(int position) { 
      // ???? 
      // transform position here into Section's item position, considering multiple Sections with Header & Footer 
      // ???? 
    } 

} 

誰もがサンプルコードでこれまでに同様のコードを実装していますか?ありがとう!

+0

セクションのgetID()メソッドを呼び出すには、 – aborocz

+0

@aboroczはい、最終的に私はSectionのgetID() –

答えて

0

&を実装して、これを私のPositionRecylerViewAdapterに実装します。 Section1クラスで

public class PositionRecyclerViewAdapter extends SectionedRecyclerViewAdapter { 

    public PositionRecyclerViewAdapter() { 
     super(); 
     ...... 

     this.setHasStableIds(true); 
    } 

    ....... 

    @Override 
    public long getItemId (int index) { 
     int viewType = this.getSectionItemViewType(index); 
     Section1 section1 = (Section1) this.getSection(POSITION_SECTION); 

     if (viewType == SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER) { 
      return section1.getHeaderId(); 
     } else if (viewType == SectionedRecyclerViewAdapter.VIEW_TYPE_ITEM_LOADED) { 
      int sectionItemIndex = this.getPositionInSection(index); 
      return section1.getItemId(sectionItemIndex); 
     } 

     return -1; 
    } 

} 

public class Section1 extends Section { 
    ..... 

    public long getHeaderId() { 
     return headerId; 
    } 

    public long getItemId(int index) { 
     return this.openPosList.get(index).getId(); 
    } 
} 

私自身の答え&がうまくいけば、誰かが感謝、次回重宝投稿!

関連する問題