RxAndroid 网络调用使应用程序滞后于后退导航
Posted
技术标签:
【中文标题】RxAndroid 网络调用使应用程序滞后于后退导航【英文标题】:RxAndroid network calls makes the app lagging on back navigation 【发布时间】:2020-05-31 09:52:23 【问题描述】:在我实现网络调用后,我的 android 应用程序在后退导航上一直滞后。对于网络调用,我使用的是 rxandroid/retrofit。我尝试使用单一和可观察来修复它。两者都使应用程序以相同的方式滞后。这是我使用 observable 时的代码。将数据加载到回收站视图时会发生滞后。所以我也添加了适配器类。
@Override
public void onResume()
super.onResume();
getMenuByShopAndCategoryId(categoryRequest.getId(), Utility.getShop(getActivity()));
private void getMenuByShopAndCategoryId(int categoryId, int shopId)
Repository.getInstance()
.getMenuByShopAndCategoryId(categoryId,shopId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<MenuResponse>>()
@Override
public void onSubscribe(Disposable d)
disposable.add(d);
@Override
public void onNext(Response<MenuResponse> menuResponse)
//other calculations
@Override
public void onError(Throwable e)
//error handling
@Override
public void onComplete()
);
@Override
public void onDestroy()
disposable.dispose();
super.onDestroy();
public Observable<Response<MenuResponse>> getMenuByShopAndCategoryId(@NonNull int category_id, @NonNull int shop_id)
return apiService.getMenuByShopAndCategoryId(category_id,shop_id);
public class MenuItemsAdapter extends RecyclerView.Adapter<MenuItemsAdapter.ViewHolder>
private Context context;
private ArrayList<MenuResponse.MenuRequest> menuItemArrayList;
private ListRowMenuItemsBinding binding;
private MenuItemsAdapterHandler menuItemsAdapterHandler;
public MenuItemsAdapter(Context context, ArrayList<MenuResponse.MenuRequest> menuItemArrayList)
this.context = context;
this.menuItemArrayList = menuItemArrayList;
@NonNull
@Override
public MenuItemsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.list_row_menu_items, viewGroup, false);
menuItemsAdapterHandler = new MenuItemsAdapterHandler(context);
binding.setHandler(menuItemsAdapterHandler);
configureLabels();
return new ViewHolder(binding);
@Override
public void onBindViewHolder(@NonNull MenuItemsAdapter.ViewHolder viewHolder, int i)
viewHolder.binding.lblItemName.setText(menuItemArrayList.get(i).getMenuName());
viewHolder.binding.lblPrice.setText("MVR " + String.format("%.2f", Double.valueOf(menuItemArrayList.get(i).getSubTotal())));
if (menuItemArrayList.get(i).getAvailability().equals(Constants.AVAILABLE))
binding.lblAvailability.set(context, HuqTypogrphyStyle.CAPS_BUTTON_GREEN);
viewHolder.binding.lblAvailability.setText("AVAILABLE");
else
binding.lblAvailability.set(context, HuqTypogrphyStyle.CAPS_BUTTON_RED);
viewHolder.binding.lblAvailability.setText("NOT AVAILABLE");
viewHolder.binding.setMenuItem(menuItemArrayList.get(i));
viewHolder.binding.executePendingBindings();
@Override
public int getItemCount()
return menuItemArrayList.size();
@Override
public int getItemViewType(int position)
return position;
public class ViewHolder extends RecyclerView.ViewHolder
ListRowMenuItemsBinding binding;
public ViewHolder(@NonNull ListRowMenuItemsBinding listRowMenuItemsBinding)
super(listRowMenuItemsBinding.getRoot());
this.binding = listRowMenuItemsBinding;
private void configureLabels()
binding.lblItemName.set(context, HuqTypogrphyStyle.H2_HEADING);
binding.lblPrice.set(context, HuqTypogrphyStyle.BODY_GRAY);
binding.lblAvailability.set(context, HuqTypogrphyStyle.BODY_GRAY);
【问题讨论】:
【参考方案1】:尝试将 api 调用从 on Resume 移至 onCreate。
【讨论】:
试过了!没用。我删除了我的片段过渡并减少了一点滞后。但我认为这不是解决方案以上是关于RxAndroid 网络调用使应用程序滞后于后退导航的主要内容,如果未能解决你的问题,请参考以下文章