0

这是我第一次尝试实现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);
        ...
    }
}
4

1 回答 1

0

您不应该设置变量和调用与此类活动初始化有关的方法。Id应该使用静态方法设置setArguments(...)并使用静态newInstance(...)方法来实例化您的片段。您应该displayItems()onCreateView(...)Fragment 内部调用,而不是从附加的 Activity 调用。

有关更多信息,请参阅有关 Fragments 的官方 Android 指南。您绝对应该阅读整个内容。 http://developer.android.com/guide/components/fragments.html

于 2013-01-16T19:16:41.903 回答