CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.29k stars 5.15k forks source link

java.lang.RuntimeException: please bind recyclerView first! #1955

Closed JavaBoyHW closed 6 years ago

JavaBoyHW commented 6 years ago

我在BaseQuickAdapter.OnItemClickListener()的点击事件中想通过adapter.getViewByPosition(position, R.id.isselected);拿到一个ImageView。但这行报错。代码如下:

mImageListAdapter = new ImageListAdapter(R.layout.item_image_list, mSelectAlbum);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(mActivity, 3, LinearLayoutManager.VERTICAL, false);
        mRecyImageList.setLayoutManager(layoutManager);
        mRecyImageList.setAdapter(mImageListAdapter);
        mImageListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LogUtils.print(TAG, "onItemClick: " + position);
                ToastUtils.showToast(mActivity, "点击了" + position);

                ImageItem imageItem = (ImageItem) adapter.getItem(position);
                ImageView selected = (ImageView) adapter.getViewByPosition(position, R.id.isselected);
                if (imageItem.isSelected == false) {
                    LogUtils.print(TAG, "选中了");
                    boolean addState = selectImage2List(imageItem);
                    if (addState) {
//                        selected.setVisibility(View.VISIBLE);
                        imageItem.isSelected = true;
                    }
                } else {
                    LogUtils.print(TAG, "取消选中");
                    boolean addState = removeImage4List(imageItem);
                    if (addState) {
//                        selected.setVisibility(View.INVISIBLE);
                        imageItem.isSelected = false;
                    }
                }
            }
        }); 
AllenCoder commented 6 years ago

先调用

 /**
     * same as recyclerView.setAdapter(), and save the instance of recyclerView
     */
    public void bindToRecyclerView(RecyclerView recyclerView) {
        if (getRecyclerView() != null) {
            throw new RuntimeException("Don't bind twice");
        }
        setRecyclerView(recyclerView);
        getRecyclerView().setAdapter(this);
    }

再调用这个

 /**
     * get the specific view by position,e.g. getViewByPosition(2, R.id.textView)
     * <p>
     * bind recyclerView {@link #bindToRecyclerView(RecyclerView)} before use!
     *
     * @see #bindToRecyclerView(RecyclerView)
     */
    @Nullable
    public View getViewByPosition(int position, @IdRes int viewId) {
        checkNotNull();
        return getViewByPosition(getRecyclerView(), position, viewId);
    }

抛错代码

    private void checkNotNull() {
        if (getRecyclerView() == null) {
            throw new RuntimeException("please bind recyclerView first!");
        }
    }

建议 通过 LinearLayoutManager.findViewByPosition(position )获取位置的view

iman2420 commented 6 years ago

@AllenCoder

setRecyclerView(android.support.v7.widget.RecyclerView)' has private access in 'com.chad.library.adapter.base.BaseQuickAdapter

How to solve this?

Bertkiing commented 6 years ago

You can try :, bindToRecyclerView(RecyclerView); mQuickAdapter.setEmptyView(layoutResId); Hope it can help U ...