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