我正在阅读有关片段的View Paging的Android开发人员指南,并坚持从我的主onCreate方法填充片段。我觉得我很困惑在填充片段时调用哪些方法,因为我得到空值。
My Fragment Class
public class CatalogFragment extends Fragment {
private String filename;
private String author;
private String title;
public CatalogFragment(String f, String t, String a){
filename = f;
author = a;
title = t;
Log.d("frag", f+" | "+t+" | "+a);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.catalog_fragment, container, false);
ImageView imgView = (ImageView) rootView.findViewById(R.id.imgFrag);
Bitmap bmp = BitmapFactory.decodeFile(filename);
imgView.setImageBitmap(bmp);
TextView txtView = (TextView)rootView.findViewById(R.id.title_text);
txtView.setText(author+": "+title);
return rootView;
}
}
我的onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog_view);
//Instantiate a ViewPager and a PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//populate the fragment
//I was thinking I could pass the fragment class the necessary information to put an imageview and text view in place.
List<Poster> posters = db.getAllPosters();
for (Poster p : posters) {
final String filename = p.getPosterFilename();
//Create a new Fragment?
new CatalogFragment(filename, p.getPosterTitle(), p.getPresenterFname()+" "+p.getPresenterLname());
}
}
然而,似乎getItem实际上被调用来填充片段。我认为。如果我把null放入我得到的参数中。如果我试图省略这一行,eclipse开始抱怨。所以我被卡住了。
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position){
return new CatalogFragment(null, null, null);
}
@Override
public int getCount(){
return NUM_PAGES;
}
}
答案 0 :(得分:1)
适配器是在视图寻呼机中创建片段的原因,您无需自己创建它们。
以下是Adapter和Activity#onCreate可能为您提供正确结果的内容。
注意我们如何告诉适配器从哪里获取数据。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog_view);
//Instantiate a ViewPager and a PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPagetAdapter.setData(db.getAllPosters()); // Provide Poster Data to Adapter
mPager.setAdapter(mPagerAdapter);
}
在适配器中,我们告诉它如何构建它的片段:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
List<Poster> posterList = new ArrayList<Poster>(0);
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public void setData(List<Poster> posters) {
posterList.addAll(posters);
}
@Override
public Fragment getItem(int position){
Poster poster = posterList.get(position)
// FRAGMENTS SHOULD NOT IMPLEMENT A CONTRUCTOR!!!
// This will lead to other issue down the line.
return new CatalogFragment(poster.getPosterFilename(), poster.getPosterTitle(), poster.getPresenterFname()+" "+poster.getPresenterLname());
}
@Override
public int getCount(){
return posterList.size();
}
}
```
作为旁注,你不应该有一个带参数的片段构造函数。请参阅此其他SO问题以获得解释:Do fragments really need an empty constructor?