这是我第一次尝试实现ViewPager
. 我的应用程序基本上有一个列表和详细信息页面,我希望用户能够在详细信息之间滑动。详细信息基于我从列表页面传入的 ID。问题是我正在调用getActivity()
详细信息片段,该片段返回为空。就像我说的,我是新手,ViewPager
所以这可能很明显:
清单片段.class:
public class ListingFragment extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
public int Id = 0;
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
DetailsFragment df1 = new DetailsFragment();
df1.Id = 1;
df1.DisplayItems();
fragments.add(lf);
DetailsFragment df2 = new DetailsFragment();
df2.Id = 2;
df2.DisplayItems();
fragments.add(lf);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
return fragments.get(index);
}
@Override
public int getCount() {
return 2;
}
}
}
DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
private void DisplayItems()
{
PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
...
}
}
更新
我已经更新了我的代码以使用静态newInstance
方法和 aBundle
将 id 传递给详细信息片段。
清单片段.class:
public class ListingFragment extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
public int Id = 0;
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
DetailsFragment df1 = DetailsFragment.newInstance(1);
fragments.add(df1);
DetailsFragment df2 = DetailsFragment.newInstance(2);
fragments.add(df2);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
return fragments.get(index);
}
@Override
public int getCount() {
return 2;
}
}
}
DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
private int detailId;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
public static DetailsFragment newInstance(int id) {
DetailsFragment lf = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
lf.setArguments(bundle);
return lf;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listing, container, false);
detailId = getArguments().getInt("id");
DisplayItems();
return view;
}
private void DisplayItems()
{
PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
...
}
}