我创建了一个使用 FragmentStatePagerAdapter
提供小型画廊的活动.但是,当活动恢复时(例如,从其他活动回来后),我无法让它刷新.每次前两张图片都是空白的,只有在我将两张图片滑到一边后,它们才会刷新.我找到的答案都没有工作(尤其是覆盖 getItemPosition()
)
I have created an activity that uses FragmentStatePagerAdapter
to provide small gallery. However, I can't get it to refresh when activity resumes (after coming back from other activity, for example). Every time first two pictures will be blank, and only after i swipe two pictures to the side, they get refreshed. None of the answers I've found work (especially overriding getItemPosition()
)
我是这样设置的:
mPagerAdapter = new PhotosPagerAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.photosViewPager);
mPager.setAdapter(mPagerAdapter);
然后我有 FragmentStatePagerAdapter 类:
Then I have FragmentStatePagerAdapter class:
private class PhotosPagerAdapter extends FragmentStatePagerAdapter{
public PhotosPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return photos.size();
}
@Override
public Fragment getItem(int position) {
ImageFragment f = new ImageFragment(position);
return f;
}
@Override
public int getItemPosition(Object object) {
throw new RuntimeException();
//return POSITION_NONE;
}
}
您可能已经注意到,我在 getItemPosition 中抛出 RuntimeException,因为我想检查它何时被调用.直到我添加一些包含我的图片的列表时才会调用它.然后是 ImageFragment 类:
As you probably noticed, I throw RuntimeException in getItemPosition, because I wanted to check when it's called. And it isn't called until I add something to list containing my pictures. Then ImageFragment class:
public class ImageFragment extends Fragment{
int position;
Bitmap mBitmap;
int width;
int height;
ImageView img;
public ImageFragment(){
}
public ImageFragment(int position){
this.position = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
img = new ImageView(container.getContext());
img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
width = container.getWidth();
height = container.getHeight();
loadBitmap();
return img;
}
public void loadBitmap(){
if (img == null){
return;
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photos.get(position), options);
options.inSampleSize = calculateInSampleSize(options, width/2, height/2);
options.inJustDecodeBounds = false;
mBitmap = BitmapFactory
.decodeFile(photos.get(position), options);
img.setImageBitmap(mBitmap);
}
@Override
public void onDestroyView() {
mBitmap.recycle();
super.onDestroyView();
}
}
在我尝试修复它之后代码有点乱......但是:删除 onDestroyView()
不起作用.我已将 mPagerAdapter.notifyDataSetChanged()
放在几个必须调用的地方(如 onResume()
),但没有结果.我对此感到有些绝望.
Code is kinda messy after I tried to fix it... But: removing onDestroyView()
doesn't work. I have put mPagerAdapter.notifyDataSetChanged()
in several places that must be called (like onResume()
), with no result. I'm getting kinda desperate with that.
处理片段分页器适配器可以是 PITA.
Dealing with fragment pager adapters can be a PITA.
以下是一些有用的提示:
Here are a few helpful tips:
ViewPager PagerAdapter 不更新视图
动态更新 ViewPager?
一般来说,这个有 99% 的时间都在工作......
Generally speaking this one works 99% of the time...
像这样覆盖 PagerAdapter 中的 getItemPosition:
Override getItemPosition in your PagerAdapter like this:
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
有时即使这些都不起作用,我必须使用蛮力"方法并重新创建整个视图(从 onCreate 开始)...
Sometimes even those don't work, I have to use the 'brute force' method and recreate the entire view again (from onCreate onwards)...
这篇关于在恢复活动时刷新 FragmentStatePagerAdapter 上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!