Android: Programatically adding multiple RecyclerViews in a fragment

Viewed 9261

I need to add multiple RecyclerViews programatically to a fragment. I have managed to include one RecyclerView using xml layout (below) and it works fine, however, when I try to add any at all programatically, not even one appears in the fragment view even though the returned RecyclerViews are not null. Because my datasource is web API driven, I cannot add a specific number of RecyclerViews in an xml layout as the number required will change from time to time, therefore it must be done programatically. I have tried a number of different methods but all results are the same, eg: not one RecyclerView. I also need to add TextViews above each RecyclerView as headers, which I have done already and they work perfectly, but are removed from the code below to make it easier to digest. All I need to be able to do to finish my project is add the multiple RecyclerViews. I hope someone can help?

Fragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
    svv = new ScrollView(getActivity());
    svv.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT));
    linLayout = new LinearLayout(getActivity());
    linLayout.setOrientation(LinearLayout.VERTICAL);
    linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

    View rootView = inflater.inflate(R.layout.fragment_root, container, false);
    HorizontalScrollView svh;
    RecyclerView itemsListing;

    int top = 100;
    for(int i = 0; i < itemCount; i++) {
    String strSubURL = myListItem.myListUrls.get(i).toString();
    sharedData.setCurrMyURL(String.valueOf(strSubURL));

    // Below is where the problems start

    // This works fine but only provides one recyclerview
    //itemsListing = (RecyclerView) rootView.findViewById(R.id.items_listing);

    // This does not work at all, showing zero recyclerviews even though the views are not null and are therefore actually created
    itemsListing = new RecyclerView(inflater.getContext());

    itemsListing.setPadding(0, top, 0, 0);
    mLayoutManager = new LinearLayoutManager(itemsListing.getContext(), LinearLayoutManager.HORIZONTAL, false);
    itemsListing.setLayoutManager(mLayoutManager);
    mAdapter = new ItemsListingAdapter(mItems, this);
    itemsListing.setAdapter(mAdapter);
    svh = new HorizontalScrollView(getActivity());
    svh.setPadding(0, top, 0, 0);
    top=top+400;
    }
    svv.addView(linLayout);
    RelativeLayout mainLayout = (RelativeLayout) rootView.findViewById(R.id.item_layout);
    mainLayout.addView(svv);
    return rootView;
    }

XML Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#000000"
    tools:context="com.myco.myapp.items.listing.ItemsListingListingFragment">
    <android.support.v7.widget.RecyclerView
    android:id="@+id/items_listing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
    </RelativeLayout>
2 Answers
Related