简体   繁体   English

在Android中使用ViewPager的片段

[英]Using Fragments for ViewPager in Android

Can anyone tell me why this isnt working? 谁能告诉我为什么这不起作用? I havent changed this file since it worked last but now I get the error below: 我没有更改此文件,因为它最后工作但现在我收到以下错误:

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

private final static int NUM_PAGES = 2;
private ViewPager mPager;
private ScreenSlidePagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(2);
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {

    Fragment frag = null;

    switch (i) {
    case 0:
        frag = new PageOneFragment();
        break;
    case 1:
        frag = new PageTwoFragment();
        break;

    }
    return frag;
}

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

The error is on "frag = new PageTwoFragment();" 错误发生在“frag = new PageTwoFragment();” which states "Type mismatch: cannot convert from PageTwoFragment to Fragment". 其中指出“类型不匹配:无法从PageTwoFragment转换为Fragment”。

Maybe I should create two projects from now on, last good version and then current working project. 也许我应该从现在开始创建两个项目,最后一个好的版本,然后是当前的工作项目。 Is this something other people do? 这是其他人做的吗?

Thanks 谢谢

The Problem is, that you are mixing Android Fragments how there were introduced in API11 and Fragments from android support library. 问题是,你正在混合Android片段如何在API11和android支持库中的片段中引入。

You have to use one or the other, but not both. 你必须使用其中一个,但不能同时使用两个。

Change your imports to 将导入更改为

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

And change getFragmentManager() to getSupportFragmentManager() . 并将getFragmentManager()更改为getSupportFragmentManager()

And it will work using the support lib. 它将使用支持库工作。

Calling mPager.setCurrentItem(2); 调用mPager.setCurrentItem(2); with a pager of 2 Fragments crashs, though. 但是,有2个碎片的寻呼机崩溃。 Only 0 and 1 are valid values in your case. 在您的情况下,只有01是有效值。

You imported the wrong Fragment. 您导入了错误的片段。

You need to import android.support.v4.Fragment 您需要导入android.support.v4.Fragment

Furthermore, your adapter only handles 2 Fragments. 此外,您的适配器只处理2个碎片。 Therefore calling 所以打电话

ViewPager.setCurrentItem(2);

will cause problems, since the index for the first fragment is 0. 会导致问题,因为第一个片段的索引是0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM