This blog is related to viewpager auto scroll.
Create new android studio project.
activity_main.xml
pager_item.xml
ActivityMain.java
viewPager= (ViewPager)findViewById(R.id.pager);
intent=getIntent();
datalist= PassData.getInstance().getListData();
displayAdapter=new FullFrameDisplayAdapter(this,datalist);
viewPager.setAdapter(displayAdapter);
autoPlay(viewPager);
}
FullFrameDisplayAdapter.java
Thank you
I hope helps this post.
Create new android studio project.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
pager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
</RelativeLayout>
ActivityMain.java
public class SliderViewActivity extends AppCompatActivity {
public ViewPager viewPager;
FullFrameDisplayAdapter displayAdapter;
ArrayList<Integer> datalist;
public ViewPager viewPager;
FullFrameDisplayAdapter displayAdapter;
ArrayList<Integer> datalist;
int currentCount;
Intent intent;
private int clickposition=0;
public SliderViewActivity() {
// Required empty public constructor
}
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_full_frame_display);
Intent intent;
private int clickposition=0;
public SliderViewActivity() {
// Required empty public constructor
}
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_full_frame_display);
datalist.add(R.drawable.image1);
datalist.add(R.drawable.image2);
datalist.add(R.drawable.image3);
datalist.add(R.drawable.image4);
datalist.add(R.drawable.image5);
datalist.add(R.drawable.image6);
datalist.add(R.drawable.image7;
datalist.add(R.drawable.image8);
viewPager= (ViewPager)findViewById(R.id.pager);
intent=getIntent();
datalist= PassData.getInstance().getListData();
displayAdapter=new FullFrameDisplayAdapter(this,datalist);
viewPager.setAdapter(displayAdapter);
autoPlay(viewPager);
}
// This method play the infinite scroll
private void autoPlay(final ViewPager viewPager) {
viewPager.postDelayed(new Runnable() {
@Override
public void run() {
try {
if (displayAdapter != null && viewPager.getAdapter().getCount() > 0) {
int position = currentCount % displayAdapter.getCount();
currentCount++;
viewPager.setCurrentItem(position);
autoPlay(viewPager);
}
} catch (Exception e) {
Log.e("", "auto scroll pager error.", e);
}
}
}, 1000);
}
}
private void autoPlay(final ViewPager viewPager) {
viewPager.postDelayed(new Runnable() {
@Override
public void run() {
try {
if (displayAdapter != null && viewPager.getAdapter().getCount() > 0) {
int position = currentCount % displayAdapter.getCount();
currentCount++;
viewPager.setCurrentItem(position);
autoPlay(viewPager);
}
} catch (Exception e) {
Log.e("", "auto scroll pager error.", e);
}
}
}, 1000);
}
}
FullFrameDisplayAdapter.java
public class FullFrameDisplayAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<Integer> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullFrameDisplayAdapter(Activity activity,
ArrayList< Integer > imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imgDisplay;
FloatingActionButton btnClose;
inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.fullframe_item, container,
false);
imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
imgDisplay.setImageResource(_imagePaths.get(position));
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
private Activity _activity;
private ArrayList<Integer> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullFrameDisplayAdapter(Activity activity,
ArrayList< Integer > imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imgDisplay;
FloatingActionButton btnClose;
inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.fullframe_item, container,
false);
imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
imgDisplay.setImageResource(_imagePaths.get(position));
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
Thank you
I hope helps this post.
No comments:
Post a Comment