发生了什么
我的应用程序中的ViewPager显示了我在第一页上实例化的所有视图。例如,如果我实例化两个视图,则第一页显示两个视图,第二页只是空白。
显示问题的图片
如下所示,第一页包含两个页面,第二页为空。
我无法弄清楚为什么会这样。很长一段时间以来一直在努力解决这个问题。我尝试不将视图添加到适配器,添加视图并提供索引,而不是给它充气,但在适配器中创建ImageView
等。
我错过了什么?任何帮助将不胜感激!
代码
活动
[Activity(Label = "Foto's", ScreenOrientation = ScreenOrientation.Portrait)]
public class PicturesActivity : BaseActivity
{
private ImagePagerAdapter _imageAdapter;
private List<string> _filePaths;
private ViewPager _viewPager;
private const int _maxPictures = 5;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Pictures);
_viewPager = FindViewById<ViewPager>(Resource.Id.ViewPager);
}
protected override void OnStart()
{
base.OnStart();
_imageAdapter = new ImagePagerAdapter(this, _filePaths);
_viewPager.Adapter = _imageAdapter;
_viewPager.SetCurrentItem(0, false);
}
}
PagerAdapter
internal class ImagePagerAdapter : PagerAdapter
{
Activity _activity;
List<string> _filePaths;
public ImagePagerAdapter(Activity activity, List<string> filePaths)
{
_activity = activity;
_filePaths = filePaths;
}
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
{
var view = _activity.LayoutInflater.Inflate(Resource.Layout.PicturePageItem, container, false);
var imageView = view.FindViewById<ImageView>(Resource.Id.ImageView);
var filePath = _filePaths[position];
DisplayMetrics metrics = _activity.Resources.DisplayMetrics;
using (var bitmap = BitmapHelper.GetResizedBitmap(filePath, new Size(metrics.WidthPixels, metrics.HeightPixels)))
imageView.SetImageBitmap(bitmap);
container.JavaCast<ViewPager>().AddView(view);
return view;
}
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object @object)
{
var view = (LinearLayout)@object;
var imageView = view.FindViewById<ImageView>(Resource.Id.ImageView);
if (imageView != null && imageView.Drawable != null)
{
imageView.Drawable.Dispose();
imageView.SetImageBitmap(null);
}
container.JavaCast<ViewPager>().RemoveView(view);
}
public override int Count
{
get { return _filePaths.Count; }
}
public override bool IsViewFromObject(View view, Java.Lang.Object @object)
{
return view is LinearLayout;
}
}
图片布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/ViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
PicturePageItem布局(适配器页面布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside" />
</LinearLayout>
答案 0 :(得分:1)
我认为问题可能是由于您的方法 IsViewFromObject()。
调用此方法以将View与 Key 对象进行比较。您的版本始终返回 true ,以便ViewPager认为您的所有观看次数都在同一页上。
你应该修改你的代码:
public override bool IsViewFromObject(View view, Java.Lang.Object @object)
{
return (object)view == @object;
}
有关详细信息,请参阅:
http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html