问题描述
我已经创建了一个使用 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;
}
}
您可能已经注意到了,我扔的RuntimeException的getItemPosition,因为我想检查,当它被称为。而且它不叫,直到我补充一下,列出包含我的照片。然后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();
}
}
code是有点乱后,我试图修复它。但是:删除 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.
推荐答案
与片段寻呼机适配器处理可以皮塔。
Dealing with fragment pager adapters can be a PITA.
下面是一些有用的提示:
Here are a few helpful tips:
ViewPager PagerAdapter更新不及时的查看
一般来说这个工作99%的时间......
Generally speaking this one works 99% of the time...
覆盖getItemPosition在PagerAdapter是这样的:
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图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!