动态更新Viewpager数据

时间:2017-10-05 17:45:04

标签: java android android-fragments android-viewpager baseadapter

我有一个自定义模型类的ArrayList,我使用Home_Fragment

通过我AsyncTask的互联网图片的网址实例化

第一次创建列表时,它有12个项目/网址来自网站。 现在,我将FullscreenView Activity传递给与ViewPager Adapter

相关联的onLoadMoreListener

- &GT;我在Home_Fragment中设置ViewPager Adapter,向ArrayList添加了更多项目,现在我想动态地将这些项目添加到 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; } , 我怎么能做到这一点?

Home_Fragment

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

全屏活动

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

ViewPager适配器

[edcs getClinicalPatientDataWithDictionaryForPatientID:participant.participantNum withStartDate:pastDate withEndDate:currentDate completionBlock:^(NSDictionary *jsonDict) {
    if ([jsonDict[@"status"] isEqualToString:@"200"]) {
        if (jsonDict[@"body"]) {
            NSMutableArray *body = [NSMutableArray  arrayWithObject:jsonDict[@"body"]];
            if (body) {
                // Query all measurements
                for (NSArray *jsonArray in body) {
                    for (NSDictionary *measurementDict in jsonArray) {
                        // Separate Hydration measurements
                        if (![body.description containsString:@"<null>"]) {
                            if (measurementDict) {
                                if ([[measurementDict objectForKey:@"MeasureUnitID"] isEqualToString:@"Cups"]) {
                                    [cupsPastYearTotalArray addObject:measurementDict];
                                }
                            }
                        }
                    }
                }
         completionBlock(cupsPastYearTotalArray);

2 个答案:

答案 0 :(得分:2)

向ArrayList添加新项目后,只需使用 -

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

答案 1 :(得分:0)

首先创建一个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();
}

...

希望它有所帮助,