使用Viewpager更新Fragment中的Imageview

问题描述 投票:0回答:1

我有一个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。我通过以下方式获取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);

}

“turn”-variable指示Viewpager是否第一次填充在当前活动内(转= = 0),或者是否刚刚更新(转= = 1)

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

profilePictureViewPager.setAdapter(null);

什么时候应该更新Viewpager。我确信,pictureList包含更新的URL。

我究竟做错了什么?

android android-fragments android-viewpager
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.