我有以下代码:
<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中的片段应该从0,0开始,就像PagerTabStrip正在做的那样。
任何帮助都很感激。谢谢!
发布于 2013-11-26 16:39:16
我终于找到了办法。
在onAttachedToWindow of PagerTitleStrip中,这将检查父级是否为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的一个可样式属性。以下是在value/atts.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>这只是概念的快速而肮脏的证明。希望这能帮上忙。
发布于 2013-01-29 18:14:59
<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)的顶部。
https://stackoverflow.com/questions/14586690
复制相似问题