简体   繁体   English

不要剪辑ViewPager页面

[英]Do not clip ViewPager pages

I can't get my ViewPager not clipping its pages contents. 我不能让我的ViewPager不剪切其页面内容。

I'm currently using ViewPager with a FragmentStatePagerAdapter in my application. 我目前正在我的应用程序中使用带有FragmentStatePagerAdapter ViewPager I have 3 pages, each page is showing a different fragment. 我有3页,每页都显示不同的片段。 Everything works fine, but now I want to be able to draw outside of my pages bounds, like in this image: 一切正常,但现在我希望能够在我的页面边界之外绘制,如下图所示:

我想要的是

I set clipChildren and clipToPadding to false in my all views parents to the views that needs to draw outside their bounds, but here's what I get : my views get clipped according to pages bounds. 我在我的所有视图中将clipChildrenclipToPadding设置为false ,以及需要在其边界之外绘制的视图,但这是我得到的:我的视图根据页面边界被剪切。

我得到了什么

Some additional info : In order to fix that issue, I used hierarchyviewer to check my view hierarchy and see if some "under the hood" views could have a clipping behaviour. 一些额外的信息:为了解决这个问题,我使用了hierarchyviewer来检查我的视图层次结构,看看是否有一些“幕后”视图可能有剪切行为。 And bingo : each of my fragments' root views is added under a NoSaveStateFrameLayout . 和bingo:我的每个片段的根视图都添加在NoSaveStateFrameLayout I suspect this guy to clip its children... 我怀疑这个家伙要夹住孩子......

Is my last assumption correct in your opinion? 你认为我的最后一个假设是否正确? How would you do to get unclipped pages? 如何获取未剪辑的页面?

There are a few different Views that require disabled clipping. 有一些不同的视图需要禁用剪辑。 First, the FrameLayout that is at the root of the ViewPager needs to have clipping disabled. 首先,位于ViewPager根目录的FrameLayout需要禁用剪辑。 You can do this wherever you initialize your view pager. 您可以在初始化视图寻呼机的任何地方执行此操作。 You also need to set the off-screen page limit to 2 to allow for an extra fragment to be loaded during a swipe. 您还需要将屏幕外页面限制设置为2,以允许在滑动期间加载额外的片段。 Without this, you'll see a pop going from 1->2 as 3 loads and adds it's overflow content to 2 once 2 has finished loading (this loads both 2 and 3 ahead of time). 如果没有这个,你会看到一个弹出从1-> 2作为3次加载,并在2加载完成后将其溢出内容添加到2(这会提前加载2和3)。

mViewPager.setClipChildren(false);
mViewPager.setClipToPadding(false);
mViewPager.setOffscreenPageLimit(2);

Second, the XML you inflate in your FragmentStatePagerAdapter needs to include the appropriate tags or it can be done programmatically. 其次,您在FragmentStatePagerAdapter中膨胀的XML需要包含相应的标记,或者可以通过编程方式完成。 Last, you need to force the NoSaveStateFrameLayout to not clip as well. 最后,您需要强制NoSaveStateFrameLayout不要剪辑。 This can be done after the view has been created in onViewCreated. 这可以在onViewCreated中创建视图后完成。 Here's an example fragment: 这是一个示例片段:

public static class PlaceholderFragment extends Fragment {

    public static PlaceholderFragment newInstance() {
        PlaceholderFragment fragment = new PlaceholderFragment();
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false);
        //This can be done in XML
        rootView.setClipChildren(false);
        rootView.setClipToPadding(false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //This is the NoSaveStateFrameLayout - force it to not clip
        FrameLayout frameLayout = (FrameLayout) getView();
        frameLayout.setClipChildren(false);
        frameLayout.setClipToPadding(false);
    }
}

I have tested this solution and am certain it works. 我已经测试了这个解决方案并确定它有效。

Full activity example: https://gist.github.com/grantamos/d664f01a30f9ad97ba53 完整活动示例: https//gist.github.com/grantamos/d664f01a30f9ad97ba53

I had the same problem. 我有同样的问题。 I fixed this by adding 我通过添加修复此问题

android:clipChildren="false"
android:clipToPadding="false"

to ViewPager and ViewGroup which is parent of ViewPager ViewPagerViewPagerViewPager ViewGroup

For instance, if you have a layout like that: 例如,如果你有这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:clipChildren="false"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.ViewPager
                android:clipChildren="false"
                android:clipToPadding="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </FrameLayout>
</RelativeLayout>

then only LinearLayout and ViewPager should have 然后只有LinearLayoutViewPager应该有

android:clipChildren="false"
android:clipToPadding="false"

Have you tried to use a negative value for the margin between pages (using ViewPager.setPageMargin(int) )? 您是否尝试在页面之间使用ViewPager.setPageMargin(int)使用ViewPager.setPageMargin(int) )? You will also have to set offscreen page limit to at least 2 (using ViewPager.setOffscreenPageLimit(int) ). 您还必须将屏幕外页面限制设置为至少2(使用ViewPager.setOffscreenPageLimit(int) )。

Try this: 尝试这个:

@Override
    public float getPageWidth(int position) {
        return 0.80f;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM