编辑

主要问题在于我使用事件总线打开的片段。我在那边介绍了导致问题的延迟。无论如何,谢谢您的帮助。

主要职位

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

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应该初始化视图,找到视图并添加侦听器
onBind应该更新ViewHolder数据,以便在启动时这些侦听器具有正确的数据


您想确保onBind中的代码尽可能少,因为它被调用很多,尤其是在滚动过程中。

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

使用多种视图类型,因此您也不需要处理动态更改。

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

10-09 12:28