Android ViewPager调用错误片段onCreateView

时间:2016-03-23 11:24:41

标签: android android-fragments android-viewpager

我有两页的ViewPager:

MainActivity:

pageCount = 2;
pager = (ViewPager) findViewById(R.id.pagerDashboard);
pagerAdapter = new DashboardFragmentAdapter(getSupportFragmentManager(),pageCount);
pager.setAdapter(pagerAdapter);

DashboardFragmentAdapter:

public class DashboardFragmentAdapter extends FragmentPagerAdapter {
int pageCount;

public DashboardFragmentAdapter(FragmentManager fm,int pageCount) {
    super(fm);
    this.pageCount = pageCount;
}
@Override
public Fragment getItem(int position) {
    return  DashboardFragment.newInstance(position);
}

@Override
public int getCount() {
    return pageCount;
}}

片段:

public class DashboardFragment extends Fragment {

private static final String DASHBOARD_INDEX = "dashboard_index";
static final String TAG = "myLogs";
int dashboardIndex;

public DashboardFragment() {
    // Required empty public constructor
}

public static DashboardFragment newInstance(int dashboardIndex) {

    DashboardFragment fragment = new DashboardFragment();
    Bundle args = new Bundle();
    args.putInt(DASHBOARD_INDEX, dashboardIndex);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        dashboardIndex = getArguments().getInt(DASHBOARD_INDEX);
    }
    Log.d(TAG, "onCreate: " + dashboardIndex);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
    Log.d(TAG, "onCreateView: " + dashboardIndex);
    return rootView;
}

}

记录结果:

 
D/myLogs: onCreate: 0
D/myLogs: onCreate: 1
D/myLogs: onCreateView: 1
W/FragmentManager: moveToState: Fragment state for DashboardFragment{193d3b90 #0 id=0x7f0e0087 android:switcher:2131624071:1} not updated inline; expected state 3 found 2
D/myLogs: onCreateView: 0

 

为什么onCreateView索引为1,在索引0之前调用。但onCreate调用正确? 什么是W / FragmentManager警告意味着什么?

在应用程序中,所有两个页面都已正确加载,但我们必须等到第二页才能获取第一页。

0 个答案:

没有答案