当我使用Recycler-View Click侦听器时,无法从Firestore实时获取数据吗?

时间:2019-04-06 14:09:41

标签: java android firebase android-recyclerview google-cloud-firestore

在Fragment中,我有recycler-view,它从firestore实时获取数据。我已在该回收站视图上设置了单击侦听器,这将打开一个新活动并显示一些我想显示的数据。但是问题是当我在家庭片段上并在Firestore中为特定文档进行任何更改时。更改立即在我的回收者视图中发生。但是当涉及到recycler-view时,单击侦听器不会实时更改。

例如:我点击了回收站视图中的一项,这是一项活动。在那个活动中,数据来自那个特定的文件。在这个时候,如果我对数据进行任何更改,它不会立即发生。我必须再次返回片段,然后转到该点击侦听器活动以查看那些更改。

以下是我的代码:

回收站视图适配器:

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);

        recyclerViewFeaturedDeal = (RecyclerView) view.findViewById(R.id.featuredDealRecyclerView);

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);
    }

活动代码:

这是单击“回收站”视图时我要进行的活动的代码。进行任何更改时,它不会实时获取数据。我必须回到家庭片段,然后再次必须进行此活动才能看到更改。

public class Pinned_Post_Clcik extends AppCompatActivity {

    private static final String Pinned_Post = "PINNED_POST";

    private PinnedPost list;


    private TextView mTitle, mDescription;
    private BottomNavigationView bottomView;
    private ImageView mImageView, FailedImg;
    private ProgressBar mProgressBar;

    private FirebaseAnalytics mfirebaseAnalytics;


    @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;
                }
            });

        }
    }

}

0 个答案:

没有答案