我的結果應該像在每個片段中顯示圖像和文本。 我在視圖中使用片段尋呼機異步任務中的刷新片段
由於圖像是高分辨率,我使用異步任務下載並保存在SD卡中。
如果下載圖像,我的代碼工作正常,如果圖像不存在,下載圖像後,碎片不會刷新,因此圖像無法顯示。完成圖像下載後如何刷新片段。
我在活動設置片段代碼
ExtendedViewPager pager = (ExtendedViewPager) findViewById(R.id.view_pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
pagerAdapter = new MyGalleryPagerAdapter(fm,eventArrayList);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
pager.setOnPageChangeListener(new MyPageChangeListener());
pager.setCurrentItem(currentpage);
我的適配器代碼是
public class MyGalleryPagerAdapter extends FragmentStatePagerAdapter implements{
public static int mCurrentPage;
/** Constructor of the class
* @param eventArrayList */
ArrayList<EventsGalleryDetails> events;
public MyGalleryPagerAdapter(FragmentManager fm, ArrayList<EventsGalleryDetails> eventArrayList) {
super(fm);
events=eventArrayList;
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
System.out.println("Get Item");
MyGallery myFragment = new MyGallery();
Bundle data = new Bundle();
data.putInt("current_page", arg0);
myFragment.setArguments(data);
mCurrentPage = arg0;
return myFragment;
}
public int currentItem(){
return mCurrentPage;
}
public int getItemPosition(Object item) {
return POSITION_NONE;
}
/** Returns the number of pages */
@Override
public int getCount() {
return events.size();
}
我的片段代碼
public class MyGallery extends Fragment implements OnPageChangeListener{
ImageLoader imageLoader;
int mCurrentPage;
Bitmap bm ;
public static final String TAG = "MyGallery";
private LruCache<String, Bitmap> mMemoryCache;
private HashMap<Integer, TouchImageView>hmap = new HashMap<Integer, TouchImageView>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageLoader=new ImageLoader(getActivity());
Bundle data = getArguments();
mCurrentPage = data.getInt("current_page", 0);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory/8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle data = getArguments();
mCurrentPage = data.getInt("current_page", 0);
View v = inflater.inflate(R.layout.gallerysecondscreenitem, container,false);
TouchImageView img = (TouchImageView)v.findViewById(R.id.galleryimage);
TextView txt = (TextView) v.findViewById(R.id.gallerytext);
hmap.put(mCurrentPage, img);
if(new File(Constants.Path+".jpg").exists()){
loadBitmap(img);
bm = null;
img = null;
}else{
img.setImageResource(R.drawable.gallery_icon);
}
txt.setText(GallerySecondScreen.eventArrayList.get(mCurrentPage).getTitle());
return v;
}
public void loadBitmap(TouchImageView mImageView) {
final String imageKey = String.valueOf(mCurrentPage);
final Bitmap bitmap = mMemoryCache.get(imageKey);
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
} else {
mImageView.setImageResource(R.drawable.gallery_icon);
BitmapWorkerTask task = new BitmapWorkerTask();
task.execute(mImageView);
}
}
爲downlaoding像我ASYC任務是
private class DownloadImageTask extends AsyncTask<Integer, Integer, Integer>{
@Override
protected Integer doInBackground(Integer... values) {
try {
for(int j=0;j<eventArrayList.size();j++){
if(!new File(Constants.Path+".jpg").exists()){
downloadImageFromServer(eventArrayList.get(j).getImage(), ""+pos,""+j);
publishProgress(j);
//Log.d("Image will be downloaded from server now..", "the "+values[0]);
}else{
publishProgress(j);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("value of j:",""+j);
return j;
}}
請建議的方式完成我的圖片下載後刷新視圖
這是我之前發佈的解決方案中發佈的解決方案,請驗證它 – Chetu
這是不公平的。您通過複製@Chetu回覆發佈答案並標記爲已回答。你應該接受切圖的答覆。 – Mazhar