我有以下代码:
<android.support.v4.view.ViewPager
android:id="@+id/main_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.view.ViewPager>
并且所有内容都应该像它应该的那样布局。
我现在想要的是 PagerTabStrip覆盖ViewPager 。
它应该在顶部呈现而不占用任何布局空间。 基本上,ViewPager中的片段应该像PagerTabStrip一样从0开始。
任何帮助表示赞赏。谢谢!
答案 0 :(得分:8)
我终于找到了办法。
在PagerTitleStrip的onAttachedToWindow
中,这会检查父级是ViewPager:
final ViewParent parent = getParent();
if (!(parent instanceof ViewPager)) {
throw new IllegalStateException("PagerTitleStrip must be a direct child of a ViewPager.");
}
因此需要这样做。
解决方案:
我现在通过创建自己的自定义PagerTitleStrip / PagerTabStrip来解决它。 我删除了上述行为,并将其替换为我的PagerTitleStrip的可设置属性,该属性指向ViewPager。这在values / attrs.xml中定义,如下所示:
<declare-styleable name="PagerTitleStripCustom">
<attr name="parentViewPager" format="reference" />
</declare-styleable>
并在PagerTitleStrip构造函数中读取如下:
TypedArray types = context.obtainStyledAttributes(attrs,R.styleable.PagerTitleStripCustom, 0, 0);
pagerID = types.getResourceId(R.styleable.PagerTitleStripCustom_parentViewPager, 0);
在onAttachedToWindow
中,我们现在可以检查mViewPager变量为null并将其分配给我们的pagerID,如下所示:
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
final ViewParent parent = getParent();
ViewPager pager = null;
if (pagerID != 0) {
View test2 = ((View) parent).findViewById(pagerID);
pager = (ViewPager) test2;
} else if (pagerID == 0 && (parent instanceof ViewPager)) {
pager = (ViewPager) parent;
} else if (pagerID == 0 && !(parent instanceof ViewPager)) {
throw new IllegalStateException("PagerTitleStrip is neither a direct child of a ViewPager nor did you assign the ID of the ViewPager.");
} else {
throw new IllegalStateException("PagerTitleStrip: Something went completely wrong.");
}
final PagerAdapter adapter = pager.getAdapter();
pager.setInternalPageChangeListener(mPageListener);
pager.setOnAdapterChangeListener(mPageListener);
mPager = pager;
updateAdapter(mWatchingAdapter != null ? mWatchingAdapter.get() : null, adapter);
}
现在,PagerTitleStrip / PagerTabStrip控件可以在布局中自由定位,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.YOUR.PATH"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/main_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v4.view.PagerTabStripCustom
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:parentViewPager="@id/main_pager" />
</RelativeLayout>
这只是快速而肮脏的概念证明。希望这有助于某人。
答案 1 :(得分:-1)
<RelativeLayout android:id="@+id/view_pager_container
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/main_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.view.ViewPager>
</RelativeLayout>
使用RelativeLayout时,如果在添加视图时未指定视图的位置,则只需在RelativeLayout的左上角(或位置0,0)中将视图添加到彼此的顶部