修改
主要问题出在我用eventbus打开的片段中。我介绍了导致这个问题的延迟。无论如何,谢谢你的帮助。
主要帖子
又一个跳帧问题。所以这是我的代码
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();
}
答案 0 :(得分:3)
根本不要在onBind中分配监听器。我想你误解了recycleview的观点。这就是你应该这样做的方式:
你想确保onBind中的代码尽可能少,因为它会被调用很多,特别是在滚动期间。
例如,不要在那里进行任何类型的代码初始化(即Picasso),在适配器构造函数中执行一次。
使用多种视图类型,因此您无需处理动态更改。
答案 1 :(得分:0)
您正在犯的一个明显错误是在onBindViewHolder()
回调中实例化到许多OnClickListeners(或一般对象)。
您可以通过每个按钮类型仅实例化一个侦听器来轻松解决此问题,例如:
将其放在onBindViewHolder()
之外(例如Adapter&#39; s构造函数):
View.OnClickListener productListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Product product = adapter.items.get((int) view.getTag)); // get item for position
EventBus.getDefault().post(product);
}
}
然后,在onBindViewHolder()
:
...
viewHolder.clickView.setOnClickListener(productListener);
viewHolder.setTag(position);
通过重用OnClickListeners
,您可以节省大量的CPU时间和内存。实例化并不昂贵,但是你释放了大量内存并使Garbage Collector
不断地释放不再使用的资源。
@abbath在评论中所说的也可能是一个问题。