1

I am using Xamarin and am having some code trouble in relation to my ViewPager not displaying my Views.

Here is my code:

MainActivity:

public class MainActivity : FragmentActivity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.activity_main);

        var pager = FindViewById<ViewPager>(Resource.Id.viewPager);
        pager.Adapter = new MyPagerAdapter(SupportFragmentManager);
    }
}

MyPagerAdapter:

public class MyPagerAdapter : FragmentPagerAdapter 
{
    int count;

    public override int Count 
    {
        get 
        {
            return count;   
        }   
    }

    public override Android.Support.V4.App.Fragment GetItem (int position)
    {

        switch(position) 
        {
        case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
        case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
        case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
        case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
        case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
        default: return ThirdFragment.newInstance("ThirdFragment, Default");
        }       
    }

    public MyPagerAdapter (Android.Support.V4.App.FragmentManager fm) : base (fm)
    {
        count = 5;
    }     
}

FirstFragment:

public class FirstFragment : Android.Support.V4.App.Fragment
{
    string text;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View v = inflater.Inflate(Resource.Layout.first_frag, container, false);

        TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragFirst);
        tv.Text = Arguments.GetString("msg");

        return v;
    }

    public static FirstFragment newInstance(String text) 
    {

        FirstFragment f = new FirstFragment();
        Bundle b = new Bundle();
        b.PutString("msg", text);

        f.Arguments = (b);

        return f;
    }
}

SecondFragment:

public class SecondFragment : Android.Support.V4.App.Fragment
{
    string text;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.Inflate(Resource.Layout.second_frag, container, false);

        TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragSecond);
        tv.Text = Arguments.GetString("msg");

        return v;
    }

    public static SecondFragment newInstance(String text) 
    {

        SecondFragment f = new SecondFragment();
        Bundle b = new Bundle();
        b.PutString("msg", text);

        f.Arguments = (b);

        return f;
    }
}

ThirdFragment:

public class ThirdFragment : Android.Support.V4.App.Fragment
{
    string text;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.Inflate(Resource.Layout.third_frag, container, false);

        TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragThird);
        tv.Text = Arguments.GetString("msg");

        return v;
    }

    public static ThirdFragment newInstance(String text) 
    {

        ThirdFragment f = new ThirdFragment();
        Bundle b = new Bundle();
        b.PutString("msg", text);

        f.Arguments = (b);

        return f;
    }
}

EDIT

Here is my activity_main.axml layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3F3F4"
android:orientation="vertical">
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="9dp"
    android:paddingRight="9dp" />
</LinearLayout>

When the application loads up, nothing is displayed. No errors are occuring.

Can I please have some help to get the views displaying?

Thanks in advance

EDIT2

Here are the fragment layouts:

first_frag.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark">
    <TextView
        android:id="@+id/tvFragFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

second_frag.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark">
    <TextView
        android:id="@+id/tvFragSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

third_frag.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light">
    <TextView
        android:id="@+id/tvFragThird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

Also, I have got this code from the following link: How to implement a ViewPager with different Fragments / Layouts

Thanks in advance

4
  • Can you put your layout code with viewpager?
    – choper
    Mar 19, 2014 at 13:17
  • @choper: Can you have a look at my code?
    – Garry
    Mar 22, 2014 at 1:47
  • Show the layouts of your fragments. Mar 24, 2014 at 10:24
  • Please have a look at my edit2
    – Garry
    Mar 25, 2014 at 6:07

1 Answer 1

1

Change the android:layout_height to some other value instead 0dp. As the height is 0 you was not able to see anything.

5
  • I have changed the layout code, but no views are displaying. What else should I do?
    – Garry
    Mar 24, 2014 at 8:13
  • How you have changed it?
    – choper
    Mar 24, 2014 at 12:37
  • I removed the android:layout_height value.
    – Garry
    Mar 25, 2014 at 6:07
  • I haven't written that you should remove it, I wrote that you should change the value
    – choper
    Mar 25, 2014 at 6:10
  • I decided to make it generic, and remove it. The above code should work.
    – Garry
    Mar 25, 2014 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.