Update asynchronous fragment view: ImageView is not updated, but other children are updated in the view

I have a ViewPager that uses fragments ... fragments contain only a framelayout with ImageView and a large TextView as the name right now .. each image fragment is loaded asynchronously ..

My problem is after the completion of the asynchronous task, I see that the image title changes with a new value. but ImageView shows an image that only loads for the first time.

onCreateView method

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout containing a title and body text. ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.fragment_sliding, container, false); rootView.setId(imgId); return rootView; } 

When my asynchronous loader loads information

  @Override public void onLoadFinished(Loader<DImage> arg0, DImage arg1) { View v = getView(); ImageView im = (ImageView) v.findViewById(R.id.fragment_image); im.setImageBitmap(arg1.image); TextView tv = (TextView) v.findViewById(R.id.fragment_image_title); tv.setText(arg1.id + ""); } 

The result obtained is different for all fragments. Also the textview text is displayed differently when I smooth the image. I tried to set the image bitmap to zero and then update using a new bitmap .. but it does not work. any clue where am i going wrong?

EDIT: adding an XML fragment

  <ImageView android:id="@+id/fragment_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:keepScreenOn="true" /> <TextView android:id="@+id/fragment_image_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textSize="30dp" /> 
+4
source share
1 answer

I also had the same problem and spent a couple of hours finding a solution. Hope helps others in this decision.

I saw the Fragment go through onCreateView and I only uploaded the images when the fragment was visible in AsyncTask . And I checked that onPostExecute bitmaps are set to ImageView , but in vain. ImageView only showed the image for the first time. Even invalidate() did not help.

After some searching, I found that the FragmentPagerAdapter re-creates fragments. And for larger data it is recommended to use: FragmentStatePagerAdapter . It uses less memory and destroys fragments.

Read more here: FragmentStatePagerAdapter

+1
source

All Articles