现在知道我不能在片段中包含片段,我使用 ViewPagerAdapter 进行了这个 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();
}
}