在ViewPager PagerTabStrip位置位置、ViewPager、PagerTabStrip

2023-09-06 09:07:01 作者:又怨

我有以下的code:

 <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>

和一切都被layouted像它应该。

and everything gets layouted like it should.

我现在想的是,在 PagerTabStrip覆盖在ViewPager 。

应该在上面呈现,而不占用任何空间布点。 基本上在ViewPager片段应该开始在0,0像PagerTabStrip在做什么。

It should be rendered on top without taking up any layouting space. Basically the fragments in the ViewPager should start at 0,0 like the PagerTabStrip is doing.

任何帮助AP preciated。谢谢!

Any help appreciated. Thanks!

推荐答案

我终于找到一个办法做到这一点。

I finally found a way to do this.

onAttachedToWindow 的PagerTitleStrip,这种检查的家长是一个ViewPager:

In the onAttachedToWindow of the PagerTitleStrip, this checks for the parent to be a ViewPager:

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

因此​​,这需要去。

Therefore this needs to go.

解决方法:

我解决它现在通过创建自己的自定义PagerTitleStrip / PagerTabStrip。 予除去上述行为并且通过设置样式属性为我PagerTitleStrip它指向ViewPager取而代之。这是在价值定义/ attrs.xml是这样的:

I solved it now by creating my own custom PagerTitleStrip/PagerTabStrip. I removed the above behaviour and replaced it by a styleable attribute for my PagerTitleStrip which points to the ViewPager. This is defined in values/attrs.xml like this:

<declare-styleable name="PagerTitleStripCustom">
    <attr name="parentViewPager" format="reference" />
</declare-styleable>

和阅读PagerTitleStrip构造是这样的:

And read in the PagerTitleStrip constructor like this:

TypedArray types = context.obtainStyledAttributes(attrs,R.styleable.PagerTitleStripCustom, 0, 0);   
pagerID = types.getResourceId(R.styleable.PagerTitleStripCustom_parentViewPager, 0);

onAttachedToWindow 现在我们可以检查mViewPager变量为空,并将其分配给我们的pagerID是这样的:

In the onAttachedToWindow we can now check for the mViewPager variable being null and assign it to our pagerID like this:

@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控制,可随意放置在这样的布局:

Now the PagerTitleStrip/PagerTabStrip control can be freely positioned in the layout like this:

<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>

这是概念只是快速和肮脏的证明。希望这可以帮助别人。

This is just the quick and dirty proof of concept. Hope this helps someone.

 
精彩推荐