简体   繁体   中英

fragment showing but recycler view isn't

The button view shows confirming that the fragment shows, however the recycler view doesn't. The logged array size of "jobs" is 1, confirming that getItemCount() isn't the problem and that the recycler adapter constructor is called. I'm reusing my JobRecyclerAdapter class but nothing's static, so there shouldn't be any interference between the two instances. The layout is aa copy of a working one I have, minus different ids and layout name, so that shouldn't be a problem either. There are no errors.

Layout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <include layout="@layout/top_bar"
        android:id="@+id/topBarAJ"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include layout="@layout/bottom_bar"
        android:id="@+id/bottomBarAJ"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/activeJobRecyclerView"
        app:layout_constraintTop_toBottomOf="@id/topBarAJ"
        app:layout_constraintBottom_toTopOf="@id/bottomBarAJ"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/activeJobRecyclerView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topBarAJ" />
</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerAdapter


    List<Job> jobs;
    List<Drawable> images;
    RecyclerViewClickListener clickListener;
    public JobRecyclerAdapter(List<Job> downloadedJobs, List<Drawable> images, RecyclerViewClickListener clickListener) {
        this.jobs = downloadedJobs;
        this.images = images;
        Integer arraySize = images.size();
        Log.d("downloadActiveJob", "JobRecyclerAdapter: images array size: " + arraySize.toString());
        arraySize = jobs.size();
        Log.d("downloadActiveJob", "JobRecyclerAdapter: jobs array size: " + arraySize.toString());
        this.clickListener = clickListener;
    }

    class JobCardViewHolder extends RecyclerView.ViewHolder {
        public ImageView itemImage;
        public TextView itemName;
        public TextView itemWeight;
        public TextView itemSize;
        public TextView transmitterName;

        public JobCardViewHolder(View v) {
            super(v);//call default constructor
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("RecyclerView", "onClick:" + getAdapterPosition());
                    clickListener.recyclerViewListClicked(v, getAdapterPosition());
                }
            });
            itemImage = (ImageView) v.findViewById(R.id.itemImage);
            itemName = (TextView) v.findViewById(R.id.itemName);
            itemWeight = v.findViewById(R.id.itemWeight);
            itemSize = v.findViewById(R.id.itemSize);
            transmitterName = v.findViewById(R.id.transmitterName);

        }
    }

    public void removeItem(int jobIndex) {
        jobs.remove(jobIndex);
        notifyDataSetChanged();
    }




    @Override
    public JobCardViewHolder onCreateViewHolder(ViewGroup vg, int n) {
        View v = LayoutInflater.from(vg.getContext()).inflate(R.layout.job_card_layout, vg, false);
        JobCardViewHolder vH = new JobCardViewHolder(v);
        return vH;
    }


    @Override
    public void onBindViewHolder(JobCardViewHolder vH, int position) {
        vH.itemName.setText(jobs.get(position).itemName);
        vH.itemImage.setImageDrawable(images.get(position));
        vH.itemWeight.setText(jobs.get(position).itemWeight);
        vH.itemSize.setText(jobs.get(position).itemSize);
        vH.transmitterName.setText(jobs.get(position).transmitterName);
    }


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

Fragment (I only showed the relevant parts of the fragment.)

RecyclerView jobRecyclerView;
JobRecyclerAdapter jobRecyclerAdapter;
LinearLayoutManager llm;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sub_fragment_active_jobs_1, container, false);
        this.jobRecyclerView = view.findViewById(R.id.activeJobRecyclerView);
        return view;
    }
    public void configureJobsRecyclerAdapter(Job activeJob, List<Drawable> imageDrawables) {
        Log.d("downloadActiveJob", "configureJobRecyclerAdapter called");

        List<Job> downloadedJobs = new ArrayList<>();
        downloadedJobs.add(activeJob);

        Integer arraySize = imageDrawables.size();
        Log.d("downloadActiveJob", "configureJobRecyclerAdapter: " + arraySize.toString());

        jobRecyclerAdapter = new JobRecyclerAdapter(downloadedJobs, imageDrawables, this);

        llm = new LinearLayoutManager(context);
        jobRecyclerView.setLayoutManager(llm);
        jobRecyclerView.setAdapter(jobRecyclerAdapter);
    }

Update: If I move the recycler view/adapter setup code to onResume, the recycler view is visible. When I update the recycler view with a new adapter, the recycler view becomes invisible.

If I can't solve it using this new discovery I'll post the whole fragment.

Hey, you create the function configureJobsRecyclerAdapter() but didn't call it inside fragment onCreateView(). Like...

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sub_fragment_active_jobs_1, container, false);
        this.jobRecyclerView = view.findViewById(R.id.activeJobRecyclerView);

        configureJobsRecyclerAdapter()//Add this line.

        return view;
    }

if you are fetching data which are required for configureJobsRecyclerAdapter() from an api maybe you are setting empty list at the time when you are calling configureJobsRecyclerAdapter(). and that's why when you call it in onResume, the list becomes visible(because then data is fetched). and don't forget to call adapter.notifyDataSetChanged() after each change in your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM