1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.paging.integration.testapp.room; 18 19 import android.view.ViewGroup; 20 import android.widget.TextView; 21 22 import androidx.paging.PagedList; 23 import androidx.paging.PagedListAdapter; 24 import androidx.paging.integration.testapp.R; 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import org.jspecify.annotations.NonNull; 28 29 /** 30 * Sample adapter which uses a AsyncPagedListDiffer. 31 */ 32 @SuppressWarnings("deprecation") 33 class PagedListCustomerAdapter extends PagedListAdapter<Customer, RecyclerView.ViewHolder> { 34 private RecyclerView mRecyclerView; 35 private boolean mSetObserved; 36 private int mScrollToPosition = -1; 37 private String mScrollToKey = null; 38 PagedListCustomerAdapter()39 PagedListCustomerAdapter() { 40 super(Customer.DIFF_CALLBACK); 41 } 42 setScrollToPosition(int position)43 void setScrollToPosition(int position) { 44 mScrollToPosition = position; 45 } 46 setScrollToKey(String key)47 void setScrollToKey(String key) { 48 mScrollToKey = key; 49 } 50 51 @Override onCreateViewHolder(ViewGroup parent, int viewType)52 public RecyclerView.@NonNull ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 53 RecyclerView.ViewHolder holder = new RecyclerView.ViewHolder( 54 new TextView(parent.getContext())) { 55 }; 56 holder.itemView.setMinimumHeight(400); 57 return holder; 58 } 59 60 @Override onBindViewHolder(RecyclerView.@onNull ViewHolder holder, int position)61 public void onBindViewHolder(RecyclerView.@NonNull ViewHolder holder, int position) { 62 Customer customer = getItem(position); 63 64 if (customer != null) { 65 ((TextView) holder.itemView).setText(customer.getId() + " " + customer.getLastName()); 66 } else { 67 ((TextView) holder.itemView).setText(R.string.loading); 68 } 69 } 70 findKeyInPagedList(@onNull String key, @NonNull PagedList<Customer> list)71 private static int findKeyInPagedList(@NonNull String key, @NonNull PagedList<Customer> list) { 72 for (int i = 0; i < list.size(); i++) { 73 Customer customer = list.get(i); 74 if (customer != null 75 && LastNameAscCustomerDataSource.getKeyStatic(customer).equals(key)) { 76 return i; 77 } 78 } 79 return 0; // couldn't find, fall back to 0 - could alternately search with comparator 80 } 81 82 @Override submitList(PagedList<Customer> pagedList)83 public void submitList(PagedList<Customer> pagedList) { 84 super.submitList(pagedList); 85 86 if (pagedList != null) { 87 final boolean firstSet = !mSetObserved; 88 mSetObserved = true; 89 90 if (firstSet 91 && mRecyclerView != null 92 && (mScrollToPosition >= 0 || mScrollToKey != null)) { 93 int localScrollToPosition; 94 if (mScrollToKey != null) { 95 localScrollToPosition = findKeyInPagedList(mScrollToKey, pagedList); 96 mScrollToKey = null; 97 } else { 98 // if there's 20 items unloaded items (without placeholders holding the spots) 99 // at the beginning of list, we subtract 20 from saved position 100 localScrollToPosition = mScrollToPosition - pagedList.getPositionOffset(); 101 } 102 mRecyclerView.scrollToPosition(localScrollToPosition); 103 } 104 } 105 } 106 107 @Override onAttachedToRecyclerView(@onNull RecyclerView recyclerView)108 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { 109 mRecyclerView = recyclerView; 110 } 111 112 @Override onDetachedFromRecyclerView(@onNull RecyclerView recyclerView)113 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) { 114 mRecyclerView = null; 115 } 116 } 117