点击Viewpager Xamarin Android

时间:2015-10-19 07:38:16

标签: android xamarin android-viewpager onclicklistener

我是Xamarin Android Development的新手。我使用 ViewPager 创建了Image滑块。现在我想点击ViewPager的图像。我可以实现吗? 我试过这个但没有用过:

{
  viewpage = mview.FindViewById<ViewPager> (Resource.Id.viewpager);
  viewpage.Adapter = new itemadapter (ChildFragmentManager);
  viewpage.Click += Viewpage_Click;
}
public void Viewpage_Click (object sender, EventArgs e)
{
    ViewModel.DetailViewCommand.Execute ();
}

2 个答案:

答案 0 :(得分:5)

如果您只有图片滑块,请在onclick listener适配器中为imageview实施viewpager's

修改:从网络下载图片的imageslider viewpager的完整来源。

public class ImageSliderAdapter : PagerAdapter
    {
        Context _context;
        List<string> _imageUrls;

        public ImageSliderAdapter (Context context, List<string> imageUrls)
        {
            _imageUrls = imageUrls;
            _context = context;
        }

        public override bool IsViewFromObject (Android.Views.View view, Java.Lang.Object @object)
        {
            return view == ((LinearLayout)@object);
        }

        public override int Count {
            get {
                return _imageUrls.Count;
            }
        }

        public override void DestroyItem (ViewGroup container, int position, Java.Lang.Object objectValue)
        {
        }

        public override Java.Lang.Object InstantiateItem (ViewGroup container, int position)
        {

            View view = container;
            var inflater = _context.GetSystemService (Context.LayoutInflaterService) as LayoutInflater;
            view = inflater.Inflate (Resource.Layout.image_slider_item, null);
            var child = view.FindViewById<ImageView> (Resource.Id.image_slider_item);
            child.Click += (o, e) =>
            {
                //your code here
            };

            Bitmap image = null;
            Task.Run (() => {
                URL url = new URL (_imageUrls [position]);
                image = BitmapFactory.DecodeStream (url.OpenConnection ().InputStream);
            }).ContinueWith (t => {
                (_context as MainView).RunOnUiThread (() => {
                    child.SetImageBitmap (image);
                });
            });

            container.AddView (view);
            return view;
        }
    }

活动:

            var imageViewer = FindViewById<ViewPager> (Resource.Id.pager);
            imageViewer.Adapter = new ImageSliderAdapter (this, imageUrls);

答案 1 :(得分:0)

您可以这样使用

public class TourPagerAdapter : PagerAdapter
   {
       private Context mContext;
        LayoutInflater mLayoutInflater;

        public TourPagerAdapter(Context context)
        {
            mContext = context;
            mLayoutInflater = (LayoutInflater)mContext.GetSystemService(Context.LayoutInflaterService);
        }

        public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {
            View itemView = mLayoutInflater.Inflate(Resource.Layout.TourItemLayout, container, false);
            ImageView imageView = itemView.FindViewById<ImageView>(Resource.Id.TextTour);

            if (position == 0)
            {
                imageView.SetImageResource(Resource.Drawable.Tour1Text);

            }
            else if (position == 1)
            {
                imageView.SetImageResource(Resource.Drawable.Tour2Text);

            }
            else
            {
                imageView.SetImageResource(Resource.Drawable.Tour3Text);

            }

            imageView.Click+=delegate {

                 //Your code
            };

            container.AddView(itemView);
            return itemView;
        }

        public override int Count => 3;

        public override bool IsViewFromObject(View view, Java.Lang.Object @object)
        {
            return view == @object;
        }

        public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object @object)
        {
            container.RemoveView((View)@object);
        }
    }