Logiraptor
Logiraptor

Reputation: 1518

Android support ViewPager is invisible

I've implemented a ViewPager from the android support library in my app, and For some reason it is now invisible. The app layout is nested a bit and I'm sure that is causing some problems. I'll try to condense it to just the meaningful code. My Layout inflation code:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

View v = getLayoutInflater().inflate(R.layout.project_row, null);

TextView header = (TextView) v.findViewById(R.id.rowheader);
header.setText(rowHeader);

ViewPager projectRow = (ViewPager) v.findViewById(R.id.pager);
projectRow.setAdapter(new ProjectPagerAdapter(tiles));

LinePageIndicator lineIndicator = (LinePageIndicator)v.findViewById(R.id.pagerIndicator);
lineIndicator.setViewPager(projectRow);
lineIndicator.setStrokeWidth(7);
lineIndicator.setLineWidth(50);
lineIndicator.setSelectedColor(Color.parseColor("#555555"));
lineIndicator.setUnselectedColor(Color.parseColor("#DCDCDC"));

ll.addView(v);

project_row.xml:

<?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="wrap_content" >

<TextView
    android:id="@+id/rowheader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/SectionHeaderColor" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="280dp" />

<com.viewpagerindicator.LinePageIndicator
    android:id="@+id/pagerIndicator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

The ViewPager works by itself if I use this inflation code:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

View v = getLayoutInflater().inflate(R.layout.pageronlytest, null);
ViewPager vp = (ViewPager)v.findViewById(R.id.pager);
vp.setAdapter(new ProjectPagerAdapter(tiles));

ll.addView(v);

pageronlytest.xml:

<?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="280dp"
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="280dp" />

</LinearLayout>

Upvotes: 2

Views: 1587

Answers (1)

MH.
MH.

Reputation: 45493

In project_row.xml you're not explicitly declaring the orientation of the LinearLayout, meaning it will default to 'horizontal'. Since the first element (TextView) defines a match_parent / fill_parent width, it will effectively push all other elements to the right and off the screen.

That being said, you probably just need to add android:orientation="vertical" to the LinearLayout and you're good to go.

Upvotes: 4

Related Questions