我有以下代码尝试使用ViewPager。
问题是图像没有显示,我猜问题是片段没有被访问(或“被叫”)你能找出原因吗?
我在Fragment方法中添加了一个打印件来检查它是否已被访问。
这个ViewPager适配器,getItem
应该返回片段的值,还是有问题?
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public static final String ARG_OBJECT = "Person_List";
private ArrayList<Listitem> personArrayList;
public DemoCollectionPagerAdapter(FragmentManager fm, ArrayList<Listitem> personArrayList)
{
super(fm);
Log.d("s", "adapterview");
this.personArrayList = personArrayList;
}
@Override
public Fragment getItem(int i) {
return DemoObjectFragment.newInstance(this.personArrayList.get(i));
}
@Override
public int getCount() {
return 1;
}
这是代码的其余部分
public class SingleViewActivity extends FragmentActivity {
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
ArrayList<Listitem> personArrayList = new ArrayList<Listitem>();
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
Log.d("s", "singleview");
Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");
personArrayList.add(item);
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), personArrayList);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_PERSON_LIST = "Person_List";
private Listitem person;
public static DemoObjectFragment newInstance(Listitem items) {
DemoObjectFragment f = new DemoObjectFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_PERSON_LIST, items);
f.setArguments(args);
System.out.print("instance1");
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args.containsKey(ARG_PERSON_LIST)) {
this.person = args.getParcelable(ARG_PERSON_LIST);
} else {
this.person = null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.funnyimage);
Bundle args = new Bundle();
this.person = args.getParcelable("Person_List");
System.out.print("here1");
if (person != null) {
Picasso.with(getActivity())
.load(person.url)
.placeholder(R.drawable.logo)
.fit()
.noFade()
.into(imageView);
Log.i("PersonsActivity", String.valueOf(person.url));
}
return rootView;
}
}
答案 0 :(得分:2)
删除这些行
Bundle args = new Bundle();
this.person = args.getParcelable("Person_List");
this.person
将为null
,因为您正试图从空Parcelable
获取Bundle
。
因此,不会输入。
if (person != null) {
您不需要这些行的原因是因为this.person
正确加载onCreate
。您不需要使用onCreateView
分配它。