Recyclerview页眉和页脚的IndexOutOfBoundsException

时间:2017-10-25 12:57:52

标签: android android-recyclerview

我尝试按照惯例实现RecyclerView页眉和页脚,但是获得了无效的索引错误。我似乎在解决问题。当我删除页眉和页脚时,Recyclerview显示正常。我添加+2到arraylist大小但它没有帮助。如果持有者instanceOf ItemViewHolder,我在onBindViewHolder()中获得此异常。 Glide没有问题。以下是我的适配器代码:

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

private ArrayList<Country> mArrayList;
private Context mContext;

public RecyclerViewAdapterCountry(ArrayList<Country> mArrayList, Context mContext) {
    this.mArrayList = mArrayList;
    this.mContext = mContext;
}

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

    if (viewType == TYPE_ITEM) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_list, parent, false);
        return new ItemViewHolder(view);
    }
    if (viewType == TYPE_HEADER) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_header, parent, false);
        return new HeaderViewHolder(view);


    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_footer, parent, false);
        return new FooterViewHolder(itemView);
    } else return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof HeaderViewHolder) {
        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
        headerHolder.headerFlag.setText("Flag");
        headerHolder.headerCountry.setText("Country");
        headerHolder.headerPopulation.setText("Population");
        headerHolder.headerCountry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "You clicked at Header View!", Toast.LENGTH_SHORT).show();
            }
        });
    } else if (holder instanceof FooterViewHolder) {
        ((FooterViewHolder) holder).footerFlag.setText("Footer");
    } else if (holder instanceof ItemViewHolder) {
        ItemViewHolder itemViewHolder=(ItemViewHolder)holder;
        Glide.with(mContext).load(mArrayList.get(position).getImagePath()).into(itemViewHolder.imageViewCountryFlag);
        itemViewHolder.textViewCountryName.setText(mArrayList.get(position).getCountry());
        itemViewHolder.textViewcountryPopulation.setText(mArrayList.get(position).getPopulation());
    }
}

@Override
public int getItemCount() {
    return mArrayList.size()+2;
}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return TYPE_HEADER;

    } else if (position == mArrayList.size() + 1) {

        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private class HeaderViewHolder extends RecyclerView.ViewHolder {
    TextView headerFlag, headerCountry, headerPopulation;

    public HeaderViewHolder(View view) {
        super(view);
        headerFlag = (TextView) view.findViewById(R.id.textViewHeaderFlag);
        headerCountry = (TextView) view.findViewById(R.id.textViewHeaderCountryName);
        headerPopulation = (TextView) view.findViewById(R.id.textViewHeaderCountryPopulation);
    }
}

private class FooterViewHolder extends RecyclerView.ViewHolder {
    TextView footerFlag, footerCountry, footerPopulation;

        public FooterViewHolder(View view) {
            super(view);
            footerFlag = (TextView) view.findViewById(R.id.textViewFooterFlag);
            footerCountry = (TextView) view.findViewById(R.id.textViewFooterCountryName);
            footerPopulation = (TextView) view.findViewById(R.id.textViewFooterCountryPopulation);
    }
}

private class ItemViewHolder extends RecyclerView.ViewHolder {
    ImageView imageViewCountryFlag;
        TextView textViewCountryName, textViewcountryPopulation;

        public ItemViewHolder(View itemView) {
            super(itemView);
            imageViewCountryFlag = (ImageView) itemView.findViewById(R.id.imageViewFlag);
            textViewCountryName = (TextView) itemView.findViewById(R.id.textViewListContryName);
            textViewcountryPopulation = (TextView) itemView.findViewById(R.id.textViewListCountryPopulation);
        }
    }
}

以下是我进入监视器的例外情况:

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at com.example.user.recyclerviewheaderfooter.RecyclerViewAdapterCountry.onBindViewHolder(RecyclerViewAdapterCountry.java:69)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
    at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at 

2 个答案:

答案 0 :(得分:1)

尝试以下代码

// Data was received from the accessory, real apps should do something with this data but currently:
//    1. bytes counter is incremented
//    2. bytes are read from the session controller and thrown away

    - (void)_sessionDataReceived:(NSNotification *)notification
    {
        EADSessionController *sessionController = (EADSessionController *)[notification object];
        uint32_t bytesAvailable = 0;

        while ((bytesAvailable = (uint32_t)[sessionController readBytesAvailable]) > 0) {
            NSData *data = [sessionController readData:bytesAvailable];
            if (data) {

                _totalBytesRead = _totalBytesRead + bytesAvailable;
            }
        }

        [_receivedBytesCountLabel setText:[NSString stringWithFormat:@"Bytes Received from Session: %u", (unsigned int)_totalBytesRead]];
    }

答案 1 :(得分:0)

您的数据数组包含0..n项。 您已将长度定义为n + 2,项目视图将始终偏移1。

但是,在您的绑定方法中,您正在访问.get(position)而不是.get(position - 1),以便考虑使用position = 0的标头。这意味着它将在最后被一个关闭并导致AIOOB例外。