将RecyclerView滚动到列表的特定位置[PagedListAdapter + Room + LiveData]

时间:2019-09-19 11:57:45

标签: android-recyclerview android-room android-livedata android-viewmodel android-pagedlistview

我已经使用ViewModelLiveDataRoomPagedListAdapter

创建了2000多个项目的列表

在我的应用中,有一个选项可以一次在列表中选择一个项目。在我的列表中,我可以标记所选项目。 现在,我想在用户访问页面时将recyclerview滚动到所选项目

按照我的代码段

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

在Activity类中

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]***

0 个答案:

没有答案