动态更新 Viewpager 数据

Update Viewpager data dynamically

我有一个自定义模型 class 的 ArrayList,我使用 AsyncTask[=20 实例化了 Home_Fragment 中从互联网上获取的图像 URL =]

第一次创建列表时,它有 12 个 items/URL 来自网站。 现在,我将这些传递给链接到我的 ViewPager Adapter

FullscreenView Activity

-> 我在我的 Home_Fragment 中设置了一个 onLoadMoreListener 以将更多项目添加到 ArrayList,现在我想将它们动态添加到我的 ViewPager Adapter, 我怎样才能做到这一点?

Home_Fragment

    class ReadHTML extends AsyncTask<String, Integer, String> {

    List list;

    @Override
    protected String doInBackground(String... params) {
            try {

                Document document = Jsoup.connect(params[0]).get();
                Element wall = document.select("ul.postul").first();
                //Log.i("LIST ", wall.toString());
                Elements url = wall.getElementsByAttribute("src");
                list = url.eachAttr("src");

                for (int i = 0; i < list.size(); i++) {
                    wallpaperNumber++;
                    String string = //Dummy
                    String septemp[] = //Dummy2
                    sep[1] = "http://" + string
                    wallpapersModelArrayList.add(new WallpapersModel(
                            string,///
                            sep[1],
                            "jpg",
                            wallpaperNumber
                    ));
                }
            }catch (Exception e){}
            return null;
    }

全屏Activity

setContentView(R.layout.fullscreen_activity_view);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    viewPager = findViewById(R.id.view_pager);

    arrayList = getIntent().getParcelableArrayListExtra("array");
    final int pos = getIntent().getIntExtra("position", 0);
    fullScreenSwipeAdapter = new 
    FullScreenSwipeAdapter(FullWallpaperViewActivity.this, arrayList);

    viewPager.setAdapter(fullScreenSwipeAdapter);
    viewPager.setCurrentItem(pos);
    viewPager.setOnTouchListener(new View.OnTouchListener(){}
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {}

ViewPager 适配器

public FullScreenSwipeAdapter(Activity activity, ArrayList<WallpapersModel> arrayList) {
    this.activity = activity;
    this.arrayList = arrayList;
    Fresco.initialize(activity);
}

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

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == (object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = layoutInflater.inflate(R.layout.fullwallpaper_view_layout, container, false);

    draweeView = v.findViewById(R.id.full_image_view);
    draweeView.setImageURI(arrayList.get(position).getWallpaperFullURL());

    container.addView(v);

    return v;


}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((CoordinatorLayout)object);
}

将新项添加到 ArrayList 后,只需使用 -

将更改通知您的适配器
arrayAdapter.notifyDataSetChanged();

首先创建一个 ArrayAdapter 的新实例并像这样设置所有新项目:

public class Activity() extends AppCompatActivity {

CustomArrayAdapter mArrayAdapter;
ViewPager mViewPager;

public void updateViewpager(){
    mArrayAdapter = new ArrayAdapter(context, itemList); // old items with new items
    mViewPager.setAdapter(mArrayAdapter);
    mArrayAdapter.notifyDataSetChanged();
}

...

希望对您有所帮助,