gpt4 book ai didi

java - 无法在fragment中实现Recycler View 适配器

转载 作者:行者123 更新时间:2023-12-02 01:39:00 26 4
gpt4 key购买 nike

我无法在 fragment 中附加 Firestore Recycler 适配器。我用谷歌搜索了很多东西,但还没有什么好的。该 fragment 是底部选项卡布局的选项卡之一。数据是从单个集合中检索的。我想在单个 View 中表示多用户帖子列表。

我已经尝试了下面的代码,但还没有效果。

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//just change the fragment_dashboard
//with the fragment you want to inflate
//like if the class is HomeFragment it should have R.layout.home_fragment
//if it is DashboardFragment it should have R.layout.fragment_dashboard
View view = inflater.inflate(R.layout.fragment_home, container, false);
final FragmentActivity c = getActivity();
LinearLayoutManager layoutManager = new LinearLayoutManager(c);

Query query = PostsRef.orderBy("timestamp", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<PostsModel> options = new FirestoreRecyclerOptions.Builder<PostsModel>()
.setQuery(query, PostsModel.class)
.build();
adapter = new PostsAdapter(options);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(c));
recyclerView.setAdapter(adapter);

mParent =view.findViewById(R.id.relative_home);
return inflater.inflate(R.layout.fragment_home, null);


}

适配器代码

public class PostsAdapter extends FirestoreRecyclerAdapter<PostsModel, PostsAdapter.PostsHolder> {
private OnItemClickListener listener;

public PostsAdapter(@NonNull FirestoreRecyclerOptions<PostsModel> options) {
super(options);
}

@Override
protected void onBindViewHolder(@NonNull PostsHolder holder, int position, @NonNull PostsModel model) {
//retrieve the fields here
holder.textViewTitle.setText(model.getPostTitle());
holder.textViewDescription.setText(model.getPostContent());
holder.textViewPriority.setText(String.valueOf(model.getSpinnerC()));
holder.AuthorName.setText(model.getName());
holder.AuthorGender.setText(model.getGender());
}

@NonNull
@Override
public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_layout,
parent, false);
return new PostsHolder(v);
}

public void deleteItem(int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}

class PostsHolder extends RecyclerView.ViewHolder {


TextView textViewTitle;
TextView textViewDescription;
TextView textViewPriority;
TextView AuthorName;
TextView AuthorGender;

public PostsHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.quote1);
textViewDescription = itemView.findViewById(R.id.name1);
textViewPriority = itemView.findViewById(R.id.timestamp1);

}
}

}

最佳答案

您正在 onCreateView 方法中膨胀另一个 View 。

return inflater.inflate(R.layout.fragment_home, null);

关于java - 无法在fragment中实现Recycler View 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57499782/

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