3

我对 Viewpager 和 Fragment 有疑问。我想创建滑动视图,因为我需要 Viewpager 的片段类。我怎么能实现这样的东西!

我通过网络搜索使用viewpager得到了类似的东西!

   public class ViewPagerFragmentActivity extends FragmentActivity {

    private PagerAdapter mPagerAdapter;

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

        // initialsie the pager
        this.initialisePaging();
    }

    /**
     * Initialize the fragments to be paged
     */
    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Fragment0.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
        this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
    }
}

清单文件:

 <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="slider.example.ViewPagerFragmentActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="slider.example.Fragment0" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" />
        <activity android:name="slider.example.Fragment1" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" />
        <activity android:name="slider.example.Fragment2" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" />
<activity android:name=".MyPagerAdapter"></activity>
    </application>

错误:

04-05 13:22:39.804: E/AndroidRuntime(468): FATAL EXCEPTION: main
04-05 13:22:39.804: E/AndroidRuntime(468): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{slider.example/slider.example.ViewPagerFragmentActivity}: java.lang.ClassNotFoundException: slider.example.ViewPagerFragmentActivity in loader dalvik.system.PathClassLoader[/data/app/slider.example-2.apk]
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.os.Looper.loop(Looper.java:123)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-05 13:22:39.804: E/AndroidRuntime(468):  at java.lang.reflect.Method.invokeNative(Native Method)
04-05 13:22:39.804: E/AndroidRuntime(468):  at java.lang.reflect.Method.invoke(Method.java:507)
04-05 13:22:39.804: E/AndroidRuntime(468):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-05 13:22:39.804: E/AndroidRuntime(468):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-05 13:22:39.804: E/AndroidRuntime(468):  at dalvik.system.NativeStart.main(Native Method)
04-05 13:22:39.804: E/AndroidRuntime(468): Caused by: java.lang.ClassNotFoundException: slider.example.ViewPagerFragmentActivity in loader dalvik.system.PathClassLoader[/data/app/slider.example-2.apk]
04-05 13:22:39.804: E/AndroidRuntime(468):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-05 13:22:39.804: E/AndroidRuntime(468):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-05 13:22:39.804: E/AndroidRuntime(468):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-05 13:22:39.804: E/AndroidRuntime(468):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-05 13:22:39.804: E/AndroidRuntime(468):  ... 11 more

任何人都可以指导我@thanks !

4

2 回答 2

5

我发现本教程对于创建 Fragment ViewPager 非常清晰和有帮助:

http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/

看看吧,另外请参考这里:

http://androidtrainningcenter.blogspot.co.il/2012/10/viewpager-example-in-android.html http://manishkpr.webheavens.com/android-viewpager-example/ http://manishkpr.webheavens.com /android-viewpager-circle-style-example/

没有标签的 ViewPager:

http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

更新:

首先删除这部分:

<activity android:name="slider.example.Fragment0" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" />
<activity android:name="slider.example.Fragment1" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" />
<activity android:name="slider.example.Fragment2" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" />
<activity android:name=".MyPagerAdapter"></activity>

不是片段或适配器不是活动,您不应该将它们写入清单文件。

并尝试更改这部分:

<activity android:name="slider.example.ViewPagerFragmentActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

对此:

<activity android:name=".ViewPagerFragmentActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
于 2013-04-05T07:45:37.343 回答
1

访问“替换视图寻呼机中的片段”,查看寻呼机事务。当您正在寻找研究片段时,请访问-' http://developer.android.com/guide/components/fragments.html#Lifecycle '

于 2013-04-05T07:46:04.923 回答