Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Programatically adding multiple RecyclerViews in a fragment

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>
like image 395
partynose Avatar asked Mar 24 '16 16:03

partynose


2 Answers

You don't need multiple RecyclerViews, you can achieve it with one. With the library SectionedRecyclerViewAdapter you can group your items in sections and add a header to each section:

class MySection extends StatelessSection {

    String title;
    List<String> list;

    public MySection(String title, List<String> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position));
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

Then you set up the RecyclerView with your Sections:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data you got from your API
MySection data1Section = new MySection("Data 1", data1List);
MySection data2Section = new MySection("Data 2", data2List);

// Add your Sections to the adapter
sectionAdapter.addSection(data1Section);
sectionAdapter.addSection(data2Section);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
like image 80
Gustavo Avatar answered Oct 16 '22 23:10

Gustavo


As you have multiple collections and and each collection has a list of items that you want displayed. This can be done easily using expandable listview. You display a list of headers, which on click expand to show list of items under the header. Here is a tutorial on its implementation

like image 45
suku Avatar answered Oct 17 '22 01:10

suku