Click on 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
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01