0

我有我通過互聯網在我的Home_Fragment拍攝的圖像的URL使用AsyncTask更新Viewpager數據動態

第一次列表中創建它有12個項目/ URL的實例我的自定義模型類的ArrayList來自網站。 現在,我通過這些上鍊接到我的ViewPager Adapter

一個FullscreenView Activity - >我已在我的Home_Fragment,增加了更多的項目到ArrayList,現在我想那些添加到我的ViewPager Adapter動態的onLoadMoreListener, 我怎樣才能做到這一點?

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

全屏活動

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

回答

2

添加新項目ArrayList中後,只用通知你的變化適配器 -

arrayAdapter.notifyDataSetChanged(); 
+0

我都試過,但似乎並沒有工作:( –

+0

@YuganshTyagi你可以用這個代碼更新這個問題!謝謝 –

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

... 

希望它能幫助,