ViewPager/PagerAdapter 在第一页显示所有浏览量
ViewPager/PagerAdapter shows all views on first page
发生了什么事
我的应用程序中的 ViewPager 显示了我在第一页上实例化的所有视图。例如,如果我实例化两个视图,两个视图都显示在第一页,第二页只是空白。
显示问题的图像
正如您在下面看到的,第一页包含两个页面,第二页是空的。
我不明白为什么会这样。长期以来一直在努力解决这个问题。我尝试不将视图添加到适配器,添加视图并提供索引,不是膨胀它而是在适配器中创建 ImageView
等
我错过了什么?如有任何帮助,我们将不胜感激!
代码
Activity
[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>
我认为问题可能出在您的方法 IsViewFromObject().
调用此方法是为了将 View 与 Key Object 进行比较。您的版本始终返回 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
发生了什么事
我的应用程序中的 ViewPager 显示了我在第一页上实例化的所有视图。例如,如果我实例化两个视图,两个视图都显示在第一页,第二页只是空白。
显示问题的图像
正如您在下面看到的,第一页包含两个页面,第二页是空的。
我不明白为什么会这样。长期以来一直在努力解决这个问题。我尝试不将视图添加到适配器,添加视图并提供索引,不是膨胀它而是在适配器中创建 ImageView
等
我错过了什么?如有任何帮助,我们将不胜感激!
代码
Activity
[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>
我认为问题可能出在您的方法 IsViewFromObject().
调用此方法是为了将 View 与 Key Object 进行比较。您的版本始终返回 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