How to use fragment with Viewpager for Swipe pages
Solution 1
I Found this tutorial realy clear and helpful for creating a Fragment ViewPager:
http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
Check it out, in addiotion please refer to here as well:
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 without tabs:
http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
UPDATE:
First of all remove this part:
<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>
Not the Fragments or the Adapter are not activities and you shouldn't write them into the manifest file.
and try to change this part:
<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>
To this:
<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>
Solution 2
Visit 'Replacing a fragment in a view pager', For view pager transactions. And when you are looking to study for fragments then visit-'http://developer.android.com/guide/components/fragments.html#Lifecycle'
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
I got doubt regarding Viewpager and Fragment .I want to create swipe view for that i need fragment classes with Viewpager .how could i implement such thing!!
i searched through net got something like this using 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); } }
Manifest File:
<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>
Error:
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
Could anybody guide me @thanks !!
-
Admin about 11 yearsThanks but i need without Tabs..how could i do that!!
-
Admin about 11 yearsthanks i am getting error using that!!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]
-
Emil Adz about 11 yearspost your ViewPagerFragmentActivity code and the manifest file code.
-
Admin about 11 yearsEmil Adz could you help me out!!
-
Admin about 11 yearsSino Raj could you help me out!!
-
Sino Raj about 11 years<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> </application>
-
Sino Raj about 11 yearsnot add all classes in your activity, and then remember all classes are not activity. Where u use fragments as activity, so please study about Activity and what that differs from fragments,adapter classes and so on
-
Sino Raj about 11 yearsAdd one fragment,adapter and view pager on layout in your post/question
-
Admin about 11 yearsSino Raj i have a doubt can we display the middle fragment first!!
-
Sino Raj about 11 yearsTheir is any error after run ur application?, when u want to show a page on view pager with ur on position then initialize the view pager, then write viewPagerName.setCurrentItem(position);
-
Admin about 11 yearsviewPagerName.setCurrentItem(position); Using this can i set the middle fragment as mainview and onswipe left first fragment and on swipe right third frag will show
-
Sino Raj about 11 yearsviewPagerName.setCurrentItem(1);, when u fine with my answer, then plz upvote, and tick as correct answer.
-
Admin about 11 yearsSino Raj k.. fine..let me check
-
Admin about 11 years@ Sino Raj no i didn't get