0

MainActivity(extends fragmentActivity) has listview which calls listviewadapter within which have a ViewPager with ViewpagerAdapter now if i need to generate 5 fragments within ViewPager how to pass getFragmentManager() to adapter.

MainActivity [fragmentActivity] ----> Listview ----> ListviewAdapter[Custom BaseAdapterAdapter with getview of layout ViewPager] ---> ViewPager ---> ViewPageAdapter[i need to create 5 fragments based on position?]

It will be too long if i paste the whole code so i have attached only

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 but it uses FragmentPagerAdapter for viewPager and passed the getSupportFragmentManager from FragmentActivity but its not possible with 2 adapters so help.

Question: How to create fragment within viewPager which is inside listview. sorry if its confusing.

Thanks,

Community
  • 1
  • 1
Anitha
  • 253
  • 4
  • 16
  • If you need to create based on position then you need to maintain a arraylist of fragments for particular position – Piyush Mar 27 '15 at 13:18

2 Answers2

0

Considering you did not post your ListView Adapter I'll assume you extend BaseAdapter (either way it'll be similar). On the getView(...) method instantiate a new ViewPagerAdapter and set it to the PagerAdapter defined in the list item xml.

Note: having a ViewPager inside the ListView (and specially while loading the items that way) could cause low performance.

Filipe Silva
  • 220
  • 4
  • 10
  • yes i have done tat but how to pass getFramentManager() inside ViewPagerAdapter to instantiate fragments? – Anitha Mar 27 '15 at 13:02
  • Oh. You could create a local variable that you would set on your ListView Adapter when you instantiate it and then pass it on to the ViewPagerAdapter once you instantiate them. Would look like `adapter = new MyListViewAdapter(fragmentManager);` and then the same for the ViewPagerAdapter. @Anitha – Filipe Silva Mar 27 '15 at 13:09
  • but what type of variable will be used to hold getSupportFragmentManager() within listviewadatper without extending it to fragmentadatper. – Anitha Mar 28 '15 at 06:40
  • that is solved but i still have new issue... pls look into it if you could solvehttp://stackoverflow.com/questions/29315705/listview-viewpager-doesnt-set-fragment-android – Anitha Mar 28 '15 at 10:05
0

I've attached fragments inside ViewPager with the code as follows

public class IntroslideActivity extends 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);

/** * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in * sequence. */ private class ScreenSlidePagerAdapter extends 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);

} }

Karthika PB
  • 1,373
  • 9
  • 16
  • sorry @karthika that wont help i want to pass getSupportFragmentManager() from listview[baseadapter] adapter to viewpageradapter[PagerAdapter]. your example shows calling viewpagerAdapter within fragmentActivity. – Anitha Mar 28 '15 at 06:36