*Java code.
package view.pager.demo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
private int previousState, currentState;
private ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewPager = (ViewPager) findViewById(R.id.ViewPagerMainActivity);
viewPager.setAdapter(new CustomPageAdapter());
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setPageMargin(10);
}
OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
Log.e("onPageSelected", "pageSelected:" + pageSelected);
}
@Override
public void onPageScrolled(int pageSelected, float positionOffset,
int positionOffsetPixel) {
Log.e("onPageScrolled", "pageSelected" + pageSelected
+ ",positionOffset:" + positionOffset
+ ",positionOffsetPixel:" + positionOffsetPixel);
}
@Override
public void onPageScrollStateChanged(int state) {
//Comment this code if doesn't need circular pageviewer
Log.e("onPageScrollStateChanged", "state:" + state);
int currentPage = viewPager.getCurrentItem();
if (currentPage == 3 || currentPage == 0) {
previousState = currentState;
currentState = state;
if (previousState == 1 && currentState == 0) {
viewPager.setCurrentItem(currentPage == 0 ? 3 : 0);
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class CustomPageAdapter extends PagerAdapter {
@Override
public int getCount() {
return 4;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.d("test", "position:"+position);
LayoutInflater layoutInflater = (LayoutInflater) container
.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (position <= container.getChildCount() - 1)
view = container.getChildAt(position);
if (view != null)
return view;
switch (position) {
case 0:
view = layoutInflater.inflate(R.layout.view1, null);
break;
case 1:
view = layoutInflater.inflate(R.layout.view2, null);
break;
case 2:
view = layoutInflater.inflate(R.layout.view3, null);
break;
case 3:
view = layoutInflater.inflate(R.layout.view4, null);
break;
default:
break;
}
((ViewPager) container).addView(view, position);
return view;
}
@Override
public void destroyItem(View view, int position, Object object) {
((ViewPager) view).removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public Parcelable saveState() {
return null;
}
}
}
*XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/ViewPagerMainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
package view.pager.demo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
private int previousState, currentState;
private ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewPager = (ViewPager) findViewById(R.id.ViewPagerMainActivity);
viewPager.setAdapter(new CustomPageAdapter());
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setPageMargin(10);
}
OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
Log.e("onPageSelected", "pageSelected:" + pageSelected);
}
@Override
public void onPageScrolled(int pageSelected, float positionOffset,
int positionOffsetPixel) {
Log.e("onPageScrolled", "pageSelected" + pageSelected
+ ",positionOffset:" + positionOffset
+ ",positionOffsetPixel:" + positionOffsetPixel);
}
@Override
public void onPageScrollStateChanged(int state) {
//Comment this code if doesn't need circular pageviewer
Log.e("onPageScrollStateChanged", "state:" + state);
int currentPage = viewPager.getCurrentItem();
if (currentPage == 3 || currentPage == 0) {
previousState = currentState;
currentState = state;
if (previousState == 1 && currentState == 0) {
viewPager.setCurrentItem(currentPage == 0 ? 3 : 0);
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class CustomPageAdapter extends PagerAdapter {
@Override
public int getCount() {
return 4;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.d("test", "position:"+position);
LayoutInflater layoutInflater = (LayoutInflater) container
.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (position <= container.getChildCount() - 1)
view = container.getChildAt(position);
if (view != null)
return view;
switch (position) {
case 0:
view = layoutInflater.inflate(R.layout.view1, null);
break;
case 1:
view = layoutInflater.inflate(R.layout.view2, null);
break;
case 2:
view = layoutInflater.inflate(R.layout.view3, null);
break;
case 3:
view = layoutInflater.inflate(R.layout.view4, null);
break;
default:
break;
}
((ViewPager) container).addView(view, position);
return view;
}
@Override
public void destroyItem(View view, int position, Object object) {
((ViewPager) view).removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public Parcelable saveState() {
return null;
}
}
}
*XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/ViewPagerMainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
Simple and perfect for my need, thank
ReplyDeleteBut as we scroll the final/first item to the right/left the entire list gets scrolled befor the corresponding first/final item is reached.
ReplyDeleteNot Working
ReplyDeleteNot working
ReplyDeleteTerminating app while loading view3
how to make circular view pager
ReplyDeletehello ,
ReplyDeletehow to make view pager change smoothly when from tab 3 to tab 0. Although, i was use function viewPager.setCurrentItem(currentPage == 0 ? 3 : 0,true); . Please help me