简体   繁体   中英

Update Viewpager data dynamically

I have an ArrayList of my custom model class which I am instantiating by URL of images taken from Internet in my Home_Fragment using an AsyncTask

The first time the List is created it has 12 items/URL's from the website. Now, I am passing these on a FullscreenView Activity that is linked to my ViewPager Adapter

-> I have set an onLoadMoreListener in my Home_Fragment that adds more items to the ArrayList, now I want to add those to my ViewPager Adapter dynamically, How can I accomplish this?

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

Fullscreen 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 Adapter

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

First make a new instance of your ArrayAdapter and set all the new items like this:

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

...

Hope it helps,

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