我已经在我的应用程序中实现了来自 android 支持库的 ViewPager,并且由于某种原因它现在是不可见的。应用程序布局有点嵌套,我确信这会导致一些问题。我将尝试将其压缩为有意义的代码。我的布局膨胀代码:
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);
项目行.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>
如果我使用这个膨胀代码,ViewPager 会自行工作:
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>