4

Please, advice. I can't get ViewPager working. Here is what I'am doing:

1) This is my LinearLayout in which I will put the ViewPager. I'am sure that it's work. I can put any View in it:

<LinearLayout
    android:id="@+id/view_contact_values_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/view_contact_sub_nav"
    android:orientation="vertical"
    android:paddingLeft="10dip" >
</LinearLayout>

2) Next I'm creating the ViewPager in code:

private void createViewPager() {
    ViewPager viewPager = new ViewPager(this);
    DatesPagerAdapter datesPagerAdapter = new DatesPagerAdapter(this);
    viewPager.setAdapter(datesPagerAdapter);
    viewPager.setCurrentItem(1);
    llContainer.addView(viewPager);
}

3) And the ViewPagerAdapter (PagerAdapter):

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = (LayoutInflater) container.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = inflater.inflate(R.layout.dates_carusel_item, null);
    ((ViewPager) container).addView(v, 0);
    return v;
}

@Override
public int getCount(){
    return 5;
}

But when I open my Activity, there is nothing showing in my LinearLayout. P.S.: Sorry for the bad question styling. Code tag in this editor is strange.

Murat Nafiz
  • 1,438
  • 17
  • 28
Ok-Alex
  • 558
  • 2
  • 13
  • 29

1 Answers1

0

Take a class extending Fragemnt

class ExampleFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.dates_carusel_item, null);
  return v;
}

And in your PagerAdapter do this

    private class DatesPagerAdapter extends FragmentPagerAdapter
{
    public DatesPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int arg0) {
        ExampleFragment fragment=new ExampleFragment();
        Bundle bundle=new Bunlde();
        //put bundle data here
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public int getCount() {
        5;
    }
}

Whatever view you want to set do it on the Example Fragment and add your viewPager

ViewPager pager=new ViewPager(this);
pager.setAdapter(new DatesPagerAdapter);
llContainer.addView(pager)

Add your ViewPager in xml itself to get it proprly alligned or else you can setLayoutParams in runtime if its a visibilty error

     android:layout_width="match_parent" android:layout_height="wrap_content"      android:orientation="vertical" android:layout_below="@id/view_contact_sub_nav"android:id="+id/view_contact_values_container"
android:paddingLeft="10dip">
<android.support.v4.view.ViewPager
    android:id="@+id/imagesgallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>
sheetal
  • 3,014
  • 2
  • 31
  • 45
  • what is "_Vo.get_quizList().size();"?? – Ok-Alex Dec 05 '12 at 10:32
  • Sorry, I can't understand. How to add fragments to the adapter? – Ok-Alex Dec 05 '12 at 10:37
  • 1
    you have created your fragment??? Inflate and return a view as above..Copy the above DatesPagerAdpater class and just set this adapter to your ViewPager which again you can add in XML rather adding runtime – sheetal Dec 05 '12 at 10:43