使用Viewpager更新Fragment中的Imageview

时间:2018-02-28 21:28:53

标签: android android-fragments android-viewpager

我有一个Viewpager,其中包含片段,每个片段只包含一个imageView。

这是我的适配器类:

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;
}
}

pictureList最多包含三个URL。我通过以下方式获取网址:

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);

}

&#34;转&#34; -variable指示Viewpager是否第一次在当前活动中填充(转= = 0),或者它是否刚刚更新(转= = 1)

当用户添加/替换URL时,Viewpager /片段不会更新。我也试过

profilePictureViewPager.setAdapter(null);

应该更新Viewpager。 我确信,pictureList包含更新的URL。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您已使用FragmentPagerAdapter,则可以更改此设置并使用FragmentStatePagerAdapter。这应该有效,您不需要将适配器设置为null。