我一直在获取java.lang.IllegalArgumentException:废弃或附加的视图可能无法回收。将Fragment与RecyclerView一起使用时,isScrap:false isAttached:true。我只有1个活动,可以在多个片段之间切换。在活动的onCreate上,我设置了默认片段,该片段恰好实现了RecyclerView,与文档中的完全一样。活动启动后,我得到java.lang.IllegalArgumentException:废弃或附加的视图可能无法回收。isScrap:false isAttached:true。
问题是,如果我在开始时加载一个空的片段容器,然后再使用RecyclerView导航到该片段,则可以正常工作。另外我不使用android:animateLayoutChanges或notifyDataSetChanged(),如此处所述。目前,我在Fragment的onResume()方法中设置了RecyclerView。我试过将其切换到其他生命周期方法,但没有运气。
任何帮助表示赞赏。
谢谢
我只添加了相关的代码片段。我认为这与生命周期相关,因为如果不在Activity的onCreate()中设置片段,它就可以工作。当我有一个ListView而不是RecyclerView时,它起作用了。我没有发布RecyclerView的代码,因为它与文档中的代码相同。
活动的onCreate
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"### onCreate ###");
super.onCreate(savedInstanceState);
setContentView(R.layout.efficientix_activity_layout);
if(savedInstanceState != null){
checkedSideMenuItemLabel = savedInstanceState.getInt("checkedSideMenuItemLabel");
}
//Init components
initActionBar(checkedSideMenuItemLabel,savedInstanceState);
initSideMenuArrayAdapter();
initSideMenu(checkedSideMenuItemLabel,savedInstanceState);
initActionBarDrawerToggle();
fragmentManager = this.getSupportFragmentManager();
if(savedInstanceState == null){
//Set default fragment upon activity creation.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
DashboardFragment df = new DashboardFragment();
fragmentTransaction.replace(R.id.fragment_container,df,DashboardFragment.class.toString());
fragmentTransaction.commit();
}
}
片段的onResume()
public void onResume() {
super.onResume();
if (recylerViewLayoutManager == null) {
recylerViewLayoutManager = new LinearLayoutManager(this.getActivity());
}
recylerView.setLayoutManager(remindersLayoutManager);
initRecylerViewAdapter();
recylerView.setAdapter(recylerViewAdapter);
}
片段布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="@+id/myRecyclerView" style="@style/Main_Content_List_View"/>
</LinearLayout>
活动的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:efficientix="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The actionbar toolbar -->
<include layout="@layout/actionbar_toolbar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/fragment_container" android:orientation="vertical">
</LinearLayout>
<include layout="@layout/side_menu_layout"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
RecyclerView项目布局
<?xml version="1.0" encoding="utf-8"?>
<android.widget.TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Main_Content_Table_Layout_List_Item">
<TableRow style="@style/Main_Content_Table_Row_List_Item">
<TextView android:id="@+id/description"
style="@style/Main_Content_TextView" />
<ImageView android:id="@+id/category"
style="@style/Main_Content_ImageView"/>
</TableRow>
<TableRow style="@style/Main_Content_Table_Row_List_Item">
<TextView android:id="@+id/alert_date"
style="@style/Main_Content_TextView" />
<TextView android:id="@+id/type"
style="@style/Main_Content_TextView" />
</TableRow>
</android.widget.TableLayout>
好的,所以我找到了问题所在。在我的ViewHolder中,除了布局视图之外,我还有一个ORMlite实体(可以是不属于布局的任何对象)。问题在于ViewHolder的equals()和hashcode()方法是基于为null的实体的。之所以为null的原因是,在RecyclerView中,您无法访问onCreateViewHolder()中的数据位置,而只能访问onBindViewHolder()中的数据位置。在我的情况下,适配器数据是ORMlite实体的列表,我想将实体捆绑在持有人内部。(仍然必须找到一种方法来做到这一点...)
我期望在这种情况下出现NullPointerException。我设法通过调用holder.setIsRecyclable(true)来获取NullPointerException。
希望这对有需要的人有所帮助。
谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句