How to Stop Refreshing Recyclerview Data Scroll to Top Position Android Everytime

Stop RecyclerView from reloading data

Basically you need to create a class for your recycle view adapter (you should use recycleview instead of using listview because it's newer then listview and have many optimization and new functionalities that listview doesn't have).

MyRecyclerViewAdapter

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;

public MyRecyclerViewAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);

//Render image using Picasso library
if (!TextUtils.isEmpty(feedItem.getThumbnail())) {
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(customViewHolder.imageView);
}

//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
}

@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}

class CustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView textView;

public CustomViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
this.textView = (TextView) view.findViewById(R.id.title);
}
}

}

as you can see in the above example, all the items are already fetched (ready to display instead of just getting them each time the user scroll up/down the list). second there are a few functions like onCreateViewHolder where you need to create your own view holder (inflate the layout and create from it a holder that will hold all the views that will be used to show the data that you want to show in the list). second onBindViewHolder is for binding (connecting) the views in the holder with the data that the adapter is getting from it's constructor.

After you implement the adapter you should use it as follows:
somewhere in your activity/fragment

...
List<FeedItem> data = manager.getItems();
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(MainActivity.this, feedsList);
mRecyclerView.setAdapter(adapter);
...

A few things to notice here:
1) manager.getItems(); is a manager from where you can get your items from, in order to use it you need to know from where you need to get those items(web/storage or other source) then you need to implement it (there are many sites that explain how to do it.
2) if you are using fragment you should use instead of MainActivity.this getActivity()

you can see for more details here

Hope it will help you understand how to do it.

Update RecyclerView while keeping its scroll position

You can get the current position, refresh the adapter and smooth scroll to the previous position like this:

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};

Then set the position to scroll to:

smoothScroller.setTargetPosition(position);
layoutManager.startSmoothScroll(smoothScroller);

On updation of data set of recycler view list ,it's scroll jumps back to the top (duplicate)

I have just use SwipeRefreshLayout which was solved my issue.

Visit https://developer.android.com/training/swipe/add-swipe-interface



Related Topics



Leave a reply



Submit