显示片段,但没有回收站视图

时间:2019-12-18 09:21:03

标签: java android android-recyclerview recycler-adapter

将显示按钮视图,以确认是否显示了片段,但是没有显示回收站视图。 记录的“作业”的数组大小为1,确认getItemCount()不是问题,并且调用了回收站适配器构造函数。我正在重用JobRecyclerAdapter类,但是没有什么是静态的,因此两个实例之间不应有任何干扰。布局是我拥有的工作副本的副本,减去了不同的ID和布局名称,因此也不是问题。没有错误。

布局

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

片段(我只显示了片段的相关部分。)

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

更新:如果我将回收站视图/适配器设置代码移至onResume,则可以看到回收站视图。当我使用新的适配器更新回收器视图时,回收器视图变得不可见。

如果我不能使用这个新发现解决它,我将发布整个片段。

2 个答案:

答案 0 :(得分:0)

  

嘿,您创建了函数configureJobsRecyclerAdapter(),但是没有在片段onCreateView()中调用它。喜欢...

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

答案 1 :(得分:0)

如果要从api中获取configureJobsRecyclerAdapter()所需的数据,则可能是在调用configureJobsRecyclerAdapter()时设置了空列表。这就是为什么当您在onResume中调用它时,列表变得可见的原因(因为随后会提取数据)。 并且不要忘记在列表中的每次更改后调用adapter.notifyDataSetChanged()。