简体   繁体   中英

Update Imageview inside Fragment with Viewpager

I got a Viewpager which contains fragments of which each only contains an imageView.

Thats my adapter Class:

public class customAdapter_ProfilePictureAdapter extends FragmentPagerAdapter    {

Context context;
ArrayList<String> pictureList;
Bundle bundle;
int size;
String TAG="profileAdapter";

public customAdapter_ProfilePictureAdapter(Context c, FragmentManager fm, ArrayList<String> pictureList, int size) {

    super(fm);

    this.pictureList=pictureList;
    this.size=size;

    context=c;
}

@Override
public Fragment getItem(int position) {

        fragment_act_1_3_profile_pictures pictureFragment = new fragment_act_1_3_profile_pictures();
        bundle = new Bundle();
        bundle.putString("picture", pictureList.get(position));
        Log.d(TAG, "getItem: "+pictureList.get(position));
        bundle.putInt("numPic", position);
        pictureFragment.setArguments(bundle);
        return pictureFragment;
}

@Override
public int getCount() {
    return size;
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}
}

The pictureList contains up to three URL. The following way I get the URLs:

public void getProfilePic(final Context context, String url, final ImageView imageView, final ArrayList<String> pictureList, final ViewPager profilePictureViewPager, final TabLayout pictureTabLayout, final FragmentManager fm, final String kind, final int turn){

    requestQueue= Volley.newRequestQueue(context);

    pictureList.clear();

    String userID = getUserId();

    final JsonObjectRequest request= new JsonObjectRequest(Request.Method.GET, url,null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {

                String firstPictureUri= response.getJSONArray("records").getJSONObject(0).getString("firstPicture");
                String secondPictureUri= response.getJSONArray("records").getJSONObject(0).getString("secondPicture");
                String thirdPictureUri= response.getJSONArray("records").getJSONObject(0).getString("thirdPicture");

                if(firstPictureUri.equals("n.a.")){
                    pictureList.add("android.resource://org.coerdt.kitebook/mipmap/default_pic");
                }
                else{
                    pictureList.add(firstPictureUri);
                }

                if(!secondPictureUri.equals("n.a.")){
                    pictureList.add(secondPictureUri);
                }

                if(!thirdPictureUri.equals("n.a.")){
                    pictureList.add(thirdPictureUri);
                }

                if(turn==0){
                    profilePictureAdapter = new customAdapter_ProfilePictureAdapter(context,fm ,pictureList,pictureList.size());
                    profilePictureViewPager.setAdapter(profilePictureAdapter);
                    pictureTabLayout.setupWithViewPager(profilePictureViewPager);

                }
                else{
                    profilePictureAdapter.notifyDataSetChanged();
                }



            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(request);

}

The "turn"-variable indicates if the Viewpager is populated the first time inside the current activity (turn==0), or if it is just updated (turn == 1)

When the user adds a/replaces a URL the Viewpager/the fragments dont get updated. I also tried

profilePictureViewPager.setAdapter(null);

when the Viewpager should be updated. I know for sure, that the pictureList contains the updated URL.

What am I doing wrong?

If you have used the FragmentPagerAdapter, you can change this and use FragmentStatePagerAdapter. This should work and you do not need to set your adapter to null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM