I'm having this annoying issue, even tho my previous apps didn't experience such bug,
I have the RecyclerView
inside a NestedScrollView
, and the params are set correctly. Looking at the code, i have the setAdapter
method after the setLayoutManager
As you can see in the code, I'm adding an empty ArrayList
, and setting the LayoutManager
first, after that i have setNestedScrollingEnabled
to false
, finally I'm setting the Adapter
.
In the XML, I have it as wrap_content
.
Still, i'm experiencing this error, and my app just FC afterwards.
Code:
if(mRecyclerView == null) mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_section_home_recyclerview); mGiftsItemsList = new ArrayList<>(); LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); mLinearLayoutManager.setAutoMeasureEnabled(true); mRecyclerView.setLayoutManager(mLinearLayoutManager); mAdapter = new AdapterFragmentSectionHome(getActivity(),mGiftsItemsList); mRecyclerView.setNestedScrollingEnabled(false); setOnItemClickListener(this); mRecyclerView.setAdapter(mAdapter); mAdapter.setOnItemClickListener(new ListenerInterfaceHomeClick() { @Override public void onItemClick(int position, View v) { if(mGiftsItemsList.get(position).isGiveaway_has_finished()){ } else { if(getActivity() != null){ Intent intent = new Intent(v.getContext(), ActivityGy.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { intent.putExtra(Constants.GIVEAWAY_IS_ANIMATED,true); ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),Pair.create(v, "cardview") ); try{ startActivity(intent,options.toBundle()); } catch (Exception e){ e.printStackTrace(); } } else { try{ startActivity(intent); } catch (Exception e){ e.printStackTrace(); } } } } } }); RecyclerView.ItemAnimator animator = mRecyclerView.getItemAnimator(); if (animator instanceof SimpleItemAnimator) { ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false); }
Layout:
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_section_home_nestedscrollview" android:fillViewport="true" android:scrollbars="none"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/fragment_section_home_nestedscrollview_layout"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fragment_section_home_recyclerview" android:scrollbars="none"/> </LinearLayout> </android.support.v4.widget.NestedScrollView>
2 Answers
Answers 1
In a project of mine, I also encounter this but no crash.
RecyclerView: No adapter attached; skipping layout
Here is how I defined my elements (focused on the recycler, that is displaying a grid):
layout.xml:
<android.support.v4.widget.NestedScrollView 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" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- stuff here --> <android.support.v7.widget.RecyclerView android:id="@+id/myRecycler" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> </RelativeLayout> </android.support.v4.widget.NestedScrollView>
Activity.java:
@VisibleForTesting RecyclerView myRecycler; private MyAdapter myRecyclerAdapter; @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); setLayout(); } private void setLayout() { myRecycler = findViewById(R.id.myRecycler); ViewCompat.setNestedScrollingEnabled(myRecycler, false); } @Override protected void onResume() { super.onResume(); setLayoutValues(); } private void setLayoutValues() { myRecyclerAdapter = new MyAdapter(); int nbCols = 2; GridLayoutManager layout = new GridLayoutManager(this, nbCols); myRecycler.setLayoutManager(layout); myRecycler.setAdapter(myRecyclerAdapter); }
I hope you could inspire from it and solve your issue :)
Answers 2
Try this: You have to add Layout Manager to your RecyclerView From xml
<android.support.v7.widget.RecyclerView xmlns:app="http://schemas.android.com/apk/res-auto" app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
and from code after recycler view initialization
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); mRecyclerView.setLayoutManager(mLayoutManager);
0 comments:
Post a Comment