请片段全屏编程全屏、片段

2023-09-12 02:08:33 作者:南城以南青如衫

我有一个ViewPager和FragmentPagerAdapter成立了一个活动有3个片段。所有片段中同时加载并使用保存在存储器

I have a ViewPager and FragmentPagerAdapter set up in one activity with 3 fragments. All fragments are loaded simultaneously and are kept in memory using

mPager.setOffscreenPageLimit(2);

其中的一个片段举办摄像头preVIEW,我想提出全屏,没有状态栏或操作栏。另一个片段要求操作栏显示出来。

One of these fragments hosts a camera preview that I would like to make full screen, with no status bar or action bar. The other fragments require that the action bar be displayed.

我将如何让只有相机preVIEW片段全屏幕,同时保持其他两个片段是否正常?我一直使用的主题,这意味着打破了操作栏尝试。

How would I make only the camera preview fragment full screen while keeping the other two fragments normal? I have tried using themes, which means breaking the action bar.

编程调用下面没有帮助,因为它迫使整个活动将是全屏幕(和其他两个与它一起的片段):

Programmatically calling the following doesn't help, because it forces the whole activity to be full screen (and thus the other two fragments along with it):

requestWindowFeature(Window.FEATURE_NO_TITLE); 
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

我也试过实施ViewPager.OnPageChangeListener:

I also tried implementing ViewPager.OnPageChangeListener:

public void onPageSelected(int position) {
    if(position == 1)
        getActionBar().hide();
    else
        getActionBar().show();
}

这确实隐藏操作栏,但它并不隐藏系统状态栏(我说的是哪里的通知,状态图标和时间),并在操作栏隐藏/显示动画也非常不连贯刷卡的时候。

This does hide the action bar, but it doesn't hide the system status bar (I'm talking about where the notifications, status icons, and time are), and the action bar hide/show animation is also very choppy when swiping.

有关的是我想要做的正是一个例子,看看Snapchat - 他们设法拉断相机和其他碎片完美的刷卡

For an example of what I want to do exactly, see Snapchat - they manage to pull off the swiping between camera and other fragments perfectly.

编辑:

所以我改变了我的onPageSelected这样:

So I changed my onPageSelected to this:

public void onPageSelected(int position) {
    if(position == 1) {
        MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    } else {
        MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }
}

这使我改变了状态栏是否显示与否。但是,它会导致大量的JANK的。同样的问题,用行动吧。

This enables me to change whether the status bar is displayed or not. However, it causes a large amount of jank. Same problem with the action bar.

我注意到在Snapchat状态栏幻灯片和标签变更后下来就完成了。我不知道如何实现这一点,所以关于这方面的任何建议将AP preciated。

I notice that in Snapchat the status bar slides up and down after the tab change is complete. I'm not sure how to implement this, so any advice regarding this aspect would be appreciated.

推荐答案

我想通了,如何做到这一点,但它绝对是一个黑客。

I figured out how to do this, though it's definitely a hack.

诀窍是不要使用Android操作栏,这是使自己在布局文件。

我在code定义了两个多RelativeLayouts:

I defined two more RelativeLayouts in my code:

<RelativeLayout
    android:id="@+id/padding"
    android:layout_width="fill_parent"
    android:layout_height="25dip"
    android:layout_alignParentTop="true"
    android:background="@color/abs__background_holo_dark" />

<RelativeLayout
    android:id="@+id/fake_action_bar"
    android:layout_width="fill_parent"
    android:layout_height="48.0dip"
    android:layout_below="@id/padding" >

<!-- stuff here -->

</RelativeLayout>

顶RelativeLayout的是填充为Android状态栏。我将它设置为25dip,但它可能会有所不同 - 它很可能是最好的以编程方式设置此值。你也需要它,这样它匹配的状态栏的颜色,使用户界面看起来更自然,设置为黑色。

The top RelativeLayout is padding for the Android status bar. I set it to 25dip, but it may vary - it would probably be best to set this value programmatically. Also you need to set it to a black color so that it matches the status bar color and makes the UI look more natural.

底部RelativeLayout的是你的假动作栏。你需要的一切东西你的动作条的需求(标题文字,按钮等)在此RelativeLayout的。我将它设置为48dip,但同样,这会有所不同,你应该通过编程设定。

The bottom RelativeLayout is for your fake action bar. You need to stuff everything your action bar needs (title text, buttons, etc.) in this RelativeLayout. I set it to 48dip, but again, this will vary and you should probably set it programmatically.

在承载您的碎片的活动,ViewPager和FragmentPagerAdapter,你需要设置的标志,告诉机器人,使状态栏的叠加:

In the Activity hosting your Fragments, ViewPager and FragmentPagerAdapter, you need to set the flags telling Android to make the status bar an overlay:

WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;

现在我们需要做的状态栏隐藏和显示,当我们切换片段。有你的活动实施ViewPager.OnPageChangeListener,然后用下面的code:

Now we need to make the status bar hide and show when we switch fragments. Have your activity implement ViewPager.OnPageChangeListener and then use the following code:

@Override
public void onPageScrollStateChanged(int state) {
    switch(state) {
    case ViewPager.SCROLL_STATE_IDLE:
        if(mPosition == 1) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
    }
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    mPosition = position;
}

我们使用onPageSelected保持的页码轨道,并用它在onPageScrollStateChanged动态隐藏/显示当前正在显示其上段是根据不同的状态栏。你不能做到这一点onPageSelected,因为这将导致状态栏,通过刷卡中途重新显示。

We keep track of the page number using onPageSelected and use it in onPageScrollStateChanged to dynamically hide/show the status bar depending on which Fragment is currently being displayed. You can't do this in onPageSelected because this will cause the status bar to redisplay halfway through a swipe.

您还需要确保添加适当的$ C $下你的假动作栏 - 用于按钮,这将是OnClickListener

You also need to make sure to add appropriate code for your fake action bar - for buttons, this would be OnClickListener.