点击 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)