回收站视图未显示在片段中

时间:2019-07-10 06:58:23

标签: android android-fragments android-recyclerview

我正在尝试创建一个应用程序,当我按下按钮时它将打开包含Recycler视图的片段,在该片段中,数据将从Firebase Firestore重试并添加到适配器。我已经设法将各个部分连接在一起,但是似乎没有显示回收站视图,但是片段中的其他组件却在显示。

任何人都能找出我在代码中缺少的内容或我做错了什么吗?

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

2 个答案:

答案 0 :(得分:3)

我认为您忘记设置适配器的尺寸:-

@Override
    public int getItemCount() {
        return yourList.size();
    }

答案 1 :(得分:0)

已解决

如@Sandeep Malik所述,我首先缺少getItemCount()函数,其次创建了一个自定义适配器来通过构造函数发送数据。它可能不是解决问题的最优雅方法,但确实可以解决问题。

这是更改

MyReportFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReport = new ArrayList<>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_my_report, container, false);
    myRecyclerView = view.findViewById(R.id.myReportRecyclerView);

    Task<QuerySnapshot> query = reportRef
            .whereEqualTo("reportedByUserID","K2TPzSkIjvXCI2m1QGhyYr7qWpw1")
            .orderBy("date",Query.Direction.DESCENDING)
            .get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
                        Report report = documentSnapshot.toObject(Report.class);
                        mReport.add(report);
                    }

                    adapter = new ReportsAdapter(getContext(),mReport);

                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
                    myRecyclerView.setLayoutManager(linearLayoutManager);
                    myRecyclerView.setAdapter(adapter);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d("ERROR", "onFailure: "+e.getMessage());
                }
            });
    return view;
}

ReportsAdapter

public class ReportsAdapter extends RecyclerView.Adapter<ReportsAdapter.ReportHolder> {

    private Context mContext;
    private List<Report> mReports;


    public ReportsAdapter(Context context, List<Report> reports){
        mContext = context;
        mReports = reports;
    }


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

    @Override
    public void onBindViewHolder(@NonNull ReportHolder holder, int position) {

        String seriousLvl = "";

        Report report = mReports.get(position);
        holder.txtTitle.setText(report.getReportType());
        holder.txtDescription.setText(report.getDescription());
        holder.txtDate.setText(report.getDate());
        holder.txtReportedBy.setText(report.getReportedBy());

        seriousLvl = report.getSeriousness();

        if (seriousLvl.equals("Low")) {
            holder.seriousness.setBackgroundColor(Color.argb(100, 0, 255, 11));
        } else if (seriousLvl.equals("Medium")) {
            holder.seriousness.setBackgroundColor(Color.argb(100, 255, 220, 0));
        } else if (seriousLvl.equals("High")) {
            holder.seriousness.setBackgroundColor(Color.argb(100, 255, 0, 0));
        } else {
            Log.d("ERROR", "NEITHER: ");
        }
    }

    @Override
    public int getItemCount() {
        return mReports.size();
    }

    public class ReportHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

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

        @Override
        public void onClick(View view) {

        }
    }
}