【发布时间】:2016-11-03 23:50:04
【问题描述】:
各位,有谁能帮帮我吗?
我有一个“小部件”对象的列表,对于这些小部件中的每一个,我需要创建一个带有 ViewPager 的 CardView,附加一个适配器并将其添加到我的 Activity 视图中。问题是只有第一个小部件有效,其他都是空白的!问题不在于“小部件”对象,因为如果我更改它们在数组中的顺序,第一个小部件总是可以工作的!这是我调用将视图添加到屏幕的方法:
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>
【问题讨论】:
-
请分享适配器和布局
-
@Hala.M 共享适配器和布局!
-
你可以试试 FragmentStatePagerAdapter 吗?看看有没有区别
-
@Hala.M 我试过了,但同样的事情发生了!
-
很困惑,因为您的布局中没有
CardView。我确实看到了ViewPager。
标签: android android-viewpager android-scrollview