我使用setOnItemSwipeListener做滑动删除事,删除最后一条数据时报错 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{e2e7c33 position=0 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent} onItemSwiped确实是执行了,因为我的数据删除了。 于是我加上了setEmptyView 报了错 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mylift.shop/com.mylift.shop.ui.main.fragmment.mine.activity.browsing_history.BrowsingHistoryActivity}: java.lang.RuntimeException: please bind recyclerView first! 我的mRecyclerView.setAdapter(adapter);在setEmptyView 之前已经调用了。 暂时解决:然后我揣测是setEmptyView 调用早了造成的,于是我在设置上拉加载下拉刷新已经侧滑删除这些监听器之后再调用setEmptyView ,问题暂时是解决了。
[CymChad/BaseRecyclerViewAdapterHelper]setEmptyView的问题
回答
出错的时候调用setEmptyView的顺序是?
adapter = new BrowsingHistoryAdapter(list);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//================================================================ // 没有数据的时候默认显示该布局 adapter.setEmptyView(R.layout.item_empty); //================================================================ adapter.setOnItemClickListener。。。 adapter.setOnLoadMoreListener。。。 ItemDragAndSwipeCallback itemDragAndSwipeCallback = new 。。。 ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback); itemTouchHelper.attachToRecyclerView(mRecyclerView);
// 开启滑动删除
adapter.enableSwipeItem();
adapter.setOnItemSwipeListener。。。
放在最后没有出错了
你用removeData方法删除的吗?用这个方法不会报错,setEmptyView放的位置应该也不会报错
开启了滑动删除后,删除最后一条数据时报的错
试着新写了一个例子,删除没有看到错误,不过setEmptyView报错了 Process: com.example.xujiafeng.test, PID: 26412 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xujiafeng.test/com.example.xujiafeng.test.MainActivity}: java.lang.RuntimeException: please bind recyclerView first! at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.RuntimeException: please bind recyclerView first! at com.chad.library.adapter.base.BaseQuickAdapter.checkNotNull(BaseQuickAdapter.java:148) at com.chad.library.adapter.base.BaseQuickAdapter.setEmptyView(BaseQuickAdapter.java:1317) at com.example.xujiafeng.test.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
activity package com.example.xujiafeng.test;
import android.graphics.Canvas; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback; import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
ArrayList<Bean> data = new ArrayList<>();
data.add(new Bean("item"));
data.add(new Bean("item"));
MyAdapter adapter = new MyAdapter(data);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter.setEmptyView(R.layout.empty);
ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(adapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
itemTouchHelper.attachToRecyclerView(mRecyclerView);
// 开启滑动删除
adapter.enableSwipeItem();
adapter.setOnItemSwipeListener(new OnItemSwipeListener()
{
@Override
public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int i)
{
}
@Override
public void clearView(RecyclerView.ViewHolder viewHolder, int i)
{
}
@Override
public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int i)
{
}
@Override
public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float v, float v1, boolean b)
{
}
});
// adapter.setEmptyView(R.layout.empty); } } empty <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="没有数据"/>
item <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
</FrameLayout>
</android.support.v7.widget.CardView> bean package com.example.xujiafeng.test;
/**
- 作者:xuyu on 2017/8/18 10:49
- 邮箱:xujiafeng_31415926@163.com
-
class描述 */ public class Bean { private String meaasge;
public Bean(String meaasge) { this.meaasge = meaasge; }
public String getMeaasge() {
return meaasge;
}
public void setMeaasge(String meaasge) { this.meaasge = meaasge; } } adapter package com.example.xujiafeng.test;
import android.support.annotation.LayoutRes; import android.support.annotation.Nullable;
import com.chad.library.adapter.base.BaseItemDraggableAdapter; import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
/**
- 作者:xuyu on 2017/8/18 10:49
- 邮箱:xujiafeng_31415926@163.com
-
class描述 */ public class MyAdapter extends BaseItemDraggableAdapter<Bean, BaseViewHolder> { public MyAdapter(@Nullable List
data) { super(R.layout.item, data); } @Override protected void convert(BaseViewHolder helper, Bean item) { helper.setText(R.id.text,item.getMeaasge()); } } activity_main <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.xujiafeng.test.MainActivity">
<android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="0dp" android:layout_height="0dp" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout> build.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.android.support:design:26.+' compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22' compile 'com.android.support:cardview-v7:26.+' } 运行的手机是Android7.1.1模拟器 如果需要可以把工程发给你们
你传个demo到github上好了
@xingxingxiaoyu 无法复现错误
这就很尴尬了。。。
要不你在试下?
我项目上的把setEmpty放在后面点调用没有问题,暂时不影响使用~
同样的用法报同样的错误$.$
java.lang.RuntimeException: please bind recyclerView first!
如果是报这个,解决方案,就是你需要在初始化的时候,将 RecyclerView 传递给适配器里面进行绑定。我的处理方式是 mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() {
}
},mRv);
mRv 就是我传进去的 RecyclerView。
You can try :,
bindToRecyclerView(RecyclerView);
mQuickAdapter.setEmptyView(layoutResId);
Hope it can help U ...