如何在每次实例化片段时创建一个新的ViewPager(再次创建它)? (寻呼机在片段破坏中幸存下来。)

时间:2012-06-12 20:22:34

标签: android fragment android-viewpager destroy

每次删除片段(再次创建)时如何创建新的ViewPager?我的寻呼机在片段破坏中幸存下来。

我在Fragment中有一个ViewPager,但是当我从FrameLayout中移除片段并将其添加回来时。他的ViewPager与旧的片段实例相同(我确实认为是这样)但所有片段都消失了(空的寻呼机是空的:()。我怎样才能重新创建ViewPager?

此代码位于我的片段上的onCreateView()方法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragment = inflater.inflate(getLayoutID(), container,false);
    ViewPager pager = (ViewPager) fragment.findViewById(R.id.myPager);

    PagerAdapter newAdapter = new MyPagerAdapter(getFragmentManager(), getFragments());
    pager.setAdapter(newAdapter);

    return fragment;
}

而且我认为这是你们所有人都需要知道的。

1 个答案:

答案 0 :(得分:2)

现在知道我不能在片段中包含片段,我使用了一个可以传递的视图数组(可能是你想要的任何东西)并使用它来显示这个ViewPagerAdapter。代码如下:

<强> ViewPagerAdapter

import java.util.List;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

public class ViewPagerAdapter extends PagerAdapter{

    private List<View> pages;

    public ViewPagerAdapter(List<View> pages) {
        this.pages = pages;
    }



    @Override  
    public Object instantiateItem(View collection, int position) {  
        ((ViewPager) collection).addView(pages.get(position),0);  

        return pages.get(position);  
    }  

    @Override
    public int getCount() {
        return pages.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    /**  
     * Remove a page for the given position. The adapter is responsible  
     * for removing the view from its container, although it only must ensure  
     * this is done by the time it returns from {@link #finishUpdate()}.  
     *  
     * @param container The containing View from which the page will be removed.  
     * @param position The page position to be removed.  
     * @param object The same object that was returned by  
     * {@link #instantiateItem(View, int)}.  
     */  
    @Override  
    public void destroyItem(View collection, int position, Object view) {  
        ((ViewPager) collection).removeView((View) view);  
    }  

    @Override  
    public boolean isViewFromObject(View view, Object object) {  
        return view==((View)object);  
    }  

    /**  
     * Called when the a change in the shown pages has been completed. At this  
     * point you must ensure that all of the pages have actually been added or  
     * removed from the container as appropriate.  
     * @param container The containing View which is displaying this adapter's  
     * page views.  
     */  
    @Override  
    public void finishUpdate(View arg0) {}  

    @Override  
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}  

    @Override  
    public Parcelable saveState() {  
        return null;  
    }  

    @Override  
    public void startUpdate(View arg0) {}  
}  

这里有一个额外的抽象片段,你可以覆盖抽象方法,然后瞧瞧:

<强> AbstractPagerFragment

import java.util.List;

import templateDigital.util.ViewPagerAdapter;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class AbstractPagerFragment extends Fragment {

    private ViewPager pager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View fragment = inflater.inflate(getLayoutID(), container,false);
        setRetainInstance(false);
        PagerAdapter mPagerAdapter = new ViewPagerAdapter(getViews());
        pager = (ViewPager) fragment.findViewById(getPagerID());
        pager.setAdapter(mPagerAdapter);
        pager.getAdapter().notifyDataSetChanged();

        return fragment;
    }


    public abstract List<View> getViews();

    public abstract int getLayoutID();

    public abstract int getPagerID();

    @Override
    public void onDestroy() {
        super.onDestroy();
        pager = null;
        System.gc();
    }


}