I need to add some RecyclerViews fragments to the fragment. I managed to enable one RecyclerView using the xml layout (see below), and it works fine, however, when I try to add any programmatically, none of them appear in the fragment view, even if the returned RecyclerView are non-zero. Since my data source is controlled by a web interface, I cannot add a certain amount of RecyclerViews to the xml layout, since the required amount of time will change from time to time, so it must be done programmatically. I tried several different methods, but all the results are the same, for example: not one RecyclerView. I also need to add TextViews on each RecyclerView as the headers I already made, and they work fine, but are removed from the code below,to simplify digestion. All I have to do to finish my project is to add some RecyclerViews. 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));
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>
source
share