ViewPager中的PagerTabStrip位置

ppx*_*ppx 8 android android-layout android-viewpager

我有以下代码:

 <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>
Run Code Online (Sandbox Code Playgroud)

一切都像它应该的那样得到安排.

我现在想要的是PagerTabStrip覆盖了ViewPager.

它应该在顶部呈现而不占用任何布局空间.基本上,ViewPager中的片段应该像PagerTabStrip一样从0开始.

任何帮助赞赏.谢谢!

ppx*_*ppx 8

我终于找到了办法.

onAttachedToWindowPagerTitleStrip中,这将检查父项是ViewPager:

final ViewParent parent = getParent();
 if (!(parent instanceof ViewPager)) {
  throw new IllegalStateException("PagerTitleStrip must be a direct child of a ViewPager.");
}   
Run Code Online (Sandbox Code Playgroud)

因此,这需要去.

解:

我现在通过创建自己的自定义PagerTitleStrip/PagerTabStrip来解决它.我删除了上述行为,并将其替换为我的PagerTitleStrip的可设置属性,该属性指向ViewPager.这在values/attrs.xml中定义,如下所示:

<declare-styleable name="PagerTitleStripCustom">
    <attr name="parentViewPager" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

并在PagerTitleStrip构造函数中读取如下:

TypedArray types = context.obtainStyledAttributes(attrs,R.styleable.PagerTitleStripCustom, 0, 0);   
pagerID = types.getResourceId(R.styleable.PagerTitleStripCustom_parentViewPager, 0);
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

现在,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>
Run Code Online (Sandbox Code Playgroud)

这只是快速而肮脏的概念证明.希望这有助于某人.