Today I study about Viewpager beacuse I'm new in android.
But I want to make a viewpager stops when the last item of card.
I tried to search the method that i want. But i fail to find..
One of the way that i find was that the getcount method change xxx.size()-1. But it didn't work for me.. So.. If there is a person who know this way that I want, please let me know how to change the code.
Here is code.
public class AutoScrollAdapter extends PagerAdapter {
Context mContext;
ArrayList<Integer>data;
public AutoScrollAdapter(Context mContext, ArrayList<Integer> data) {
this.mContext = mContext;
this.data = data;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.auto_viewpager,null);
ImageView image_container =(ImageView)v.findViewById(R.id.image_container);
Glide.with(mContext).load(data.get(position)).into(image_container);
container.addView(v);
return v;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return data.size()-1;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
public class MainActivity extends AppCompatActivity {
AutoScrollViewPager autoViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = findViewById(R.id.btn_yoga1);
ImageButton btn1 = findViewById(R.id.btn_yoga2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Integer> data = new ArrayList<>();
data.add(R.drawable.a1);
data.add(R.drawable.a2);
data.add(R.drawable.a3);
data.add(R.drawable.a4);
data.add(R.drawable.a5);
data.add(R.drawable.a6);
data.add(R.drawable.a7);
data.add(R.drawable.a8);
data.add(R.drawable.a9);
data.add(R.drawable.a10);
data.add(R.drawable.a11);
autoViewPager = (AutoScrollViewPager) findViewById(R.id.autoViewPager);
AutoScrollAdapter scrollAdapter = new AutoScrollAdapter(MainActivity.this, data);
autoViewPager.setAdapter(scrollAdapter);
autoViewPager.setInterval(5000);
autoViewPager.startAutoScroll();
}
});
I find the way....
There was a method in the AutoScrollViewPager class.
/**
* set whether automatic cycle when auto scroll reaching the last or first item, default is true
*
* @param isCycle the isCycle to set
*/
public void setCycle(boolean isCycle) {
this.isCycle = isCycle;
}