I try to use RecyclerView with RecyclerView.Adapter but here is something wrong. I post my code below:
Layout:
For anyone trying to use a ViewPager for tabs, and inside their tabs have a RecyclerView which causes this crazy error, I suggest to postpone inflating by creating yet another Fragment for including only the RecylerView.
This xml file is one of my tabs which contain a frame layout instead a recycler .
shop_most_views_fragment.xml
its java class
public class MostViews extends Fragment {
private String title;
private int page;
View convertedView;
RecyclerView grids;
public static MostViews newInstance(int page, String title) {
MostViews fragmentFirst = new MostViews();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
convertedView = inflater.inflate(R.layout.shop_most_views_fragment, container, false);
transitMasonaryLayoutFragmentIntoFramLayout();
return convertedView;
}
private void transitMasonaryLayoutFragmentIntoFramLayout() {
MasanoryLayout masanoryLayout = new MasanoryLayout();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.most_view_item_frameLayout, masanoryLayout).commit();
}
}
so we have a frame layout to put our recylcer fragment into it.
shop_most_view_masanory_fragment.xml
its java class
public class MasanoryLayout extends Fragment {
View convertedView;
RecyclerView grids;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
convertedView = inflater.inflate(R.layout.shop_most_view_masanory_fragment, container, false);
grids = (RecyclerView) convertedView.findViewById(R.id.mostViewsItem);
setLayoutManagerOnRecyclerView();
MasonryAdapter adapter = new MasonryAdapter(initializeData());
grids.setAdapter(adapter);
return convertedView;
}
private void setLayoutManagerOnRecyclerView() {
grids.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
}
private List initializeData() {
List storeItems = new ArrayList<>();
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.clothstore));
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.clothstore));
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
storeItems.add(new StoreItem("احسان", "احسان", R.drawable.clothstore));
return storeItems;
}
}
as you see your FragmentPagerAdapter would call MostViews to be inflated and MostViews itself would call MasanoryLayout to be inflated. so we escaped FragmentPagerAdapter context ;)