2017-10-06 193 views
0

我想知道如何保存滚动位置和恢复滚动位置,如果我来自另一个活动与onbackpressed按钮。我坚持了这一点,并在过去的几天尝试了很多方法,但都是徒劳的。任何帮助都感激不尽。如何获得相同的滚动位置上的交错网格布局从其他活动显示后的详细信息

以下是我Activity.class

private ImageButton mhomebtn; 
    private ImageButton mexplorebtn; 
    private ImageButton mcamerabtn; 
    private ImageButton mstorebtn; 
    private ImageButton mprofilebtn; 
    private RecyclerView mexplore_page; 
    private StaggeredGridLayoutManager mgrid; 
    private DatabaseReference mdatabase; 
    private DatabaseReference mdatabaseLike; 
    private DatabaseReference mdatabaseSave; 
    private DatabaseReference mdatabasefollow; 
    private DatabaseReference mdatabasefollowers; 
    private boolean mProcessSave = false; 
    private boolean mProcessLike = false; 
    private FirebaseAuth mAuth; 
    private static final int TIME_OUT = 2000; 
    private String mCurrent_state; 
    private Animation mslide_up; 
    private static int mPosition; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_explore); 

     mAuth = FirebaseAuth.getInstance(); 
     String uid = mAuth.getCurrentUser().getUid(); 

     mdatabase = FirebaseDatabase.getInstance().getReference().child("Explores"); 
     mdatabaseLike = FirebaseDatabase.getInstance().getReference().child("Like"); 
     mdatabaseSave = FirebaseDatabase.getInstance().getReference().child("Saved"); 
     mdatabasefollow = FirebaseDatabase.getInstance().getReference().child("Following"); 
     mdatabasefollowers = FirebaseDatabase.getInstance().getReference().child("Followers"); 
     mdatabasefollowers.keepSynced(true); 
     mdatabaseLike.keepSynced(true); 
     mdatabaseSave.keepSynced(true); 
     mdatabasefollow.keepSynced(true); 
     mhomebtn = (ImageButton) findViewById(R.id.homebtn); 
     mexplorebtn = (ImageButton) findViewById(R.id.explorebtn); 
     mcamerabtn = (ImageButton) findViewById(R.id.camerabtn); 
     mstorebtn = (ImageButton) findViewById(R.id.storebtn); 
     mprofilebtn = (ImageButton) findViewById(R.id.profilebtn); 
     mslide_up = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up); 
     mslide_up.setAnimationListener(this); 
     mgrid = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
     mgrid.setReverseLayout(true); 
     mexplore_page = (RecyclerView) findViewById(R.id.explore_page); 
     mexplore_page.setHasFixedSize(true); 
     mexplore_page.setLayoutManager(mgrid); 

     mCurrent_state = "not_following"; 

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

       startActivity(new Intent(ExploreActivity.this, HomeActivity.class)); 
      } 
     }); 



     mexplorebtn.setImageResource(R.mipmap.ic_explore_blue); 


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

       startActivity(new Intent(ExploreActivity.this, CameraActivity.class)); 
      } 
     }); 

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

       startActivity(new Intent(ExploreActivity.this, StoreActivity.class)); 
      } 
     }); 

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

       startActivity(new Intent(ExploreActivity.this, ProfileActivity.class)); 
      } 
     }); 

    } 





    @Override 
    protected void onStart() { 
     super.onStart(); 

     FirebaseRecyclerAdapter <Explores, ExploreViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Explores, ExploreViewHolder>(
       Explores.class, 
       R.layout.explore_row, 
       ExploreViewHolder.class, 
       mdatabase 
     ) { 
      @Override 
      protected void populateViewHolder(final ExploreViewHolder viewHolder, Explores model, final int position) { 

       final String post_key = getRef(position).getKey(); 
       mPosition = position; 

       viewHolder.setPostimage(getApplicationContext(), model.getPostimage()); 
       viewHolder.setLikebtn(post_key); 
       viewHolder.setAddgallerybtn(post_key); 

       final String uid = mAuth.getCurrentUser().getUid(); 




       mdatabase.child(post_key).addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 

         String right = dataSnapshot.child("right").getValue(String.class); 
         String center = dataSnapshot.child("center").getValue(String.class); 
         String left = dataSnapshot.child("lefttop").getValue(String.class); 
         final String category = dataSnapshot.child("category").getValue(String.class); 
         final String description = dataSnapshot.child("description").getValue(String.class); 
         final String heading = dataSnapshot.child("heading").getValue(String.class); 

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


           startActivity(new Intent(ExploreActivity.this, DetailActivity.class)); 



          } 
         }); 








         final String post_uid = dataSnapshot.child("uid").getValue(String.class); 


        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 




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

         mProcessSave = true; 

         mdatabaseSave.child(uid).addValueEventListener(new ValueEventListener() { 
          @Override 
          public void onDataChange(DataSnapshot dataSnapshot) { 

           if (mProcessSave){ 

            if (dataSnapshot.hasChild(post_key)){ 

             new Handler().postDelayed(new Runnable() { 
              @Override 
              public void run() { 

               viewHolder.mmessagetext.setVisibility(View.VISIBLE); 

              } 
             },TIME_OUT); 


             mProcessSave = false; 
            }else { 

             mdatabase.child(post_key).addValueEventListener(new ValueEventListener() { 
              @Override 
              public void onDataChange(DataSnapshot dataSnapshot) { 

               DatabaseReference newSaved = mdatabaseSave.child(uid).child(post_key); 

               newSaved.child("postimage").setValue(dataSnapshot.child("postimage").getValue()); 
               newSaved.child("heading").setValue(dataSnapshot.child("heading").getValue()).addOnCompleteListener(new OnCompleteListener<Void>() { 
                @Override 
                public void onComplete(@NonNull Task<Void> task) { 

                } 
               }); 
              } 

              @Override 
              public void onCancelled(DatabaseError databaseError) { 

              } 
             }); 


             mProcessSave = false; 

            } 
           } 
          } 

          @Override 
          public void onCancelled(DatabaseError databaseError) { 

          } 
         }); 

        } 
       }); 



       mdatabaseLike.child(post_key).addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 

         long likecounts = dataSnapshot.getChildrenCount(); 
         mdatabase.child(post_key).child("likecounts").setValue(likecounts); 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 

       mdatabase.child(post_key).addValueEventListener(new ValueEventListener() { 
        @SuppressWarnings("ConstantConditions") 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 


         if(dataSnapshot.hasChild("likecounts")){ 

          int count = dataSnapshot.child("likecounts").getValue(int.class); 
         }else { 

          Toast.makeText(ExploreActivity.this, "Post has been deleted successfully", Toast.LENGTH_SHORT).show(); 

         } 

         String post_uid = dataSnapshot.child("uid").getValue(String.class); 
         viewHolder.midtext.setText(post_uid); 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 

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

         mProcessLike = true; 

         mdatabaseLike.addValueEventListener(new ValueEventListener() { 
          @Override 
          public void onDataChange(DataSnapshot dataSnapshot) { 

           if (mProcessLike){ 

            if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){ 

             mdatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue(); 

             mProcessLike = false; 
            }else { 

             mdatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).setValue(mAuth.getCurrentUser().getEmail()); 

             mProcessLike = false; 
            } 
           } 

          } 

          @Override 
          public void onCancelled(DatabaseError databaseError) { 

          } 
         }); 


        } 
       }); 


      } 
     }; 

     mexplore_page.setAdapter(firebaseRecyclerAdapter); 
    } 

    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 

    public static class ExploreViewHolder extends RecyclerView.ViewHolder{ 

     View mView; 
     ImageButton mlikebtn; 
     ImageButton maddgallerybtn; 
     TextView mmessagetext; 
     DatabaseReference mdatabaseLike; 
     DatabaseReference mdatabaseSave; 
     TextView midtext; 
     DatabaseReference mdatabasefollow; 
     DatabaseReference mdatabasefollowers; 
     FirebaseAuth mAuth; 
     TextView mrighttext; 
     TextView mcentertext; 
     TextView mlefttext; 
     TextView mheadinghere; 
     TextView mfollow; 
     TextView mdescription; 
     TextView mcategory; 
     TextView mlocation; 
     TextView mcameraused; 
     DatabaseReference mdatabase; 
     ScrollView mdetailscroll; 


     private static final int TIME_OUT = 100; 
     public ExploreViewHolder(View itemView) { 
      super(itemView); 
      mView = itemView; 

      mheadinghere = (TextView) mView.findViewById(R.id.headinghere); 
      mfollow = (TextView) mView.findViewById(R.id.follow); 
      mdescription = (TextView) mView.findViewById(R.id.deschere); 
      mcategory = (TextView) mView.findViewById(R.id.category); 
      mlocation = (TextView) mView.findViewById(R.id.location); 
      mcameraused = (TextView) mView.findViewById(R.id.cameraused); 
      mlikebtn = (ImageButton) mView.findViewById(R.id.likebtn); 
      maddgallerybtn = (ImageButton) mView.findViewById(R.id.addgallerybtn); 
      mmessagetext = (TextView) mView.findViewById(R.id.messagetext); 
      mdatabaseLike = FirebaseDatabase.getInstance().getReference().child("Like"); 
      mdatabaseLike.keepSynced(true); 
      mdatabaseSave = FirebaseDatabase.getInstance().getReference().child("Saved"); 
      mdatabaseSave.keepSynced(true); 
      midtext = (TextView) mView.findViewById(R.id.idtext); 
      mdatabasefollow = FirebaseDatabase.getInstance().getReference().child("Follow"); 
      mdatabasefollow.keepSynced(true); 
      mdatabasefollowers = FirebaseDatabase.getInstance().getReference().child("Followers"); 
      mdatabasefollowers.keepSynced(true); 
      mAuth = FirebaseAuth.getInstance(); 
      mdatabase = FirebaseDatabase.getInstance().getReference().child("Explores"); 
      mdetailscroll = (ScrollView) mView.findViewById(R.id.detailscroll); 

     } 

     public void setAddgallerybtn (final String post_key){ 

      mdatabaseSave.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 

        if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(post_key)){ 

         maddgallerybtn.setImageResource(R.mipmap.ic_assignment_turned_in); 
        }else { 

         maddgallerybtn.setImageResource(R.mipmap.ic_assignment_returned); 
        } 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 
     } 

     public void setLikebtn (final String post_key){ 

      mdatabaseLike.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 

        if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){ 

         mlikebtn.setImageResource(R.mipmap.ic_favorite_red); 
        }else { 

         mlikebtn.setImageResource(R.mipmap.ic_favorite_border); 

        } 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 

     } 

     public void setPostimage (final Context ctx, final String postimage){ 

      final ImageView mpostimage = (ImageView) mView.findViewById(R.id.postimage); 
      Picasso.with(ctx).load(postimage).networkPolicy(NetworkPolicy.OFFLINE).into(mpostimage, new Callback() { 
       @Override 
       public void onSuccess() { 

       } 

       @Override 
       public void onError() { 

        Picasso.with(ctx).load(postimage).into(mpostimage); 
       } 
      }); 
     } 


    } 


} 

这是Activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.selftize.snapblink.ExploreActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/explore_page" 
     android:layout_above="@+id/id_views"> 
    </android.support.v7.widget.RecyclerView> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="#000000" 
     android:id="@+id/id_views" 
     android:layout_above="@+id/bottomoption"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="#ffffff" 
     android:orientation="horizontal" 
     android:id="@+id/bottomoption"> 

     <ImageButton 
      android:id="@+id/homebtn" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_home" /> 

     <ImageButton 
      android:id="@+id/explorebtn" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_explore" /> 

     <ImageButton 
      android:id="@+id/camerabtn" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_camera" /> 

     <ImageButton 
      android:id="@+id/storebtn" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_store" /> 

     <ImageButton 
      android:id="@+id/profilebtn" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_person" /> 

    </LinearLayout> 

</RelativeLayout> 

如果你想知道别的比现有的资料分开,请向我,帮我实现它。

+0

你完成你以前的活动,而对去另一项活动? –

+0

不,我刚使用了startactivity(new intent(oneactivity.this,newactivity.class)); –

回答

0

尝试重写以下方法

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    if(savedInstanceState != null) { 
     recyclerViewState = savedInstanceState.getParcelable("statekey"); 
} 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    recyclerViewState = mexplore_page.getLayoutManager().onSaveInstanceState();//save 
    outState.putParcelable("statekey", recyclerViewState); 
} 

不要忘记申报

private Parcelable recyclerViewState; 

然后添加的onResume这样

@Override 
protected void onResume() { 
    super.onResume(); 
    if (recyclerViewState != null) { 
     mexplore_page.getLayoutManager().onRestoreInstanceState(recyclerViewState); 
    } 
} 
+0

我试过你的答案,但它仍然是一样的。你知道任何其他方式来做到这一点吗? –

+0

尝试在onSaveInstanceState上设置一个调试点,检查该方法是否正在调用,如果是,则检查当恢复视图时是否在onRestoreInstanceState中使用recyclerViewState的已保存值 –

+0

我试过并得到一条指向recyclerViewState = null的消息。 –

相关问题