恢复滚动位置

时间:2017-06-29 20:22:17

标签: android-recyclerview

我有一个从Firebase数据库中检索数据的简单片段。 我使用firebase recycleler视图来显示检索数据。滚动或屏幕旋转后,我无法强制回收视图(或线性布局管理器)恢复滚动位置。

我在这里找到了一些答案,但它们不起作用。

我的代码是:

公共类NewsListFragment扩展了ParentNewsFragment {

static int color_naval, color_black;
private boolean mProcessLikes = false;
private DatabaseReference mDatabaseLikes;
private DatabaseReference mDatabaseViews;
private FirebaseAuth mAuth;
private int position = 0;


public static NewsListFragment getInstance() {
    return new NewsListFragment();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDatabaseReference = FirebaseDatabase.getInstance().getReference()
            .child("App_news");
    mDatabaseLikes = FirebaseDatabase.getInstance().getReference().child("news_likes");
    mDatabaseViews = FirebaseDatabase.getInstance().getReference().child("news_views");
    mAuth = FirebaseAuth.getInstance();
    mQuery = mDatabaseReference.orderByChild("pos").startAt("100");
    color_naval = getResources().getColor(R.color.colorPrimary);
    color_black = getResources().getColor(R.color.colorBlack);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.rv_choose, container, false);
    rv = (RecyclerView) rootView.findViewById(R.id.rv_choose);
    lm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(lm);
    rv.setHasFixedSize(true);
    return rootView;
}

@Override
public void onPause() {
    super.onPause();
    position = lm.findFirstVisibleItemPosition();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("position", position);
}

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    if(savedInstanceState != null) {
        position = savedInstanceState.getInt("position");
    }
}

@Override
public void onResume() {
    super.onResume();
    if (position != 0) {
        lm.scrollToPosition(position);
        showToast(position+"");
    }
}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<NewsList, NewsListViewHolder> adapter =
            new FirebaseRecyclerAdapter<NewsList, NewsListViewHolder>(
                    NewsList.class,
                    R.layout.frag_newslist_card_view,
                    NewsListViewHolder.class,
                    mQuery
            ) {
                @Override
                protected void populateViewHolder(NewsListViewHolder viewHolder, final NewsList model, int position) {
                    final String post_key = getRef(position).getKey();
                    viewHolder.setDate(model.getDate()+",");
                    viewHolder.setTime(model.getTime());
                    viewHolder.setTitle(model.getTitle());
                    viewHolder.setImage(getContext(), model.getImage());
                    viewHolder.setEye(model.getCode());
                    viewHolder.setThumb(post_key);

                    viewHolder.thumb.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mProcessLikes = true;
                            mDatabaseLikes.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    if (mProcessLikes){
                                        if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){
                                            mDatabaseLikes.child(post_key).child(mAuth.getCurrentUser().getUid())
                                                    .removeValue();
                                            mProcessLikes = false;
                                        }
                                        else {
                                            mDatabaseLikes.child(post_key).child(mAuth.getCurrentUser().getUid())
                                                    .setValue("like");
                                            mProcessLikes = false;
                                        }
                                    }
                                }
                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });
                        }
                    });

                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mDatabaseViews.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    if (!dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())) {
                                        mDatabaseViews.child(post_key).child(mAuth.getCurrentUser().getUid())
                                                .setValue("view");
                                    }
                                }
                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });
                            mChooserListener.chooseNews(model.getCode());
                        }
                    });
                }
            };
    rv.setAdapter(adapter);
}

public static class NewsListViewHolder extends RecyclerView.ViewHolder {
       //some text here
}

}

方法showToast在最后显示实际位置数,但recyclelerview从头开始。

任何想法?

2 个答案:

答案 0 :(得分:0)

确保您没有从服务器或任何检索数据的位置重新加载数据。 添加非空检查您的片段中的onCreateView或活动onCreate,如:

        if(savedInstanceState != null){
    ...
    }else{
    loadData();
}

答案 1 :(得分:0)

使用类似于以下内容的方式替换线性布局管理器。在片段中使用RecyclerView时,我已多次使用此实现。让我知道它是怎么回事

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
yourItem.setLayoutManager(mLayoutManager);