0

People, can anyone help me with this?

I have a list of "Widget" objects, and for each of these widgets I need to create a CardView with a ViewPager inside it, attach an adapter and add it to my Activity's view. The problem is that only the first widget works, the others are all blank!! The problem is not with the "Widget" objects because if I change their order inside the array, the first widget is always the one to work! This is the method I call to add the views to the screen:

private void setUpWidgets(){
    int widgetsSize = widgets.size();
    layoutToAddViews.removeAllViews();
    for(int iterator = 0; iterator<widgetsSize; iterator++) {
        Widget currentWidget = widgets.get(iterator);
        if (currentWidget.getIs_enabled()) {
                    final WidgetPagerAdapter pagerAdapter = new WidgetPagerAdapter(getChildFragmentManager(), currentWidget, getContext());
                    final View v = LayoutInflater.from(getContext()).inflate(R.layout.list_item_downline_reporting_widget_tabbed, layoutToAddViews, false);
                    views.add((CardView) v);
                    final ViewPager viewPager = (ViewPager) v.findViewById(R.id.view_pager);
                    final LinearLayout indicatorsParent = (LinearLayout) v.findViewById(R.id.linearIndicatorsParent);
                    final int size = currentWidget.getTile_items().size();
                    ((TextView) v.findViewById(R.id.widgetTitle)).setText(currentWidget.getTitle());
                    layoutToAddViews.addView(v);
                    viewPager.setAdapter(pagerAdapter);
                    viewPager.setOffscreenPageLimit(3);
         }
    }
}

The Adapter:

public class WidgetPagerAdapter extends FragmentPagerAdapter {

ArrayList<Object> items;
ArrayList<String> slugs;
Context context;
HashMap<Integer,Fragment> fragments;
Widget widget;

public Fragment getFragments(int position) {
    return fragments.get(position);
}

public WidgetPagerAdapter(FragmentManager fm, Widget widget, Context context) {
    super(fm);
    this.widget = widget;
    this.items = widget.getTile_items();
    this.slugs = widget.getTile_item_slugs();
    this.context = context;
    this.fragments = new HashMap<>(items.size());
}

@Override
public Fragment getItem(int position) {
    /*
        Creates the specific fragment depending on which widget slug it is.
     */
    Fragment fragment;

    switch (slugs.get(position)){

        case Widget.MY_STATUS_SLUG:
            fragment = MyStatusFragment.newInstance((ProfileItem) items.get(position));
            break;

        case Widget.RANK_REPORT_GOAL_SLUG:
            fragment = RankReportFragment.newInstance((RankReportItem) items.get(position));
            break;

        case Widget.RANK_REPORT_ATTAINED_SLUG:
            fragment = RankReportFragment.newInstance((RankReportItem) items.get(position));
            break;

        case Widget.RANK_REPORT_PREVIOUS_SLUG:
            fragment = RankReportFragment.newInstance((RankReportItem) items.get(position));
            break;

        case Widget.NEW_ENROLLMENTS_SLUG:
            fragment = NewEnrollmentsFragment.newInstance((NewEnrollmentsItem) items.get(position));
            break;

        case Widget.VOLUME_HISTORY_SLUG:
            fragment = VolumeHistoryFragment.newInstance((VolumeHistoryItem) items.get(position));
            break;

        case Widget.MY_AUTOSHIP_SLUG:
            fragment = MyAutoshipFragment.newInstance((MyAutoshipItem) items.get(position));
            break;

        case Widget.ADDITIONAL_SERVICES_SLUG:
            fragment = null;
            break;

        default:
            fragment = null;
    }

    fragments.put(position, fragment);
    return fragment;
}

@Override
public int getCount() {
    return items.size();
}

}

The Layout:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/white"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Status"
        android:textColor="@color/black"
        android:layout_margin="10dp"
        android:id="@+id/widgetTitle"
        android:textSize="16sp"
        />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_alignParentBottom="true"
            >

        <LinearLayout
            android:layout_alignParentBottom="true"
            android:id="@+id/linearIndicatorsParent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            >


        </LinearLayout>

        <FrameLayout
            android:visibility="invisible"
            android:id="@+id/frameSelectedPage"
            android:elevation="3dp"
            android:layout_width="8dp"
            android:layout_height="8dp"
            android:background="@drawable/blue_circle"
            ></FrameLayout>

        </RelativeLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="490dp"></android.support.v4.view.ViewPager>

    </RelativeLayout>

</android.support.v7.widget.CardView>

1 Answers1

0

I managed to find the solution! To those having the same problem, take a look at this answer on another question: https://stackoverflow.com/a/12894074/2568851

What you have to do is assign a different ID to each viewpager, so what I did was just bellow this line:

final ViewPager viewPager = (ViewPager) v.findViewById(R.id.view_pager);

I added:

viewPager.setId(iterator+1);

And not it works like a charm :)

Community
  • 1
  • 1