我尝试获得具有 3 个不同页面的 ViewPager。所有三个页面都有一个带有图片和文本的 GridView。我正在以 Json 格式从服务器获取数据。
问题是......如果我从 GridView 中的服务器 ViewPager 页面获取数据是空白的。现在我正在使用 Fragment,但它不起作用。
TestFragment 只是一个没有服务器数据的简单布局(只是为了测试 ViewPager 是否工作)......这个页面工作。PersonenFragment 应该显示服务器数据,但它没有。由于显示 GridView 中的数据运行良好,没有 ViewPager 问题不在“PersonsListAdapter”的代码中。
我认为问题在于,在 GridView 填充来自 asyncTask 的数据之前,ViewPager 显示了页面。因为这需要一些时间。我需要做什么 ?
public class PersonenFragmentActivity extends FragmentActivity{
private MyAdapter mAdapter;
private ViewPager mPager;
public Activity act = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personen_view_pager);
act= this;
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
case 1:
return new PersonenFragment("ProfessorInnen", act);
default:
return null;
}
}
}
}
人物片段
public class PersonenFragment extends Fragment {
private int mCurrentPage;
private String personenKategorie;
private Activity activity;
private View view;
private GridView gv ;
public PersonenFragment(String personenKategorie, Activity activity) {
this.personenKategorie = personenKategorie;
this.activity = activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//readWebPage contains the asyncTask which downloads dataFromServer and returns an ArrayList with PersonObjects with the data in it
DownloadPersonen download = new DownloadPersonen();
download.readWebpage(this.personenKategorie);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//layout.Personen is the Layout with the Grid
gv = (GridView) inflater.inflate(R.layout.personen, container, false);
return gv;
}
在从 AsyncTask DownloadPersonen 执行 postExecute
@Override
protected void onPostExecute(ArrayList<Person> personen) {
GridView grid= (GridView) ll.findViewById(R.id.personen_grid_view);
PersonsListAdapter p = new PersonsListAdapter(activity,personen);
grid.setAdapter(p);
}