使用改型从api中获取回收器视图中的图像和名称会在应用程序android studio中显示空白屏幕
我正在开发一个应用程序,其中我使用改装库在我的应用程序中获取api。我想从回收器视图中的api中获取图像和名称列表。我已将回收器视图lyout管理器设置为网格布局 问题是,当我试图运行片段来加载这些图像列表时,屏幕显示为空白。在调试日志上显示将代码缓存容量增加到2MB,但手机屏幕上的输出为空 我在logcat中收到api的响应,但在应用程序中没有 这是适配器类使用改型从api中获取回收器视图中的图像和名称会在应用程序android studio中显示空白屏幕,android,android-recyclerview,retrofit,Android,Android Recyclerview,Retrofit,我正在开发一个应用程序,其中我使用改装库在我的应用程序中获取api。我想从回收器视图中的api中获取图像和名称列表。我已将回收器视图lyout管理器设置为网格布局 问题是,当我试图运行片段来加载这些图像列表时,屏幕显示为空白。在调试日志上显示将代码缓存容量增加到2MB,但手机屏幕上的输出为空 我在logcat中收到api的响应,但在应用程序中没有 这是适配器类 public class ProductCategoriesAdapter extends RecyclerView.Adapter&l
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);
}
}
}
公共类ProductCategoriesAdapter扩展了RecyclerView.Adapter{
私人语境;
私人充气机;
私有列表分类列表;
公共产品类别适配器(上下文、列表类别列表){
this.context=上下文;
this.categoryList=categoryList;
this.inflater=layoutiner.from(上下文);
notifyDataSetChanged();
}
@非空
@凌驾
public ProductCategoriesAdapter.CategoriesViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图=充气机。充气(R.layout.all_category_item,parent,false);
返回新的ProductCategoriesAdapter.CategoriesViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(@NonNull ProductCategoriesAdapter.CategoriesViewHolder,int位置){
holder.categoryName.setText(categoryList.get(position.getProductCategoryName());
字符串url=categoryList.get(position.getProductImage();
带(上下文)滑动
.asBitmap()
.load(url)
.占位符(R.drawable.sample\u product\u img)
.centerInside()
.进入(持有人类别图像);
}
@凌驾
public int getItemCount(){
返回categoryList.size();
}
类分类ViewHolder扩展了RecyclerView.ViewHolder{
CircleImageView类别图像;
TextView类别名称;
CategoriesViewHolder(@NonNull View itemView){
超级(项目视图);
categoryImage=itemView.findViewById(R.id.category\U图像);
categoryName=itemView.findViewById(R.id.category\U名称);
}
}
}
在getProductCatList方法中,我得到响应并将列表设置为recyclerview with adapter,并在fragment的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();
}
}
private void getProductCatList(){
categoryList=新的ArrayList();
setLayoutManager(新的GridLayoutManager(getActivity(),2));
适配器=新产品类别适配器(getActivity(),categoryList);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(适配器);
progressBar.setVisibility(View.VISIBLE);
Call Call=productClient.getInstance().getApi().getAllCategories();
试一试{
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用,
回应(回应){
if(response.issusccessful()){
categoryList=Objects.requirennull(response.body()).getProductCategories();
adapter=newproductcategoriesadapter(getContext(),categoryList);
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}否则{
progressBar.setVisibility(View.GONE);
StyleableToast.makeText(getContext(),“failed”+response.message(),R.style.StyledToast.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
progressBar.setVisibility(View.GONE);
StyleableToast.makeText(getContext(),t.getLocalizedMessage(),R.style.StyledToast.show();
}
});
}捕获(例外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()
I/okhttp.OkHttpClient:
似乎您为recyclerview设置了一个适配器,但每次都创建一个新的适配器,而您的recyclerview对此一无所知
相反,请尝试将所有新项目添加到类别列表中,然后像这样通知适配器:
categoryList.clear()
categoryList.addAll(myNewItems)
adapter.notifyDataSetChanged();
添加适配器代码和设置位置。
categoryList.clear()
categoryList.addAll(myNewItems)
adapter.notifyDataSetChanged();