Desplácese RecyclerView a una determinada posición de la lista [PagedListAdapter + Habitación + LiveData]
He creado una lista de más de 2000 artículos de uso ViewModel, LiveData, Sala, PagedListAdapter
En mi aplicación, hay una opción para seleccionar un elemento en una época en la lista. En mi lista, yo soy capaz de marcar el elemento seleccionado. Ahora quiero desplazamiento recyclerview para el elemento seleccionado cada vez que el usuario entra a la página.
Después de mi fragmentos de código
LocationDao.java
@Dao
public interface LocationDao {
@Query("SELECT * from tbl_location ORDER BY timestamp ASC")
DataSource.Factory<Integer, Location> getLiveDataLocations();
}
LocationRepository.java
public class LocationRepository {
LocationDao mDao;
public LocationRepository(Application application) {
DeviceRoomDatabase db = DeviceRoomDatabase.getDatabase(application);
this.mDao = db.locationDao();
}
public LiveData<PagedList<Location>> getLiveDataAllLocations() {
LiveData<PagedList<Location>> locationList = new LivePagedListBuilder<>(
mDao.getLiveDataLocations(), 10).build();
return locationList;
}
}
LocationViewModel.java
public class LocationViewModel extends AndroidViewModel {
final String TAG = getClass().getSimpleName();
LocationRepository mLocationRepository;
public LocationViewModel(@NonNull Application application) {
super(application);
mLocationRepository = new LocationRepository(application);
}
public LiveData<PagedList<Location>> getLocationList() {
return mLocationRepository.getLiveDataAllLocations();
}
}
LocationRecyclerAdapter.java
public class LocationRecyclerAdapter extends PagedListAdapter<Location, FooterViewHolder> {
OnRecyclerItemClickListener mItemClickListener;
final int VIEW_TYPE_FOOTER=1;
final int VIEW_TYPE_ITEM=2;
public LocationRecyclerAdapter() {
super(DIFF_CALLBACK);
}
public void setItemClickListener(OnRecyclerItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position==getItemCount()?VIEW_TYPE_FOOTER:VIEW_TYPE_ITEM;
}
@NonNull
@Override
public FooterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
if(viewType==VIEW_TYPE_ITEM){
v= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_location_list,parent,false);
FooterViewHolder vh= new LocationViewHolder(v);
vh.setItemClickListener(mItemClickListener);
return vh;
}else{
v= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_footer,parent,false);
FooterViewHolder vh= new FooterViewHolder(v);
//vh.setItemClickListener(mItemClickListener);
return vh;
}
}
@Override
public void onBindViewHolder(@NonNull FooterViewHolder holder, int position) {
if(holder instanceof LocationViewHolder) {
Location loc= getItem(position);
if(loc!=null) {
((LocationViewHolder) holder).bind(position, loc);
}
}
}
private static DiffUtil.ItemCallback<Location> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Location>() {
// Concert details may have changed if reloaded from the database,
// but ID is fixed.
@Override
public boolean areItemsTheSame(Location oldLoc, Location newLoc) {
return oldLoc.networkId == newLoc.networkId&&oldLoc.locationName.equals(newLoc.locationName);
}
@Override
public boolean areContentsTheSame(Location oldLoc, Location newLoc) {
return oldLoc.equals(newLoc);
}
};
}
Y en la Actividad de clase
RecyclerView rv_main =view. findViewById(R.id.rv_main);
rv_main.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
mLocationViewModel.getLocationListV2().observe(this, mAdapter::submitList);
rv_main.setAdapter(mAdapter);
int storedLocationId=1100;
//***TODO: here I need to move a particular position of the list item, in which location id 1100 [assume that it is in the 1100 position of the list and i have to move the recycler position to 1100th position]***

Vea otras preguntas sobre etiquetas android-recyclerview android-room android-livedata android-viewmodel android-pagedlistview