我正在开发类似于instagram的android应用程序。在主要活动中,我使用了四个片段供稿,搜索用户,个人资料等。但是,当我从包含所有帖子的主页片段切换到其他片段时,并且当我返回到主页片段时,我希望它处于相同状态就像我之前离开的一样。但是它又从顶部开始。我该如何解决这个问题?
这是家庭片段的代码。
公共类HomeFragment扩展了片段{
private RecyclerView recyclerView;
private PostAdapter postAdapter;
private ArrayList<Post> postLists;
private ImageView notification;
private ImageView add_post;
private ImageView request_pickup;
private TextView pls_follow, no_posts;
private Button click_follow;
Fragment selectedFragment = null;
SwipeRefreshLayout swipeRefreshLayout;
private List<String> followingList;
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
//setRetainInstance(true);
view = inflater.inflate(R.layout.fragment_home, container, false);
notification = view.findViewById(R.id.notification);
pls_follow = view.findViewById(R.id.pls_follow);
click_follow = view.findViewById(R.id.click_follow);
no_posts = view.findViewById(R.id.no_posts);
notification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext() , NotificationActivity.class);
ContextCompat.startForegroundService(getContext() , intent);
startActivity(intent);
}
});
add_post = view.findViewById(R.id.add_post);
add_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext() , PostSelectActivity.class);
ContextCompat.startForegroundService(getContext() , intent);
startActivity(intent);
}
});
request_pickup=view.findViewById(R.id.request_pickup);
request_pickup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext() , Request_Pickup.class);
ContextCompat.startForegroundService(getContext() , intent);
startActivity(intent);
}
});
recyclerView = view.findViewById(R.id.recycler_view12);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
postLists = new ArrayList<>();
postAdapter = new PostAdapter(getContext() , (List<Post>) postLists);
recyclerView.setAdapter(postAdapter);
progressBar = view.findViewById(R.id.progress_circular);
checkFollowing();
return view;
}
}