gpt4 book ai didi

java - 使用可滑动的选项卡更改单个 fragment 中的数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:07 25 4
gpt4 key购买 nike

我想在 Fragment 中滑动 ViewPager 时更改数据。

Tabview的数据来自于JSON。它给出id,根据该id,将检索数据。但是当加载 mainActivity 时,它会同时给出前两个位置。

ActivityMain.java

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String serverUrl = DataService.category;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverUrl);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
System.out.println("the response text is " + responseText);
response_text = new JSONObject(responseText);
JSONArray jarray=response_text.getJSONArray("posts");
for(int k=0;k<jarray.length();k++)
{
JSONObject jk=jarray.getJSONObject(k);
adapter.addFragment(new TopNewsFragment(),jk.getString("categoryName"),jk.getString("c_id"));
arraylist.add(jk.getString("c_id"));
}
fetchDate();
viewPager.setAdapter(adapter);
}

这是我的 ViewPager Adapter 类。

public class ViewPagerAdapter extends FragmentPagerAdapter
{

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}


@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title,String category)
{
mFragmentList.add(fragment);
Bundle b=new Bundle();
b.putString("category_id",category);
b.putString("title",title);
fragment.setArguments(b);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

它也不显示第一个和最后一个位置。提前致谢。

最佳答案

使用FragmentStatePagerAdapter

检查此链接您可能会得到答案:click here

关于java - 使用可滑动的选项卡更改单个 fragment 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35552180/

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