人们,任何人都可以帮助我吗?
我有一个" Widget"对象,对于每个小部件,我需要创建一个带有ViewPager的CardView,附加一个适配器并将其添加到我的Activity视图中。问题是只有第一个小部件工作,其他都是空白!问题不在于" Widget"对象,因为如果我在数组中更改它们的顺序,第一个小部件总是要工作的!这是我调用将视图添加到屏幕的方法:
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);
}
}
}
适配器:
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();
}
}
布局:
<?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>
答案 0 :(得分:0)
我设法找到了解决方案!对于那些有同样问题的人,请看另一个问题的答案:https://stackoverflow.com/a/12894074/2568851
你要做的是为每个viewpager分配一个不同的ID,所以我所做的只是在这一行:
final ViewPager viewPager = (ViewPager) v.findViewById(R.id.view_pager);
我补充说:
viewPager.setId(iterator+1);
而不是它像魅力一样:)