使用改造从api获取回收站视图中的图像和名称会在应用程序android studio中显示空白屏幕

问题描述

我正在开发一个应用,其中我正在使用改型库在我的应用中获取api。我想从回收者视图中的api获取图像和名称的列表。我已将回收站视图布局管理器设置为网格布局。

问题是,当我尝试运行片段以加载这些图像列表时,屏幕显示为空白。在调试日志上,它显示代码缓存容量增加到2 MB,但移动屏幕上的输出为空白。

我从Logcat的api中获得响应,但在应用程序上却没有。

这是适配器类

public class ProductCategoriesAdapter extends RecyclerView.Adapter<ProductCategoriesAdapter.CategoriesViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<CategoryModel> categoryList;

    public ProductCategoriesAdapter(Context context,List<CategoryModel> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
        this.inflater = LayoutInflater.from(context);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ProductCategoriesAdapter.CategoriesViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        View view = inflater.inflate(R.layout.all_category_item,parent,false);
        return new ProductCategoriesAdapter.CategoriesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductCategoriesAdapter.CategoriesViewHolder holder,int position) {
        holder.categoryName.setText(categoryList.get(position).getProductCategoryName());

        String url = categoryList.get(position).getProductImage() ;
        Glide.with(context)
                .asBitmap()
                .load(url)
                .placeholder(R.drawable.sample_product_img)
                .centerInside()
                .into(holder.categoryImage);
    }

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

    class CategoriesViewHolder extends RecyclerView.ViewHolder {

        CircleImageView categoryImage;
        TextView categoryName;

        CategoriesViewHolder(@NonNull View itemView) {
            super(itemView);

            categoryImage = itemView.findViewById(R.id.category_image);
            categoryName = itemView.findViewById(R.id.category_name);

        }
    }
}

在getProductCatList方法中,我正在获取响应并使用适配器将列表设置为recyclerview,并在片段的oncreateview方法上调用此方法

 private void getProductCatList() {

        categoryList = new ArrayList<>();
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        adapter = new ProductCategoriesAdapter(getActivity(),categoryList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);

        progressBar.setVisibility(View.VISIBLE);
        Call<AllCategoriesModel> call = RetrofitProductClient.getInstance().getApi().getAllCategories();

try{
    call.enqueue(new Callback<AllCategoriesModel>() {
    @Override
    public void onResponse(Call<AllCategoriesModel> call,Response<AllCategoriesModel> response) {
                                                                                
    if (response.isSuccessful()) {
                                                                      
   categoryList = Objects.requireNonNull(response.body()).getProductCategories();
                        adapter = new ProductCategoriesAdapter(getContext(),categoryList);
                        adapter.notifyDataSetChanged();
                        progressBar.setVisibility(View.GONE);
                    } else {
                        progressBar.setVisibility(View.GONE);
                        StyleableToast.makeText(getContext(),"failed " + response.message(),R.style.StyledToast).show();
                    }
                }

                @Override
                public void onFailure(Call<AllCategoriesModel> call,Throwable t) {
                    progressBar.setVisibility(View.GONE);
                    StyleableToast.makeText(getContext(),t.getLocalizedMessage(),R.style.StyledToast).show();
                }
            });
        }catch (Exception e)
        {
            StyleableToast.makeText(Objects.requireNonNull(getContext()),e.getLocalizedMessage(),R.style.StyledToast).show();
        }
    }

以下是从api获得响应后的调试日志,并且移动屏幕为空白

I/okhttp.OkHttpClient: <-- END HTTP (2222-byte body)
I/okhttp.OkHttpClient: <-- END HTTP (2222-byte body)
I/okhttp.OkHttpClient: <-- END HTTP (2222-byte body)
I/zygote64: Compiler allocated 7MB to compile void android.widget.TextView.<init>(android.content.Context,android.util.AttributeSet,int,int)
I/Toast: Show toast from OpPackageName:com.test.myapp,PackageName:com.test.myapp
I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()

我该怎么办。有人可以帮忙吗

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)