MainActivity(extends fragmentActivity)有listview,如果我需要在ViewPager中生成5个片段,如何将getFragmentManager()传递给适配器,则listview调用listviewadapter,其中ViewPager现在有ViewPagerAdapter。
MainActivity [fragmentActivity] ----> Listview ----> ListviewAdapter [带有getview布局ViewPager的自定义BaseAdapterAdapter] ---> ViewPager ---> ViewPageAdapter [我需要根据位置创建5个片段吗?]
如果我粘贴整个代码就太长了,所以我只附加了
public class ViewPagerAdapter extends PagerAdapter {
ArrayList<ModelClass> arrayModelClasses = new ArrayList<ModelClass>();
Context mcontext ;
LinearLayout layouttest;
final int PAGE_COUNT = 5;
private String titles[] = new String[] { "Villa ","", "" ,"","",""};
private int[] imageResId = { R.drawable.transparant,R.drawable.ic_action_place, R.drawable.icon_3d1,
R.drawable.icon_flooplan,R.drawable.icon_gallery,R.drawable.icon_location };
@SuppressLint("NewApi")
@Override
public void finishUpdate(ViewGroup container) {
// TODO Auto-generated method stub
super.finishUpdate(container);
}
public ViewPagerAdapter(ArrayList<ModelClass> arrayModelClasses , Context context) {
super();
this.arrayModelClasses = arrayModelClasses;
this.mcontext= context;
}
@Override
public boolean isViewFromObject(View collection, Object object) {
return collection == ((View) object);
}
View view;
@Override
public Object instantiateItem(View collection, int position) {
if (position == 0) {
//need to call fragment 1
}if (position == 1) {
//need to call fragment 2
}if (position == 2) {
//need to call fragment 3
}if (position == 3) {
//need to call fragment 4
}if (position == 4) {
//need to call fragment 5
}
return view;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public CharSequence getPageTitle(int position) {
Drawable image = mcontext.getResources().getDrawable(imageResId[position]);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableString sb = new SpannableString(" " + titles[position] );
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
link但它使用FragmentPagerAdapter进行viewPager并从FragmentActivity传递getSupportFragmentManager,但是不能用2个适配器帮助。
问题:如何在listview内部的viewPager中创建片段。抱歉,如果它令人困惑。
谢谢,
答案 0 :(得分:0)
考虑到您没有发布ListView
适配器,我假设您延长BaseAdapter
(无论哪种方式都相似)。在getView(...)
方法上实例化一个新的ViewPagerAdapter
并将其设置为列表项xml中定义的PagerAdapter
。
注意:在ViewPager
内部ListView
(特别是在加载项目时)会导致性能下降。
答案 1 :(得分:0)
我在ViewPager中附加了片段,代码如下
公共类IntroslideActivity扩展了FragmentActivity {
private static int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introslide);
mPager = (ViewPager) findViewById(R.id.pager);
/ ** *一个简单的寻呼机适配器,代表5个{@link ScreenSlidePageFragment}对象 * 序列。 * / 私有类ScreenSlidePagerAdapter扩展了FragmentPagerAdapter {
protected final int[] ICONS = new int[] {
R.drawable.ic_message,
R.drawable.ic_phone,
R.drawable.ic_message,
R.drawable.ic_message,
R.drawable.ic_message
};
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment frm = null;
System.out.println("scrolling position"+position);
if(position==0){
frm=ScreenSlidePageFragment.create(position);
}
else if(position==1){
frm=Screenslidetwo.create(position);
}
else if(position==2){
frm=Screenslidethree.create(position);
}
else if(position==3){
frm=Screenslidefour.create(position);
}
else if(position==4){
frm=Screenslidefive.create(position);
}
return frm;
}
@Override
public int getCount() {
return NUM_PAGES;
}
public void setCount(int count) {
if (count > 0 && count < 5) {
NUM_PAGES = count;
notifyDataSetChanged();
}
}
}
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
} }