1

我正在尝试使用 FragmentPagerAdapter 将 ListFragment 与 ViewPager 一起使用。寻呼机由 ListFragment 的两个实例组成。ViewPager 控件有效并允许我在片段之间滑动,但是两个页面中出现相同的 ListFragment。ViewPager 的 getItem() 方法似乎创建了 ListFragment 的两个不同实例,但它只为两个页面显示它的第二个实例。这是一个代码:

// FragmentPagerAdapter
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {      
    public MyFragmentPagerAdapter(FragmentManager fm)   {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {
        Log.i("Page: ", index + "");
        return PageFragment.newInstance(pages.get(index));
        // pages is an ArrayList<ArrayList<Restaurant>> pages
    }

    @Override
    public int getCount() {
        return numberOfPages;
    }
}


// ListFragment
public class PageFragment extends ListFragment {

    private static ArrayList<Restaurant> restaurantsList;

    public static PageFragment newInstance(ArrayList<Restaurant> restaurantsList) {
        PageFragment pageFragment = new PageFragment();
        pageFragment.setRestaurantsList(restaurantsList);
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {
    View view = inflater.inflate(R.layout.fragment, container, false);      
    setListAdapter(new RestaurantArrayAdapter(getActivity().getBaseContext(), getRestaurantsList()) );
    return view;
    }


    private void setRestaurantsList(ArrayList<Restaurant> list)
    {
        restaurantsList = list;
    }

    private ArrayList<Restaurant> getRestaurantsList()
    {
        return this.restaurantsList;
    }
}
4

1 回答 1

2

好吧,伙计,问题是您已将数组成员声明为“静态”,

private static ArrayList<Restaurant> restaurantsList;

失去静电,你应该没事。

于 2013-01-12T20:49:38.090 回答