RecyclerView适配器的getItemViewType方法有时返回2、8等,而不是0或1

时间:2018-07-07 06:02:56

标签: android android-recyclerview recycler-adapter recyclerview-layout

下面是我正在使用的代码:

public class PaymentAndReceiptAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    public Context context;
    private List<CustomerAndAmount> customerAndAmountList = new ArrayList<>();
    private PaymentAndReceiptItemClickListener paymentAndReceiptItemClickListener;
    private Map<Long, Double> chartMap;

    public PaymentAndReceiptAdapter(final Context context, final List<CustomerAndAmount> customerAndAmountList, final Map<Long, Double> chartMap, final PaymentAndReceiptItemClickListener paymentAndReceiptItemClickListener) {
        this.context = context;
        setResult(customerAndAmountList, chartMap);
        this.paymentAndReceiptItemClickListener = paymentAndReceiptItemClickListener;
    }

    public void setResult(final List<CustomerAndAmount> customerAndAmountList, final Map<Long, Double> chartMap) {
        this.customerAndAmountList.clear();
        if (null != customerAndAmountList) {
            this.customerAndAmountList.addAll(customerAndAmountList);
        }
        this.chartMap = chartMap;
        notifyDataSetChanged();
    }

    public List<CustomerAndAmount> getItemResult() {
        return this.customerAndAmountList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        if (i == TYPE_HEADER) {
            final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_barchart, parent, false);
            return new PaymentAndReceiptAdapter.HeaderViewHolder(view);
        } else if (i == TYPE_ITEM) {
            final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_transaction_item, parent, false);
            return new CustomerCustomerAndAmountViewHolder(view, customerAndAmountList, paymentAndReceiptItemClickListener);
        }
        throw new RuntimeException("there is no type that matches the type " + i + " make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PaymentAndReceiptAdapter.HeaderViewHolder) {
            PaymentAndReceiptAdapter.HeaderViewHolder headerHolder = (PaymentAndReceiptAdapter.HeaderViewHolder) holder;
            if (null != chartMap) {
                headerHolder.mChart.setVisibility(View.VISIBLE);
                headerHolder.setData(context, chartMap);
            } else {
                headerHolder.mChart.setVisibility(View.GONE);
            }
        } else if (holder instanceof PaymentAndReceiptAdapter.CustomerCustomerAndAmountViewHolder) {
            PaymentAndReceiptAdapter.CustomerCustomerAndAmountViewHolder customerCustomerAndAmountViewHolder = (PaymentAndReceiptAdapter.CustomerCustomerAndAmountViewHolder) holder;
            final CustomerAndAmount customerAndAmount = customerAndAmountList.get(position - 1);

            customerCustomerAndAmountViewHolder.customerName.setText(customerAndAmount.customerName);
            customerCustomerAndAmountViewHolder.customerAndAmountValue.setText(Utils.formatCommaSeperatedNumber(context, customerAndAmount.value));
            customerCustomerAndAmountViewHolder.date.setVisibility(View.GONE);

            if (position == (customerAndAmountList.size() - 1)) {
                customerCustomerAndAmountViewHolder.separator.setVisibility(View.GONE);
            } else {
                customerCustomerAndAmountViewHolder.separator.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position)) {
            return TYPE_HEADER;
        }
        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    @Override
    public int getItemCount() {
        int sz = 0;
        if (null != customerAndAmountList) {
            sz = customerAndAmountList.size();
        }
        return sz + 1;
    }

    public interface PaymentAndReceiptItemClickListener {
        void onPaymentAndReceiptItemClickListener(final CustomerAndAmount customerAndAmount);
    }

    protected static class CustomerCustomerAndAmountViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.customer_name)
        protected TextView customerName;

        @BindView(R.id.transaction_value)
        protected TextView customerAndAmountValue;

        @BindView(R.id.date)
        protected TextView date;

        @BindView(R.id.separator)
        protected View separator;

        private List<CustomerAndAmount> customerAndAmountList;
        private PaymentAndReceiptItemClickListener PaymentAndReceiptItemClickListener;

        public CustomerCustomerAndAmountViewHolder(final View itemView, final List<CustomerAndAmount> customerAndAmountList, final PaymentAndReceiptItemClickListener PaymentAndReceiptItemClickListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            this.customerAndAmountList = customerAndAmountList;
            this.PaymentAndReceiptItemClickListener = PaymentAndReceiptItemClickListener;
        }

        @Override
        public void onClick(View view) {
            if (Utils.isNotEmpty(customerAndAmountList)) {
                final CustomerAndAmount customerAndAmount = customerAndAmountList.get(getLayoutPosition() - 1);
                PaymentAndReceiptItemClickListener.onPaymentAndReceiptItemClickListener(customerAndAmount);
            }
        }
    }

    public interface BarItemClickListener {
        void onBarItemClick(final SearchRequest searchRequest);
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.chart)
        protected BarChart mChart;

        private PaymentAndReceiptAdapter.BarItemClickListener listener;

        public HeaderViewHolder(final View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(final Context context, final Map<Long, Double> chartMap) {
            mChart.setMaxVisibleValueCount(60);
            mChart.setDrawGridBackground(false);
            mChart.setDragEnabled(true);
            mChart.getDescription().setEnabled(false);
            mChart.setFitBars(true);
            mChart.setPinchZoom(false);
            mChart.setDoubleTapToZoomEnabled(false);

            XAxis xAxis = mChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setDrawGridLines(false);
            xAxis.setGranularity(0.5f);
            xAxis.setGranularityEnabled(true);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    Float dfloat = value;
                    long dateInMillis = dfloat.longValue();
                    return ChartUtils.getMonthYearFromMillis(dateInMillis);
                }
            });

            YAxis leftAxis = mChart.getAxisLeft();
            leftAxis.setValueFormatter(new ChartUtils.YAxisValueRsFormatter(context));
            leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
            leftAxis.setSpaceTop(15f);
            leftAxis.setDrawGridLines(false);

            YAxis rightAxis = mChart.getAxisRight();
            rightAxis.setEnabled(false);

            Legend l = mChart.getLegend();
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
            l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            l.setDrawInside(false);
            l.setForm(Legend.LegendForm.SQUARE);
            l.setFormSize(9f);
            l.setTextSize(11f);
            l.setXEntrySpace(4f);

            final Map<Long, Double> monthMap = new HashMap<>();
            for (Map.Entry<Long, Double> cMap : chartMap.entrySet()) {
                long month = ChartUtils.getFirstDateMonthYear(cMap.getKey());
                Double amount = monthMap.get(month);
                if (null == amount) {
                    amount = cMap.getValue();
                } else {
                    amount += cMap.getValue();
                }
                monthMap.put(month, amount);
            }
            final Map<Long, Double> treeMap = new TreeMap<>(monthMap);

            final ArrayList<BarEntry> yVals1 = new ArrayList<>();
            for (Map.Entry<Long, Double> cMap : treeMap.entrySet()) {
                yVals1.add(new BarEntry(cMap.getKey(), cMap.getValue().floatValue()));
            }

            final BarDataSet set1;

            if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
                set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
                set1.setValues(yVals1);
                mChart.getData().notifyDataChanged();
                mChart.notifyDataSetChanged();
            } else {
                set1 = new BarDataSet(yVals1, "Amounts");
                set1.setValueTextColor(context.getResources().getColor(R.color.dark_gray_primary));
                set1.setColor(context.getResources().getColor(R.color.colorPrimaryLight));
                set1.setHighLightColor(context.getResources().getColor(R.color.colorPrimaryLight));
                set1.setDrawValues(false);

                ArrayList<IBarDataSet> dataSets = new ArrayList<>();
                dataSets.add(set1);

                BarData data = new BarData(dataSets);
                data.setValueTextSize(10f);
                if (monthMap.size() >= 4) {
                    data.setBarWidth(2000000000f);
                } else {
                    data.setBarWidth(2500000000f);
                }

                mChart.setData(data);
            }

            final CustomBarChartMarkerView mv = new CustomBarChartMarkerView(context, R.layout.view_custom_marker);
            mChart.setMarker(mv);
            mChart.setHighlightFullBarEnabled(true);
            if (monthMap.size() > 1) {
                if (monthMap.size() >= 4) {
                    xAxis.setLabelCount(4);
                } else {
                    xAxis.setLabelCount(monthMap.size());
                }
                mChart.setVisibility(View.VISIBLE);
            } else {
                mChart.setVisibility(View.GONE);
            }
        }
    }
}

崩溃日志报告:

Fatal Exception: java.lang.RuntimeException: there is no type that matches the type 7 make sure your using types correctly
       at in.bizanalyst.adapter.PaymentAndReceiptAdapter.onCreateViewHolder(PaymentAndReceiptAdapter.java:79)
       at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
       at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1333)
       at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1077)
       at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1815)
       at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3076)
       at android.view.View.dispatchTouchEvent(View.java:12534)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3153)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2829)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
       at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:600)
       at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
       at android.app.Activity.dispatchTouchEvent(Activity.java:3384)
       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
       at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:562)
       at android.view.View.dispatchPointerEvent(View.java:12782)
       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5662)
       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5457)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4950)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5003)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4969)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5106)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4977)
       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5163)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4950)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5003)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4969)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4977)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4950)
       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7719)
       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7659)
       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7620)
       at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7830)
       at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
       at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(InputEventReceiver.java)
       at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
       at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7793)
       at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7857)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
       at android.view.Choreographer.doCallbacks(Choreographer.java:723)
       at android.view.Choreographer.doFrame(Choreographer.java:652)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6938)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

0 个答案:

没有答案