我无法获取我的片段以保存我的recycleview滚动位置

时间:2017-07-10 13:38:59

标签: android android-recyclerview onsaveinstancestate

我无法在线找到解决方法。我试图保存RecycleView的滚动位置,以便当用户点击帖子并返回主页时,将继续他们停在的位置。 onCreateView正在接收int变量,但当我将其设置为RecycleView时,没有任何反应。

以下是Activity

Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




        mSectionsPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);



    // Set up the ViewPager with the sections adapter.
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);


private void setupViewPager(ViewPager viewPager){

        SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Card_view(), "New");
        adapter.addFragment(new Card_view2(), "Subscribed");
        viewPager.setAdapter(adapter);

}

以下是Fragment

@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState){
    View view=inflater.inflate(R.layout.card_view,container,false);

    mDatabase= FirebaseDatabase.getInstance().getReference().child("Posts");
    mDatabaseUpvote= FirebaseDatabase.getInstance().getReference().child("Upvote");
    mAuth=FirebaseAuth.getInstance();
    recyclerView = (RecyclerView) view.findViewById(R.id.recycleview_new);
    recyclerView.setHasFixedSize(true);

    mLayoutManager=(new LinearLayoutManager(getActivity()));
    recyclerView.setLayoutManager(mLayoutManager);
    mLayoutManager.setReverseLayout(true);
    mDatabaseUpvote.keepSynced(true);
    mDatabase.keepSynced(true);

    if (savedInstanceState!=null){
        int position2 = savedInstanceState.getInt("position");
        recyclerView.getLayoutManager().scrollToPosition(position2);
    }






 firebaseRecyclerAdapter  = new FirebaseRecyclerAdapter<Cards, Postviewholder>(

            Cards.class,
            R.layout.cards,
            Postviewholder.class,
            mDatabase

    ) {
        @Override
        protected void populateViewHolder(final Postviewholder viewHolder, Cards model, final int position) {



            final String postKey=getRef(position).getKey();

            viewHolder.setUsername(model.getUsername());
            viewHolder.setImage(getActivity(),model.getImage());
            viewHolder.setimage_icon(getActivity(),model.getImageicon());
            viewHolder.setTitle(model.getTitle());
            viewHolder.setTextColor(model.getTextColor());
            viewHolder.setUpvote(postKey);



            viewHolder.post_Image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent2= new Intent(getActivity(),View_post.class);
                    intent2.putExtra("Userprofile",postKey);
                    startActivity(intent2);

                }
            });

            viewHolder.imageicon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent2= new Intent(getActivity(),UserActivity_2.class);
                    intent2.putExtra("Userprofile",postKey);
                    startActivity(intent2);


                }
            });





            viewHolder.upvote.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ProcessUpvote = true;



                    mDatabaseUpvote.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (ProcessUpvote) {
                                if (dataSnapshot.child(postKey).hasChild(mAuth.getCurrentUser().getUid())) {

                                    mDatabaseUpvote.child(postKey).child(mAuth.getCurrentUser().getUid()).removeValue();
                                    ProcessUpvote = false;

                                } else {
                                    mDatabaseUpvote.child(postKey).child(mAuth.getCurrentUser().getUid()).setValue("RandomValue");
                                    ProcessUpvote = false;
                                }

                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });


                }
            });
        }

    };
    recyclerView.setAdapter(firebaseRecyclerAdapter);

    return view;
}



@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    Position= mLayoutManager.findLastCompletelyVisibleItemPosition();
    savedInstanceState.putInt("position",Position);
    super.onSaveInstanceState(savedInstanceState);

}

}

1 个答案:

答案 0 :(得分:0)

...
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setOffscreenPageLimit(2); 
// 2 is the number of pages(fragments in the ViewPager)
...