我使用像carousel这样的包含三个图像的viewpager,所以我想获得每个都是1,2,3的位置并将其显示为消息
示例:如果我拖到第二个,则返回2,如果我移动到3则返回3
// my form
ImageAdapter adapter = new ImageAdapter(this);
var viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
viewPager.PersistentDrawingCache = Android.Views.PersistentDrawingCaches.ScrollingCache;
viewPager.Adapter = adapter;
这是我的班级:
public class ImageAdapter : PagerAdapter
{
private Context context;
private string[] imageList = {
"image1",
"image2",
"image3",};
public ImageAdapter(Context context)
{
this.context = context;
}
public override int Count
{
get
{
return imageList.Length;
}
}
public override bool IsViewFromObject(View view, Java.Lang.Object objectValue)
{
return view == ((ImageView)objectValue);
}
public override Java.Lang.Object InstantiateItem(View container, int position)
{
ImageView imageView = new ImageView(context);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
Koush.UrlImageViewHelper.SetUrlDrawable(imageView, imageList[position]);
((ViewPager)container).AddView(imageView, 0);
return imageView;
}
public override void DestroyItem(View container, int position, Java.Lang.Object objectValue)
{
((ViewPager)container).RemoveView((ImageView)objectValue);
}
}
答案 0 :(得分:0)
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
System.out.println("position = " + position);
}
});