Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{2a49 #479

Closed
jttttttttttttt opened this issue Nov 7, 2016 · 23 comments

Comments

@jttttttttttttt
Copy link

jttttttttttttt commented Nov 7, 2016

当我界面上第一次获取数据时出错,我是这样写的

View view = getErrorView(rv);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadListData();
            }
        });
        adapter.setEmptyView(view);
        adapter.notifyItemChanged(0);

但我点击view 加载数据 再次获取没有数据并出错的时候,出现了java.lang.IllegalArgumentException: Called attach on a child which is not detached,求助
这是代码里的:我加了removeAllFooterView();

/**
     * Sets the view to show if the adapter is empty
     */
    public void setEmptyView(View emptyView) {
        removeAllFooterView();
        setEmptyView(false, false, emptyView);
    }
@CymChad
Copy link
Owner

CymChad commented Nov 7, 2016

你的版本是?为什么要加removeAllFooterView?

@jttttttttttttt
Copy link
Author

最新的 加removeAllFooterView是出现过没有更多数据和加载错误一起出现了

@CymChad
Copy link
Owner

CymChad commented Nov 7, 2016

你有没有按demo里面的步奏来?demo是正常的。

@jttttttttttttt
Copy link
Author

jttttttttttttt commented Nov 7, 2016

想问下 showLoadMoreFailView 是不是只能再异步之中进行
demo里面如果一开始加载没有setLoadMoreFailView并且没有数据 加载错误 直接显示showLoadMoreFailView 会崩的~~~ 因为context没有~ 我还是想知道LoadMoreFailView的click事件 是设置view的时候直接设置 还是adapter里面再开个接口设置

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

在没有数据的时候不能调用setLoadMoreFailView

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

setLoadMoreFailView的使用场景是在有数据的情况下,加载数据没有网络然后在setLoadMoreFailView添加一个错误提示,点击可以再次加载。

@jttttttttttttt
Copy link
Author

没有数据又加载错误的时候还是setEmptyView吗?

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

恩 是的,setEmptyView是支持设置两个不同view的切换的。具体使用可见demo

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

LoadMoreFailView的click事件 默认已经给你写好了。

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

如果开发者在调用 showLoadMoreFailed() 方法前传入自定义加载更多错误的View的话,就算没有 Context 也不会有问题的。

@jttttttttttttt
Copy link
Author

rxjava+retrofit2.0异步请求失败之后可以这么写吗?
if (remindLst.size() == 0) {
setErrorView(rv, remindAdapter);
} else {
rv.post(new Runnable() {
@OverRide
public void run() {
remindAdapter.showLoadMoreFailedView();
}
});
}

@jttttttttttttt
Copy link
Author

jttttttttttttt commented Nov 8, 2016

还是就是setErrorView(rv, remindAdapter); 但是这是emptyView

@jttttttttttttt
Copy link
Author

showLoadMoreFailed() click事件我看了 但是感觉点击没效果 所以自己添加了view.click事件

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

这不是异步的问题,如果你第一次就没有数据adapter不会进入onCreateViewHolder,Context是在onCreateViewHolder里面赋值的,所以没有Context,然而showLoadMoreFailed需要加载布局所以要用到Context,Context没有就会报错。如果开发者在调用 showLoadMoreFailed() 方法前传入自定义加载更多错误的View的话,就算没有 Context 也不会有问题的。

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

正常的业务逻辑应该是,如果第一次没有数据就改变EmptyView,因为这时候是一整个屏幕的,而如果在有数据的情况下出现网络错误,那么就是调用showLoadMoreFailed显示一个错误的view。

@jttttttttttttt
Copy link
Author

嗯是的,谢谢大腿

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

没有其他问题了吧?

@jttttttttttttt
Copy link
Author

showLoadMoreFailed 我想知道 这个点击的时候 是不是触发onscroll 会再次进入BaseQuickAdapter.RequestLoadMoreListener()。前几天,给搞混了empty和showLoadMoreFailed

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

showLoadMoreFailed是打开了上拉的开关,上拉的触发判断就是如果item现在滑到了最后一条了,并且你的上拉开关没有关闭,就会触发RequestLoadMoreListener

@jttttttttttttt
Copy link
Author

是不是点击了LoadMoreFailed事件 hide了之后 就会触发一次 我现在东西搞乱了~

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

恩,是的。

@jttttttttttttt
Copy link
Author

谢谢 我明白了 我之前是没有数据就showLoadMoreFailed,再这之前我是设置了setLoadMoreFailed,并且自己设置了点击事件 发现点击事件不好用~~ 谢谢大腿 ,没问题了

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2016

恩,好的,感谢你的使用。

@CymChad CymChad closed this as completed Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants