我的主要活动包含视图寻呼机,它有3个标签,每个标签包含一个片段,每个片段包含一个recyclerview。单击recyclerview中的项目时,将启动新活动。返回时,片段中的recylerview从初始位置开始。我想知道如何恢复Recyclerview的位置?
这是主要活动
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private com.whiteturtlestudio.mywalls1.SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
int vibrantColor = -1, mutedColor = -1;
int defaultColor = 0x000000;
DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Starting.");
Utils.getDatabase();
mSectionsPageAdapter = new com.whiteturtlestudio.mywalls1.SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
mViewPager.setCurrentItem(1);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Button like_frm_btn = (Button) findViewById(R.id.liked_fragement_btn);
like_frm_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
LikedActivity.class);
startActivity(myIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
/*ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();*/
NavigationView navigationView = (NavigationView) findViewById(R.id.NavigationDrawer);
navigationView.getMenu().getItem(0).setChecked(true);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
}if (id == R.id.nav_donate) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_rate_us) {
} else if (id == R.id.nav_about) {
Intent i = new Intent(MainActivity.this, AboutActivity.class);
MainActivity.this.startActivity(i);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
private void setupViewPager(ViewPager viewPager) {
com.whiteturtlestudio.mywalls1.SectionsPageAdapter adapter = new com.whiteturtlestudio.mywalls1.SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragement(new tab1fragment(), "");
adapter.addFragement(new tab2fragment(), "");
adapter.addFragement(new tab3fragment(), "");
viewPager.setAdapter(adapter);
}
}
这是我的片段,其中包含recyclerview
public class tab2fragment extends Fragment {
private RecyclerView home_rec;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseLikes;
private static Context context = null;
public static boolean Likestate = false;
private static String android_id;
private static int width;
private static int height;
private static DisplayMetrics dm;
private static String DatabaseName;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab2,container,false);
android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
DatabaseName = "HomeData";
mDatabase= FirebaseDatabase.getInstance().getReference().child(DatabaseName);
mDatabaseLikes = FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabase.keepSynced(true);
mDatabaseLikes.keepSynced(true);
context=getActivity();
home_rec=(RecyclerView)view.findViewById(R.id.RecyclerHome);
home_rec.setHasFixedSize(true);
home_rec.setLayoutManager(new LinearLayoutManager(getActivity()));
dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
return view;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<HomeData,WallViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HomeData, WallViewHolder>
(HomeData.class,R.layout.home_item,WallViewHolder.class,mDatabase) {
@Override
protected void populateViewHolder(WallViewHolder viewHolder, final HomeData model, int position) {
final String Wall_id = model.getId();
final String wallOrderID = getRef(position).getKey();
//Toast.makeText(context,wallOrderID,Toast.LENGTH_SHORT).show();
viewHolder.setName(model.getName());
viewHolder.setCategory(model.getCategory());
viewHolder.setLikes(model.getLikes(), Wall_id, wallOrderID);
viewHolder.setUrl(getContext(), model.getUrl());
viewHolder.setCat_url(getContext(), model.getCat_url());
viewHolder.setHigh_res_url(model.getHigh_res_url());
viewHolder.setArtist_name(model.getArtist_name());
viewHolder.setArtist_url(model.getArtist_url());
viewHolder.setSource(model.getSource());
viewHolder.setId(model.getId());
viewHolder.setLikeBtn(Wall_id, android_id, model.getLikes());
viewHolder.mlike_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Likestate = true;
final String likes = model.getLikes();
mDatabaseLikes.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(Likestate) {
if (dataSnapshot.child(Wall_id).hasChild(android_id)) {
mDatabaseLikes.child(Wall_id).child(android_id).removeValue();
Likestate = false;
//decr count
int likeplus = Integer.parseInt(likes.replaceAll("[\\D]", ""));
likeplus = likeplus - 1;
mDatabase.child(wallOrderID).child("likes").setValue(Integer.toString(likeplus));
} else {
mDatabaseLikes.child(Wall_id).child(android_id).setValue("Random");
Likestate = false;
//incr count
int likeplus =Integer.parseInt(likes.replaceAll("[\\D]", ""));
likeplus = likeplus + 1;
mDatabase.child(wallOrderID).child("likes").setValue(Integer.toString(likeplus));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
home_rec.setAdapter(firebaseRecyclerAdapter);
}
public static class WallViewHolder extends RecyclerView.ViewHolder
{
String wall_name;
String no_likes;
Bitmap wall_image;
Bitmap cat_image;
ImageView post_img;
View mView;
String high_res;
String artist_name_val;
String category_name;
String artist_url_val;
ImageView mlike_btn;
String source_val;
String id_val;
String Wall_id;
String wallOrderID;
String thumb_url;
DatabaseReference mDatabaseLikes;
public WallViewHolder(View itemView)
{
super(itemView);
mView=itemView;
mlike_btn = (ImageView)mView.findViewById(R.id.like_btn);
mDatabaseLikes = FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabaseLikes.keepSynced(true);
post_img = (ImageView)mView.findViewById(R.id.home_img);
post_img.getLayoutParams().height = width;
post_img.getLayoutParams().width = width;
post_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent go = new Intent(context, WallpaperActivity.class);
go.putExtra("wall_name", wall_name);
go.putExtra("no_likes", no_likes);
go.putExtra("high_res_url", high_res);
go.putExtra("artist_name", artist_name_val);
go.putExtra("category_name",category_name);
go.putExtra("artist_url", artist_url_val);
go.putExtra("Wall_id", Wall_id);
go.putExtra("android_id", android_id);
go.putExtra("DBname", DatabaseName);
go.putExtra("website", source_val);
go.putExtra("wallOrder", wallOrderID);
go.putExtra("thumbUrl", thumb_url);
createImageFromBitmap(wall_image);
createCatImageFromBitmap(cat_image);
context.startActivity(go);
}
});
/*ImageView popup = (ImageView)mView.findViewById(R.id.popup_but);
popup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent pop = new Intent(context, popup.class);
context.startActivity(pop);
}
});*/
}
public void setLikeBtn(final String Wall_id, final String android_id, final String likes){
mDatabaseLikes.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(Wall_id).hasChild(android_id)) {
mlike_btn.setImageResource(R.drawable.like_pressed);
} else {
mlike_btn.setImageResource(R.drawable.unliked_heart);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setName(String name)
{
TextView img_name = (TextView)mView.findViewById(R.id.home_img_text);
img_name.setText(name);
wall_name = name;
}
public void setLikes(String likes, String Wall, String wallOrder)
{
TextView post_likes = (TextView)mView.findViewById(R.id.home_likes);
post_likes.setText(likes);
no_likes = likes;
Wall_id = Wall;
wallOrderID = wallOrder;
}
public void setCategory(String category)
{
TextView img_name = (TextView)mView.findViewById(R.id.home_cat_text);
img_name.setText(category);
category_name = category;
}
public void setUrl(Context ctx, String url){
//Picasso.with(ctx).load(url).into(target);
Glide.with(itemView.getContext())
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
wall_image = resource;
post_img.setImageBitmap(wall_image);
}
});
thumb_url = url;
}
public void setCat_url(Context ctx, String cat_url){
Glide.with(itemView.getContext())
.asBitmap()
.load(cat_url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
cat_image = resource;
ImageView hom_cat_img = (ImageView)mView.findViewById(R.id.home_cat_img);
hom_cat_img.setImageBitmap(cat_image);
hom_cat_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context,CategoryActivity.class);
String category = category_name.toLowerCase();
i.putExtra("Category_name",category);
context.startActivity(i);
}
});
}
});
}
public void setHigh_res_url(String high_res_url){
high_res = high_res_url;
}
public void setArtist_name(String artist_name){
artist_name_val = artist_name;
}
public void setArtist_url(String artist_url){
artist_url_val = artist_url;
}
public void setSource(String source) { source_val = source; }
public void setId(String Id) { id_val = Id; }
public String createImageFromBitmap(Bitmap bitmap) {
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
public String createCatImageFromBitmap(Bitmap bitmap) {
String fileName = "my_Cat_Image";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
}
@Override
public void onPause()
{
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}