I have very strange problem and I tried to resolve it about 2 days. I have a RecyclerView
which nested in the NestedScrollView
. I create EndlessScrollListener
for my NestedScrollView
and it load more data correctly, but when I call notifyDataSetChanged()
or notifyItemRangeChanged()
my list scroll to top to the second item or another. Also when I want an update all items in my SwipeRefreshLayout
it maybe mixed(for example the image from fourth item maybe set in first item and etc). This happens only when I want to update items, when I open my app first time everything is ok. I have no idea why it happens, please help me. Thanks in advance. My code
It's my function for initialize the adapter
private void setFeedRecycler(RecyclerView feedRecyclerView, List<Edge<Activity>> feeds){
noFeedsTitle.setVisibility(View.GONE);
feedsArray.addAll(feeds);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
feedRecyclerView.setHasFixedSize(true);
feedRecyclerView.setNestedScrollingEnabled(false);
feedRecyclerView.setLayoutManager(layoutManager);
feedsDashboardAdapter = new FeedsDashboardAdapter(getActivity(), this, feedsArray, feedRecyclerView, mainDashboardLayout);
feedsDashboardAdapter.setOnLoadMoreListener(loadMoreListenerFeeds);
feedRecyclerView.setAdapter(feedsDashboardAdapter);
}
After get data from server I update my adapter from the main UI
getActivity().runOnUiThread(new Runnable() {
public void run() {
feedsArray.addAll(feedMobile.getEdges());
feedsDashboardAdapter.notifyDataSetChanged();
}});
When I want to update my all items
private SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadAfterCursorFeed = "";
feedsArray.clear();
getUserFeeds(user.getAccessToken(), user.getClientId(), loadAfterCursorFeed);
}
};
loadAfterCursorFeed it's like an id for the next page of a data. And finally my adapter
public FeedsDashboardAdapter(Context context, DashboardFragment fragment, ArrayList<Edge<Activity>> feeds, RecyclerView recyclerView,
NestedScrollView mainDashboardLayout){
this.feeds = feeds;
this.context = context;
this.fragment = fragment;
this.mainDashboardLayout = mainDashboardLayout;
this.linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
this.client = new RestClient().getApiService();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
user = new Gson().fromJson(sharedPreferences.getString("user", null), User.class );
mainDashboardLayout.setOnScrollChangeListener(new EndlessParentScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount) {
Log.d(TAG, "page " + page + " totalItemsCount " + totalItemsCount);
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
}
});
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_USER_FEEDS) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_feed, parent, false);
return new CategoryUserFeedHolder(layoutView, context);
} else if(viewType == VIEW_TYPE_SHARE_EVENT) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_feed_shared_event, parent, false);
return new CategoryShareEventHolder(layoutView);
} else if (viewType == VIEW_TYPE_LOADING) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_item_layout, parent, false);
return new LoadingViewHolder(layoutView);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof CategoryShareEventHolder) {
//set necessary field
}
else if(holder instanceof CategoryUserFeedHolder){
//set necessary field
}
}
@Override
public int getItemCount() {
return this.feeds ==null ? 0 : this.feeds.size();
}
@Override
public int getItemViewType(int position) {
if (feeds.get(position) == null)
return VIEW_TYPE_LOADING;
else if (feeds.get(position).getResponse().getEvent() != null)
return VIEW_TYPE_SHARE_EVENT;
return VIEW_TYPE_USER_FEEDS;
}
public void setOnLoadMoreListener(OnLoadMoreListener listener){
this.onLoadMoreListener = listener;
}