如何在 FirestoreRecyclerAdapter 中实现 onClick 以打开新的片段



我正在尝试在我的回收器视图中实现OnClick,但是我在如何解决此问题的所有搜索中都惨遭失败。我想通过单击我的卡片视图转到另一个活动/更好的片段,其中包含另一个可扩展列表,但我现在甚至无法做到这一点。任何帮助都非常感谢

MainActivity code
package com.oleg.firestoretest;

    public static final String TAG = "FireLog";
    private CollectionReference noteBookRef;
    private FirebaseFirestore mFirestore;
    private NoteAdapter adapter;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFirestore = FirebaseFirestore.getInstance();
        noteBookRef = mFirestore.collection("Notebook");
        setupRecyclerView();


    }
    private void setupRecyclerView() {
        Query query = noteBookRef.orderBy("priority", Query.Direction.DESCENDING);
        FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
                .setQuery(query,Note.class)
                .build();
        adapter = new NoteAdapter(options);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }
    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }

}

    public Note() {
    }
    public Note(String title, String content, int priority) {
        this.title = title;
        this.content = content;
        this.priority = priority;
    }
    public String getTitle() {
        return title;
    }
    public String getContent() {
        return content;
    }
    public int getPriority() {
        return priority;
    }
}

public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> {
    private Context mContext;



    public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
        super(options);
    }
    @Override
    protected void onBindViewHolder(@NonNull NoteHolder noteHolder, int i, @NonNull Note note) {
        noteHolder.title.setText(note.getTitle());
        noteHolder.content.setText(note.getContent());
        noteHolder.priority.setText(String.valueOf(note.getPriority()));
    }


    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,
                parent, false);
        return new NoteHolder(v);
    }
    class NoteHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView content;
        TextView priority;

        public NoteHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title_text);
            content = itemView.findViewById(R.id.content_text);
            priority = itemView.findViewById(R.id.priority_text);

                }

    }
    }

这里有你需要的代码:

class NoteHolder extends RecyclerView.ViewHolder {
            TextView title;
            TextView content;
            TextView priority;
            CardView cardView;

            public NoteHolder(@NonNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title_text);
                content = itemView.findViewById(R.id.content_text);
                priority = itemView.findViewById(R.id.priority_text);
                cardView = itemView.findViewById(R.id.cardView);

            }
            private void bind(){
                cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(FirstActivity.this, SecoundActivity.class);
                        startActivity(intent);
                    }
                });
            }

        }

用这段代码修复了它!!

        public NoteHolder(@NonNull final View itemView) {
        super(itemView);
        cardView = itemView.findViewById(R.id.cardview);
        title = itemView.findViewById(R.id.title_text);
        content = itemView.findViewById(R.id.content_text);
        priority = itemView.findViewById(R.id.priority_text);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemView.getContext().startActivity(new Intent(itemView.getContext(),Empty.class));
            }
        });

            }

相关内容

  • 没有找到相关文章

最新更新