0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RecyclerViewでConcatAdapter利用におけるonBindViewHolderのposition

Posted at

はじめに

RecyclerViewでConcatAdapter利用におけるonBindViewHolderのpositionは何のpositionなのかわかったので自分メモです。

結論

ConcatAdapterを使うには複数のAdapterを作るが、それぞれのAdapterの表示位置を返している。
それぞれのAdapterのViewHolderごとにpositionを振っているから当たり前といえば当たり前なんだけども…
ViewModelにObjectじゃなくてpositionを送ってたら、Adapter1のposition-->1を選択してるつもりが全部のAdapterのposition-->1を選択してしまうというミスをしていたから一応メモしておこうかと…
position.png

Code

ConcatAdapter.java
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
Adapter1 = new adapter1();
Adapter2 = new adapter2();
Adapter2 = new adapter3();
ConcatAdapter concatAdapter = new ConcatAdapter(adapter1, adapter2, adapter3);
recyclerView.setAdapter(concatAdapter);
Adapter[n].java
public class Adapter1 extends RecyclerView.Adapter<ThisDayItemRecyclerViewAdapter.ViewHolder>{
~~省略~~
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.i("ここ", String.valueOf(position));
        ~~それぞれのAdapter.javaで 0 から振られる~~
    }
}
~~省略~~

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?