gpt4 book ai didi

java - 动态更新 Viewpager 数据

转载 作者:行者123 更新时间:2023-11-29 19:07:38 32 4
gpt4 key购买 nike

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

在我的 Home_Fragment 中通过从 Internet 获取的图像的 URL 对其进行实例化

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

FullscreenView Activity

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

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

最佳答案

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

...

希望对你有帮助

关于java - 动态更新 Viewpager 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591816/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com