我正在搜索如何保存和恢复我的应用程序的回收站视图位置(从未尝试实现保存和恢复回收站视图的位置),在看到许多不同的实现方式后,我发现了一种有趣的方式,即 recyclerview:1.2。 0-alpha02 它是自动完成的 这里是一篇 中等文章
我已经阅读了这篇文章,但我不知道如何在我的代码中准确地实现它
请原谅我写这篇文章的非常不专业的方式,因为我真的不知道如何实现一个回收器视图来保存滚动并恢复位置
activities
我想要保存和恢复位置的功能是 Instagram 或 youtube 或任何具有无限滚动的大型社交媒体或应用程序,如果你在不同或仍然之间切换,fragments
当你回来时,回收站视图的位置保持不变
这是代码// 代码没有任何 recyclerview:1.2.0-alpha02 的实现,因为正如我上面所说,我不知道如何实现它
注意:-如果有人想要我的代码的更多参考,请告诉我我会更新问题,我使用了 StaggeredGridLayoutManager 只是提到它,以防有必要知道
Home_Fragment.java
private final boolean loading = true;
private final String KEY_RECYCLER_STATE = "recycler_state";
public List<Upload> mUploads;
PostAdapter_Home postsAdapter;
ShimmerFrameLayout shimmerFrameLayout;
private RecyclerView postRecyclerView;
// private Parcelable listState;
private int pastVisibleItems, visibleItemCount, totalItemCount;
// Boolean isScrolling = false;
// int currentItems, totalItems, scrolledOutItems;
@SuppressLint("SourceLockedOrientationActivity")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
MaterialToolbar materialToolbar = view.findViewById(R.id.toolbar);
materialToolbar.setOnMenuItemClickListener(toolbarItemClickListener);
postRecyclerView = view.findViewById(R.id.recyclerViewHome);
shimmerFrameLayout = view.findViewById(R.id.shimmerEffect);
// this is for one item per scroll
// SnapHelper snapHelper = new PagerSnapHelper();
// snapHelper.attachToRecyclerView(verticalRecyclerView);
postRecyclerView.setAdapter(postsAdapter);
// listState = savedInstanceState.getParcelable("ListState");
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
postRecyclerView.setLayoutManager(
staggeredGridLayoutManager // I have 3 rows
);
// postRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
// @Override
// public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
//
// visibleItemCount = staggeredGridLayoutManager.getChildCount();
// totalItemCount = staggeredGridLayoutManager.getItemCount();
// int[] firstVisibleItems = null;
// firstVisibleItems = staggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
// if (firstVisibleItems != null && firstVisibleItems.length > 0) {
// pastVisibleItems = firstVisibleItems[0];
// }
//
// if (loading) {
// if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
// loading = false;
// getData();
// Log.d("tag", "LOAD NEXT ITEM");
// }
// }
// }
// });
// setupFirebaseAuth();
getData();
shimmerFrameLayout.startShimmer();
mUploads = new ArrayList<>();
postsAdapter = new PostAdapter_Home(getContext(), mUploads);
postRecyclerView.setAdapter(postsAdapter);
postRecyclerView.scrollToPosition(Home_Fragment.saved_position);
return view;
}
private void getData() {
databaseReference.addValueEventListener(new ValueEventListener() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
shimmerFrameLayout.stopShimmer();
shimmerFrameLayout.setVisibility(View.GONE);
postRecyclerView.setVisibility(View.VISIBLE);
mUploads.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Upload upload = dataSnapshot.getValue(Upload.class);
assert upload != null;
upload.setmKey(dataSnapshot.getKey());
mUploads.add(upload);
}
}
//notify the adapter
postsAdapter.notifyDataSetChanged();
// loading = true;
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
// loading = true;
}
});
}
PostAdapter_Home.java
public class PostAdapter_Home extends RecyclerView.Adapter<PostAdapter_Home.PostViewHolder> {
public static List<Upload> mUploads;
public Context mcontext;
public PostAdapter_Home(Context context, List<Upload> uploads) {
mUploads = uploads;
mcontext = context;
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(mcontext).inflate(R.layout.ex_home, parent, false);
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
.setBaseColor(Color.parseColor("#F3F3F3"))
.setBaseAlpha(1)
.setHighlightColor(Color.parseColor("#E7E7E7"))
.setHighlightAlpha(1)
.setDropoff(50)
.build();
ShimmerDrawable shimmerDrawable = new ShimmerDrawable();
shimmerDrawable.setShimmer(shimmer);
Upload uploadCurrent = mUploads.get(position);
Glide.with(mcontext)
.load(uploadCurrent.getmImageUrl())
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.placeholder(shimmerDrawable)
.centerCrop()
.fitCenter()
.into(holder.imageView);
// holder.imageView.setOnClickListener(view -> changeScaleType(holder, position));
}
@Override
public int getItemCount() {
return mUploads.size();
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
private final ShapeableImageView imageView;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imagePostHome);
}
}
}