【问题标题】:fragment showing but recycler view isn't片段显示但回收站视图不是
【发布时间】:2019-12-18 09:21:03
【问题描述】:

按钮视图显示确认片段显示,但回收器视图没有。 “jobs”的记录数组大小为 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,则回收站视图可见。当我使用新适配器更新回收器视图时,回收器视图变得不可见。

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

【问题讨论】:

  • 我想你忘了调用configureJobsRecyclerAdapter() 函数。
  • 您的 configureJobsRecyclerAdapter 调用在哪里
  • 正如我在问题中所述,调用了回收器适配器构造函数。我只展示了片段的相关部分。
  • @Dylan 您需要在onCreateView() 内的片段上调用configureJobsRecyclerAdapter() 函数。
  • @SaugatJonchhen 阅读了我在您之前的评论。

标签: java android android-recyclerview


【解决方案1】:

嘿,您创建了函数 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;
    }

【讨论】:

  • "我只展示了片段的相关部分"。 "记录的jobs数组大小为1,确认调用了recycler适配器构造函数。"
【解决方案2】:

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

【讨论】:

  • ConfigureRecyclerAdapter() 从 API 回调中调用。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
相关资源
最近更新 更多