在 ViewPager android 中创建片段
create fragment inside ViewPager android
MainActivity(extends fragmentActivity) 有一个调用 listviewadapter 的 listview,如果我需要在 ViewPager 中生成 5 个片段,如何将 getFragmentManager() 传递给适配器。
MainActivity [fragmentActivity] ----> Listview ----> ListviewAdapter[Custom BaseAdapterAdapter with getview of layout ViewPager] ---> 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 中创建片段。对不起,如果它混乱。
谢谢,
考虑到您没有 post 您的 ListView
适配器,我假设您扩展了 BaseAdapter
(无论哪种方式都是相似的)。在 getView(...)
方法上实例化一个新的 ViewPagerAdapter
并将其设置为列表项 xml.
中定义的 PagerAdapter
注意: 在 ListView
中有一个 ViewPager
(特别是在以这种方式加载项目时)可能会导致性能下降。
我在 ViewPager 中附加了片段,代码如下
public class 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} 对象,在
* 顺序。
*/
私有 class 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);
}
}
MainActivity(extends fragmentActivity) 有一个调用 listviewadapter 的 listview,如果我需要在 ViewPager 中生成 5 个片段,如何将 getFragmentManager() 传递给适配器。
MainActivity [fragmentActivity] ----> Listview ----> ListviewAdapter[Custom BaseAdapterAdapter with getview of layout ViewPager] ---> 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 中创建片段。对不起,如果它混乱。
谢谢,
考虑到您没有 post 您的 ListView
适配器,我假设您扩展了 BaseAdapter
(无论哪种方式都是相似的)。在 getView(...)
方法上实例化一个新的 ViewPagerAdapter
并将其设置为列表项 xml.
PagerAdapter
注意: 在 ListView
中有一个 ViewPager
(特别是在以这种方式加载项目时)可能会导致性能下降。
我在 ViewPager 中附加了片段,代码如下
public class 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} 对象,在 * 顺序。 */ 私有 class 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);
} }