recyclerview在notifyDataSetChanged()之后滚动到顶部

时间:2017-03-03 06:14:23

标签: android android-recyclerview adapter notifydatasetchanged

我有一个非常奇怪的问题,我试着解决它大约2天。我有RecyclerView嵌套在NestedScrollView中。我为EndlessScrollListener创建了NestedScrollView并且正确加载了更多数据,但当我拨打notifyDataSetChanged()notifyItemRangeChanged()时,我的列表会滚动到第二个项目或其他项目的顶部。此外,当我想要更新SwipeRefreshLayout中的所有项目时,它可能会混合(例如,第四项中的图像可能设置在第一项等等)。只有当我想要更新项目时才会发生这种情况,当我第一次打开我的应用程序时一切正常。我不知道为什么会这样,请帮帮我。提前致谢。我的代码 这是我初始化适配器的功能

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

从服务器获取数据后,我从主UI更新我的适配器

getActivity().runOnUiThread(new Runnable() {
       public void run() {
           feedsArray.addAll(feedMobile.getEdges());
           feedsDashboardAdapter.notifyDataSetChanged();
}});

当我想更新我的所有项目时

private SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        loadAfterCursorFeed = "";
        feedsArray.clear();
        getUserFeeds(user.getAccessToken(), user.getClientId(), loadAfterCursorFeed);
    }
};

loadAfterCursorFeed它就像是下一页数据的id。 最后我的适配器

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

1 个答案:

答案 0 :(得分:0)

尝试在布局中使用不带NestedScrollView的RecyclerView,请看此答案  https://stackoverflow.com/a/31818632/6926324