我在我的家庭碎片上构建了回收站视图。从Firebase Firestore提取数据。因此,如果我更新Firestore中的任何内容,它都会实时发生。但是问题是我在回收者视图上设置了“单击听众”,这会打开一个活动。在这种活动中,数据不是实时获取的。例如,我参与了该活动,并且在Firestore上进行了一些更新,因此要查看这些更改,我必须返回到片段,然后再次必须进行活动以查看那些更改。如何解决。当我当时处于活动状态时,只有数据更改是从Firestore获取的。
适配器类:
public class PinnedPostAdapter extends RecyclerView.Adapter<PinnedPostAdapter.ViewHolder> {
public List<PinnedPost> pinnedPostList;
private Context context;
private OnItemClicked onClick;
public PinnedPostAdapter (Context context, List<PinnedPost> pinnedPostList) {
this.pinnedPostList = pinnedPostList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pinned_post_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
if (TextUtils.isEmpty(pinnedPostList.get(position).getPinned_post_title())) {
holder.pinnedPosttitle.setText("Not Available");
} else {
holder.pinnedPosttitle.setText(pinnedPostList.get(position).getPinned_post_title());
}
holder.read_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemCl(position, pinnedPostList.get(position));
}
});
}
@Override
public int getItemCount() {
return pinnedPostList.size();
}
public void setOnClick(PinnedPostAdapter.OnItemClicked onClick) {
this.onClick = onClick;
}
public interface OnItemClicked {
void onItemCl(int position, PinnedPost passData);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView pinnedPosttitle;
public Button read_more;
View mView;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
pinnedPosttitle = (TextView) mView.findViewById(R.id.pinned_post_title);
pinnedPosttitle.setSelected(true);
pinnedPosttitle.setSingleLine(true);
read_more = (Button) mView.findViewById(R.id.pinned_post_read_more);
}
}
}
家庭片段:
public class HomeFragment extends Fragment implements PinnedPostAdapter.OnItemClicked {
private static final String Pinned_Post = "PINNED_POST";
private RecyclerView recyclerViewPinnedPosts;
private PinnedPostAdapter pinnedPostAdapter;
private List<PinnedPost> pinnedPostList;
pinnedPostList = new ArrayList<>();
pinnedPostAdapter = new PinnedPostAdapter(getActivity(), pinnedPostList);
recyclerViewPinnedPosts = (RecyclerView) view.findViewById(R.id.pinned_post);
recyclerViewPinnedPosts.setHasFixedSize(true);
recyclerViewPinnedPosts.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
recyclerViewPinnedPosts.setAdapter(pinnedPostAdapter);
pinnedPostAdapter.setOnClick(this);
mFirestore.collection("pinned_post").orderBy("priority", Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "Error : " + e.getMessage());
}
assert documentSnapshots != null;
for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
String doc_id = doc.getDocument().getId();
PinnedPost pinnedPost = doc.getDocument().toObject(PinnedPost.class).withDocId(doc_id);
pinnedPostList.add(doc.getNewIndex(), pinnedPost);
pinnedPostAdapter.notifyDataSetChanged();
} else if (doc.getType() == DocumentChange.Type.MODIFIED) {
String docID = doc.getDocument().getId();
PinnedPost changedModel = doc.getDocument().toObject(PinnedPost.class).withDocId(docID);
if (doc.getOldIndex() == doc.getNewIndex()) {
// Item changed but remained in same position
pinnedPostList.set(doc.getOldIndex(), changedModel);
pinnedPostAdapter.notifyItemChanged(doc.getOldIndex());
} else {
// Item changed and changed position
pinnedPostList.remove(doc.getOldIndex());
pinnedPostList.add(doc.getNewIndex(), changedModel);
pinnedPostAdapter.notifyItemMoved(doc.getOldIndex(), doc.getNewIndex());
}
} else if (doc.getType() == DocumentChange.Type.REMOVED) {
// remove
pinnedPostList.remove(doc.getOldIndex());
pinnedPostAdapter.notifyItemRemoved(doc.getOldIndex());
}
}
}
});
@Override
public void onItemCl(int position, PinnedPost passData) {
Intent intent = new Intent(getActivity(), Pinned_Post_Clcik.class);
Bundle args = new Bundle();
args.putSerializable(Pinned_Post, passData);
intent.putExtras(args);
startActivity(intent);
}
单击“回收者”视图项时要进行的活动。在此活动中,当Firestore中的任何数据发生更改时,不会实时获取数据。我必须返回片段,然后再次进行此活动以查看那些更改。
public class Pinned_Post_Clcik extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pinned_post);
list = (PinnedPost) getIntent().getExtras().getSerializable(Pinned_Post);
mTitle = (TextView) findViewById(R.id.m_PP_title);
if (TextUtils.isEmpty(list.getPinned_post_title())) {
mTitle.setText("Not Available");
} else {
mTitle.setText(list.getPinned_post_title());
}
mDescription = (TextView) findViewById(R.id.m_PP_Description);
if (TextUtils.isEmpty(list.getPinned_post_desc())) {
mDescription.setText("Not Available");
} else {
mDescription.setText(list.getPinned_post_desc());
}
Spanned result;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
result = Html.fromHtml(list.getPinned_post_desc(), Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(list.getPinned_post_desc());
}
mDescription.setText(result);
mDescription.setMovementMethod(LinkMovementMethod.getInstance());
mImageView = (ImageView) findViewById(R.id.mPinnedPostImg);
mProgressBar = (ProgressBar) findViewById(R.id.pb);
FailedImg = (ImageView) findViewById(R.id.ef_failed_image);
if (TextUtils.isEmpty(list.getmImagePinnedPost())) {
mImageView.setImageResource(R.drawable.failed_to_load_img);
mProgressBar.setVisibility(View.GONE);
} else {
Picasso.with(getApplicationContext())
.load(list.getmImagePinnedPost())
.into(mImageView, new Callback() {
@Override
public void onSuccess() {
mProgressBar.setVisibility(View.GONE);
FailedImg.setVisibility(View.GONE);
}
@Override
public void onError() {
mImageView.setImageResource(R.drawable.app_logo);
mProgressBar.setVisibility(View.GONE);
}
});
bottomView = (BottomNavigationView) findViewById(R.id.pinned_post_bottom);
bottomView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.bottom_home:
onBackPressed();
finish();
}
return false;
}
});
}
}
}