gpt4 book ai didi

android - 跳过 320 帧在主线程上做的太多

转载 作者:行者123 更新时间:2023-11-29 17:15:21 28 4
gpt4 key购买 nike

编辑

主要问题出在我用事件总线打开的 fragment 中。我在那里引入了延迟,导致了这个问题。无论如何感谢您的帮助。

主帖

又一个跳帧问题。所以这是我的代码

public class ProductDisplayAdapter extends RecyclerView.Adapter<ProductDisplayAdapter.ViewHolder> {

private Context context;
private List<Item> productList;
private Table<String,Integer,Item> cartTable = HashBasedTable.create();
private OnItemChanged itemChangedListener;
private ViewHolder viewHolder;
private Picasso picasso;
public ProductDisplayAdapter(Context mContext, List<Item> items)
{
this.context = mContext;
this.productList = items;
this.picasso = Picasso.with(context);
this.picasso.setIndicatorsEnabled(true);
}

private Item getItem(int position) {
return new Item(productList.get(position));
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.list_product,parent,false);
this.viewHolder = new ViewHolder(view);
viewHolder.cartAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewHolder.numberButton.setNumber("1");
viewHolder.viewSwitcher.showNext();
if(itemChangedListener != null)
{
int position = viewHolder.getAdapterPosition();
Item product = productList.get(position);
product.setTotalQuantity(1);
notifyItemChanged(viewHolder.getAdapterPosition());
productList.set(position, product);
cartTable.put(product.getId(), product.getMetricPosition(), product);
itemChangedListener.onItemAdded(cartTable, 1);
}
}
});
viewHolder.numberButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
if(itemChangedListener != null)
{
int position = viewHolder.getAdapterPosition();
Item product = productList.get(position);
product.setTotalQuantity(newValue);
notifyItemChanged(viewHolder.getAdapterPosition());
productList.set(position, product);
cartTable.put(product.getId(), product.getMetricPosition(), product);
itemChangedListener.onItemAdded(cartTable, newValue-oldValue);

}
}
});
viewHolder.clickView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(productList.get(viewHolder.getAdapterPosition()));
}
});
return this.viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder mViewHolder, int position) {
Item product = getItem(position);
picasso.load(product.getImageUrl()).fit().into(viewHolder.productImage);
String price = CurrencyUtils.getCurrencySymbol(product.getCurrency()) + product.getPrice().get(product.getMetricPosition());
viewHolder.productPrice.setText(price);
viewHolder.productName.setText(product.getName());
viewHolder.productQuantity.setText(product.getQuantity().get(product.getMetricPosition()));
viewHolder.productQuantity.setTypeface(FontUtils.getRegularTypeFace());
viewHolder.productQuantity.setOnClickListener(dialogClickListener);
if(product.getTotalQuantity() > 0)
{
viewHolder.viewSwitcher.setDisplayedChild(1);
viewHolder.numberButton.setNumber(String.valueOf(product.getTotalQuantity()),false);
}
else if(product.getStock() > 0)
{
viewHolder.viewSwitcher.setDisplayedChild(0);
viewHolder.cartAdd.setBackgroundColor(Color.WHITE);
viewHolder.cartAdd.setTag(false);
viewHolder.cartAdd.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_cart_add));
}
else {
viewHolder.viewSwitcher.setDisplayedChild(0);
viewHolder.cartAdd.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_cart_soldout));
}

removeButtonShadow(viewHolder.productQuantity);
}

@Override
public long getItemId(int position) {
return position;
}

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

public static class ViewHolder extends RecyclerView.ViewHolder{

ImageView productImage,cartAdd;
TextView productName;
TextView productPrice;
Button productQuantity;
ElegantNumberButton numberButton;
ViewSwitcher viewSwitcher;
View clickView;
public ViewHolder(View itemView) {
super(itemView);
this.clickView = itemView;
productImage = (ImageView) itemView.findViewById(R.id.product_image);
productName = (TextView) itemView.findViewById(R.id.product_name);
productPrice = (TextView) itemView.findViewById(R.id.product_price);
productQuantity = (Button) itemView.findViewById(R.id.product_quantity);
cartAdd = (ImageView) itemView.findViewById(R.id.cart_add);
numberButton = (ElegantNumberButton) itemView.findViewById(R.id.number_button);
viewSwitcher = (ViewSwitcher) itemView.findViewById(R.id.view_switcher);
}

}

private View.OnClickListener dialogClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction fragmentTransaction = ((Activity) context).getFragmentManager().beginTransaction();
Fragment previousDialog = ((Activity) context).getFragmentManager().findFragmentByTag(StringConstants.DIALOG_TAG);
if(previousDialog != null)
{
fragmentTransaction.remove(previousDialog);
}
fragmentTransaction.addToBackStack(null);
QuantityDialogFragment mFragment = QuantityDialogFragment.newInstance(getItem(viewHolder.getAdapterPosition()),getItem(viewHolder.getAdapterPosition()).getMetricPosition());
mFragment.setQuantityListener(new OnQuantityChosen() {
@Override
public void onSelectQuantity(int position) {
Item item = getItem(viewHolder.getAdapterPosition());
item.setMetricPosition(position);
item.setTotalQuantity(0);
productList.set(viewHolder.getAdapterPosition(),item);
notifyItemChanged(viewHolder.getAdapterPosition());
}
});
mFragment.show(fragmentTransaction,StringConstants.DIALOG_TAG);
}
};

private void removeButtonShadow(Button button)
{
if(Build.VERSION.SDK_INT >= 21)
button.setStateListAnimator(null);
}

public void setOnItemChangedListener(OnItemChanged onItemChangedListener)
{
this.itemChangedListener = onItemChangedListener;
}


}

我遇到了跳帧问题,尤其是当我遇到以下几行时,

viewHolder.clickView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(product);
}
});

我不知道问题出在哪里。但是一旦我单击 recyclerview 项目,我就会看到明显的跳帧,编译器会在跟踪中抛出 300 多个跳帧。谁能缩小范围并提出一些更改建议?

编辑

这是我的 Eventbus 订阅者。

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onProductClicked(Item product)
{
Fragment fragment = ProductLandingFragment.newInstance(product);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.hide(fragmentManager.findFragmentByTag(StringConstants.FRAGMENT_CATEGORIES_TAG));
fragmentTransaction.replace(R.id.fragmentholder,fragment,StringConstants.FRAGMENT_PRODUCT_TAG);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

最佳答案

根本不要在 onBind 中分配监听器。我认为你误解了 recycleview 的意义。你应该这样做:

  • onCreateViewHolder 应该初始化 View ,找到 View 并添加监听器
  • onBind 应该更新 ViewHolder 数据,以便这些监听器在触发时拥有正确的数据

您要确保 onBind 中的代码尽可能少,因为它会被频繁调用,尤其是在滚动期间。

例如,不要在那里进行任何类型的代码初始化(即 Picasso),在适配器构造函数中进行一次。

使用多种 View 类型,因此您也不需要处理动态变化。

关于android - 跳过 320 帧在主线程上做的太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com