gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:26:30 25 4
gpt4 key购买 nike

我正在尝试在我的回收 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com