Issue
Hi I have a tab layout which inflates a bunch of fragments through a viewpager each fragment instantiates a bunch of views everything works fine but there is one problem if i swipe through a few fragments and then swipe back the fragments go through there lifecycle of being detached and attached and setting up its views resulting in the fragment doubling the views in its layout,
eg: fragment shows 5 views, swipe through some fragments and swipe back same fragment then shows 10 views
i believe its because im setting everything up in onCreateView ive tried to move the setup calls to onViewCreated which gives me a different issue, the fragment and views are created fine but when you swipe away and then back the views are gone and the console tells me 'no adapter attached skipping layout', researching this gave me the impression that I needed to move my adapter calls to the main thread but I couldnt get this to work null pointers whatever i try can someone point me in the right direction,
here is the main part of my fragment
public class OneFragment extends Fragment{
public static List<CardWriter> cardMakerList = new ArrayList<>();
private RecyclerView recyclerView;
private GridLayoutManager staggeredGridLayoutManagerVertical;
public static CardAdapter cardAdapter;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate
(R.layout.fragment_one, container, false);
RecyclerView.LayoutManager staggeredGridLayoutManagerVertical = new
StaggeredGridLayoutManager(4,LinearLayoutManager.VERTICAL);
recyclerView = (RecyclerView) rootView.findViewById(R.id.card_grid);
cardAdapter = new CardAdapter(cardMakerList);
recyclerView.setLayoutManager(staggeredGridLayoutManagerVertical);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(cardAdapter);
recyclerView.addOnItemTouchListener(new
RecyclerTouchListener(getActivity().getApplicationContext(),
recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
CardWriter cardWriter = cardMakerList.get(position);
SpeakGrid.cardMakerList.add(SpeakGrid.cardMakerList.size(),cardWriter);
SpeakGrid.cardAdapter.notifyItemInserted(SpeakGrid.cardMakerList.size());
SpeakRecyclerGrid.cardMakerList.add
(SpeakRecyclerGrid.cardMakerList.size(),cardWriter);
SpeakRecyclerGrid.cardAdapter.notifyItemInserted
(SpeakGrid.cardMakerList.size());
Snackbar.make(view, "This card is " +
cardWriter.getCardEmotion() + " and it would say " +
cardWriter.getCardEmotion(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
ive been going through and debugging this and it was also happening while rotating so im now using if (savedinstance state == null) to solve the issue with rotating but it must be lost when scrolling because it still happens if i scroll more than one away and come back the cards in the view have doubled
Solution
Init your views in onCreateView then edit them in onActivityCreated. Calling getActivity() in onCreateView can cause null pointer exceptions
/**
* Called to have the fragment instantiate its user interface view, can be
* null for non-graphical fragments Called after onCreate(Bundle) and
* beforeonActivityCreated(Bundle).
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
RecyclerView.LayoutManager staggeredGridLayoutManagerVertical = new
StaggeredGridLayoutManager(4,LinearLayoutManager.VERTICAL);
recyclerView = (RecyclerView) rootView.findViewById(R.id.card_grid);
return rootView;
}
/**
* Called when the fragment's activity has been created and this fragment's
* view hierarchy instantiated. Called after onCreateView(Bundle) and before
* onViewStateRestored(Bundle).
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cardAdapter = new CardAdapter(cardMakerList);
recyclerView.setLayoutManager(staggeredGridLayoutManagerVertical);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(cardAdapter);
recyclerView.addOnItemTouchListener(new
RecyclerTouchListener(getActivity().getApplicationContext(),
recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
CardWriter cardWriter = cardMakerList.get(position);
SpeakGrid.cardMakerList.add(SpeakGrid.cardMakerList.size(),cardWriter);
SpeakGrid.cardAdapter.notifyItemInserted(SpeakGrid.cardMakerList.size());
SpeakRecyclerGrid.cardMakerList.add
(SpeakRecyclerGrid.cardMakerList.size(),cardWriter);
SpeakRecyclerGrid.cardAdapter.notifyItemInserted
(SpeakGrid.cardMakerList.size());
Snackbar.make(view, "This card is " +
cardWriter.getCardEmotion() + " and it would say " +
cardWriter.getCardEmotion(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
Answered By - Tara Fennell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.