Recycler View未显示在片段中 [英] Recycler View not displaying in fragment
问题描述
我正在尝试创建一个应用程序,当我按下一个按钮时,它将打开包含Recycler视图的片段,在该片段中,将从Firebase Firestore重试数据并将其添加到适配器.我已经设法将各个部分连接在一起,但是似乎没有显示回收站视图,但是片段中的其他组件却在显示.
I'm trying to make an application where when i press a button it would open the fragment which contains a Recycler view where the data is retried from Firebase Firestore and added to an adapter. I've managed to connect the pieces together but the recycler view does not seem to be showing but the other components inside the fragments are.
任何人都能找出我在代码中缺少的内容或我做错了什么吗?
Anyone able to find out what am I missing in the code or what I'm doing wrong?
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReport = findViewById(R.id.myReport);
publicReport = findViewById(R.id.publicReport);
myReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myReportFragment();
}
});
/*Opens separate fragment but similar code*/
publicReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
publicReportFragment();
}
});
}
public void publicReportFragment() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.report_fragments, new PublicReportFragment());
fragmentTransaction.commit();
}
public void myReportFragment() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.report_fragments, new MyReportFragment());
fragmentTransaction.commit();
}
activity_main.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/publicReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="74dp"
android:layout_marginLeft="74dp"
android:text="public report" />
<Button
android:id="@+id/myReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="-1dp"
android:layout_marginEnd="67dp"
android:layout_marginRight="67dp"
android:text="public report"
app:layout_anchorGravity="top|center" />
<FrameLayout
android:id="@+id/report_fragments"
android:layout_below="@id/myReport"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MyReportFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_report, container, false);
Query query = reportRef.whereEqualTo("reportByUserID","K2TPzSkIjvXCI2m1QGhyYr7qWpw1").orderBy("date", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Report> options = new FirestoreRecyclerOptions.Builder<Report>()
.setQuery(query, Report.class)
.build();
adapter = new ReportAdapter(options);
myRecyclerView = view.findViewById(R.id.myReportRecyclerView);
myRecyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
myRecyclerView.setLayoutManager(linearLayoutManager);
myRecyclerView.setAdapter(adapter);
return view;
}
fragment_my_report.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyReportFragment">
<!--When button click the text view shows but recycler view doesnt*/-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY REPORTS"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myReportRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
ReportAdapter.java
public class ReportAdapter extends FirestoreRecyclerAdapter<Report, ReportAdapter.ReportHolder>{
public ReportAdapter(@NonNull FirestoreRecyclerOptions<Report> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ReportHolder reportHolder, int i, @NonNull Report report) {
String seriousLvl = "";
reportHolder.txtTitle.setText(report.getReportType());
reportHolder.txtDescription.setText(report.getDescription());
reportHolder.txtDate.setText(report.getDate());
reportHolder.txtReportedBy.setText(report.getReportedBy());
seriousLvl = report.getSeriousness();
if(seriousLvl.equals("Low")){
reportHolder.seriousness.setBackgroundColor(Color.argb(100,0,255,11));
}else if(seriousLvl.equals("Medium")){
reportHolder.seriousness.setBackgroundColor(Color.argb(100,255,220,0));
}else if(seriousLvl.equals("High")){
reportHolder.seriousness.setBackgroundColor(Color.argb(100,255,0,0));
}else{
Log.d("ERROR", "NEITHER: ");
}
}
@NonNull
@Override
public ReportHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,parent,false);
return new ReportHolder(v);
}
class ReportHolder extends RecyclerView.ViewHolder {
TextView txtTitle,txtDescription,txtDate,txtReportedBy;
CardView seriousness;
public ReportHolder(@NonNull View itemView) {
super(itemView);
seriousness = itemView.findViewById(R.id.seriousness);
txtTitle = itemView.findViewById(R.id.text_view_title);
txtDescription = itemView.findViewById(R.id.text_view_description);
txtDate = itemView.findViewById(R.id.text_view_date);
txtReportedBy = itemView.findViewById(R.id.text_view_report_by);
}
}
}
推荐答案
我认为您忘记在适配器中设置大小:-
I think you forgot to set size in your adapter:-
@Override
public int getItemCount() {
return yourList.size();
}
这篇关于Recycler View未显示在片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!