Android ViewPager 不会显示第二页?
Android ViewPager Won't Show Second Page?
我正在使用 ViewPager 滑动屏幕的一部分(而不是整个 View/Page)。
我可以将 isViewFromObject 修改为 return true,然后我的第一张图片出现,但第二张图片无法加载。对我在这里缺少的东西有什么想法吗?我在想我的 isViewFromObject 方法或实例化项目中的 for 循环仍然有问题。
注意:有意不使用扩展 FragmentStatePagerAdapter 和扩展常规 PagerAdapter。
private class ScreenSlidePagerAdapter extends PagerAdapter {
public ScreenSlidePagerAdapter() {}
@Override
public Object instantiateItem (ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int RLayoutId;
RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout
ViewGroup imageLayout = (ViewGroup) inflater.inflate(RLayoutId, null);
for (position = 0; position < mImageURLArraylist.size(); position++) {
container.addView(insertPhoto("http:" + mImageURLArraylist.get(position) ) );
}//end of for loop
return imageLayout;
}
@Override
public boolean isViewFromObject (View view, Object obj) { //Object parameter is received from instantiateItem(ViewGroup, int)
//loads the first image successfully if i just do return true.
//doesn't load any of my images when I do return view == ( (View) obj);
}
//one of four methods that you must override when using pageradapters
@Override
public int getCount() {
//tested in debugging and this has the correct size (2)
return mImageURLArraylist.size(); //the number of pages the adapter will create.
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}//end of ScreenSlidePagerAdapter
我在上面的 instantiateItem 中执行的 insertPhoto 方法:
public View insertPhoto(String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
return layout;
}
您需要将视图添加到容器中。尝试按此处所示更改您的方法:
@Override
public Object instantiateItem (ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int RLayoutId;
RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout
ViewGroup imageLayout = (ViewGroup) inflater.inflate(RLayoutId, container, false);
container.addView(insertPhoto(imageLayout, "http:" + mImageURLArraylist.get(position) ));
return imageLayout;
}
// this needs to be refactored, too many viewgroups. Move all layouts to XML
public View insertPhoto(ViewGroup root, String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
root.addView(layout);
return root;
}
如果您的布局全部在 XML 中完成就更好了,因此 insertPhoto 函数只需调用 Picasso 来加载图像。
每个页面都会调用PagerAdapter的instantiateItem()方法,所以容器中只能插入一个page/child。
@Override
public Object instantiateItem (ViewGroup container, int position) {
final View page = insertPhoto("http:" + mImageURLArraylist.get(position) );
container.addView(page);
return page;
}
@Override
public boolean isViewFromObject (View view, Object obj) {
return view == obj;
}
我正在使用 ViewPager 滑动屏幕的一部分(而不是整个 View/Page)。
我可以将 isViewFromObject 修改为 return true,然后我的第一张图片出现,但第二张图片无法加载。对我在这里缺少的东西有什么想法吗?我在想我的 isViewFromObject 方法或实例化项目中的 for 循环仍然有问题。
注意:有意不使用扩展 FragmentStatePagerAdapter 和扩展常规 PagerAdapter。
private class ScreenSlidePagerAdapter extends PagerAdapter {
public ScreenSlidePagerAdapter() {}
@Override
public Object instantiateItem (ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int RLayoutId;
RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout
ViewGroup imageLayout = (ViewGroup) inflater.inflate(RLayoutId, null);
for (position = 0; position < mImageURLArraylist.size(); position++) {
container.addView(insertPhoto("http:" + mImageURLArraylist.get(position) ) );
}//end of for loop
return imageLayout;
}
@Override
public boolean isViewFromObject (View view, Object obj) { //Object parameter is received from instantiateItem(ViewGroup, int)
//loads the first image successfully if i just do return true.
//doesn't load any of my images when I do return view == ( (View) obj);
}
//one of four methods that you must override when using pageradapters
@Override
public int getCount() {
//tested in debugging and this has the correct size (2)
return mImageURLArraylist.size(); //the number of pages the adapter will create.
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}//end of ScreenSlidePagerAdapter
我在上面的 instantiateItem 中执行的 insertPhoto 方法:
public View insertPhoto(String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
return layout;
}
您需要将视图添加到容器中。尝试按此处所示更改您的方法:
@Override
public Object instantiateItem (ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int RLayoutId;
RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout
ViewGroup imageLayout = (ViewGroup) inflater.inflate(RLayoutId, container, false);
container.addView(insertPhoto(imageLayout, "http:" + mImageURLArraylist.get(position) ));
return imageLayout;
}
// this needs to be refactored, too many viewgroups. Move all layouts to XML
public View insertPhoto(ViewGroup root, String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
root.addView(layout);
return root;
}
如果您的布局全部在 XML 中完成就更好了,因此 insertPhoto 函数只需调用 Picasso 来加载图像。
每个页面都会调用PagerAdapter的instantiateItem()方法,所以容器中只能插入一个page/child。
@Override
public Object instantiateItem (ViewGroup container, int position) {
final View page = insertPhoto("http:" + mImageURLArraylist.get(position) );
container.addView(page);
return page;
}
@Override
public boolean isViewFromObject (View view, Object obj) {
return view == obj;
}