java - 如何在FirestoreRecyclerAdapter中实现onClick来打开新的Fragment?

标签 java android google-cloud-firestore android-recyclerview firebaseui

我正在尝试在我的回收 View 中实现 OnClick,但我在所有搜索如何解决此问题的过程中都惨遭失败。 我想通过单击我的卡片 View 转到另一个 Activity/甚至更好的带有另一个可扩展列表的 fragment ,但我现在甚至无法执行此操作。 非常感谢任何帮助

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

            }



        }

关于java - 如何在FirestoreRecyclerAdapter中实现onClick来打开新的Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53925969/

相关文章:

java - 尝试使用标签名称使用新的 XML 节点更新同一 XML 文件

java - 动态添加彼此相邻的按钮-RelativeLayout

android - 如何解决 Manifest merge failed Error

android - 在一个Google Play开发者帐户中使用不同的软件包名称

java - 如何避免使用带有 firestore 的 snapshotListener 删除消息的 onEvent(QuerySnapshot snapshots, FirestoreException error) 调用?

java - 组合字符串 - 相同字符的运行保持在一起

java - 将用户 ID 与其他模型类一起使用

安卓.view.WindowManager$BadTokenException : Unable to add window — token null is not for an application

Firebase 云函数身份验证触发时间

node.js - 如何使用云函数在firestore中创建新字段?