点击 Viewpager Xamarin Android

Click on Viewpager Xamarin Android(点击 Viewpager Xamarin Android)

本文介绍了点击 Viewpager Xamarin Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Xamarin Android Development 的新手.我使用 ViewPager 创建了图像滑块.现在我想点击该 ViewPager 的图像.我该如何实现?我已经尝试过了,但没有成功:

I am new to Xamarin Android Development.I have created Image slider using ViewPager.Now I want click on image of that ViewPager.How can I implement that ? I have tried this but not worked:

{
  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 ();
}

推荐答案

如果你有 viewpager's 适配器,只需为 imageview 实现 onclick listener仅图像滑块.

Just implement onclick listener for imageview in viewpager's adapter if you have only image slider.

imageslider viewpager 的完整源代码,可从网络下载图像.

full source of imageslider viewpager that downloads images from web.

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);

这篇关于点击 Viewpager Xamarin Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:点击 Viewpager Xamarin Android

基础教程推荐