简体   繁体   English

如何在fragment android中保存布局状态

[英]How to save state of layout in fragment android

I am working with tab layout with view pager and have ran into a problem, and I am not sure how to solve this, I have searched online but haven't found anything useful to work with. 我正在使用视图分页器处理选项卡布局,但遇到了一个问题,我不确定如何解决此问题,我在网上搜索过,但没有发现有用的方法。

I wasn't able to take video, so instead took screenshots: 我无法拍摄视频,因此拍摄了屏幕截图:

在此处输入图片说明 Scenario of these screen shots: 这些屏幕快照的场景:

I am on tab 1,
I enter in a name and press enter button
layout 1 visiblity is view.Gone and layout 2 is view.visible
I swipe/click to tab 2
I swipe/click to tab 3
when I swipe/click to tab 1
I expect layout visiblity to be view.visible and layout 1 to be view.gone
instead the app does not save the state at which I left
and goes back showing layout 1

The problem: I have 3 tabs, currently tab 2 and 3 are empty and I am working on tab 1. For tab 1, I have put two layouts and am using visibility function to show/hide tabs based on if-else statement. 问题:我有3个选项卡,当前选项卡2和3是空的,并且正在使用选项卡1。对于选项卡1,我放置了两种布局,并使用可见性功能基于if-else语句显示/隐藏选项卡。 First layout contains a edittext field with a button and for time being the second layout of tab 1 is empty. 第一种布局包含带有按钮的edittext字段,并且暂时,选项卡1的第二种布局为空。 The if-else statement checks if the user has written in their name in layout 1 and if so, it will make visibility of layout 1 View.GONE and make tab 2 View.VISIBLE. if-else语句检查用户是否在布局1中使用其名称书写,如果是,则将使布局1 View.GONE可见,并使选项卡2 View.VISIBLE。

However, when the user has written in a correct name and the app has validated it, it goes to layout 2 but when user goes to either by swipe or by click tab 2 or 3 and then go back to tab 1, layout 1 comes to top again and layout 2 is gone. 但是,当用户输入了正确的名称并且应用程序对其进行了验证时,它将转到布局2,但是当用户通过滑动或单击选项卡2或3转到然后回到选项卡1时,布局1进入了布局1。再次回到顶部,布局2消失了。

What I want to do is, if the app has validated and is showning layout 2 in tab 1, then I want the app to save this state and if the user changes tabs and come back to tab 1, I want the layout 2 to be shown and not 1. 我想做的是,如果该应用程序已经过验证并且在选项卡1中显示了布局2,那么我希望该应用程序保存此状态,并且如果用户更改了选项卡并返回到选项卡1,则我希望布局2为显示而不是1。

My layout for tab 1 is: 选项卡1的布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/layout1"
        android:visibility="gone">

        <EditText
            android:id="@+id/entered"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:textColor="#fff"
            android:background="?attr/colorPrimary"
            android:text="Welcome"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/layout2"
        android:visibility="visible">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="1dp"
            android:orientation="vertical">

        </LinearLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="100dp"
            android:orientation="vertical">

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_gravity="center_horizontal"
                android:textColor="#000000"
                android:hint="Your Name"/>

            <Button
                android:id="@+id/enter"
                android:textColor="#fff"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:background="?attr/colorPrimary"
                android:text="Enter" />
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

The if statement I have written to validate user input, currently it will only proceed to layout 2, if the text is 'name': 我为验证用户输入而编写的if语句,如果文本为“ name”,则当前仅进入布局2:

if (name.getText().toString().equals("name")){
                layout1.setVisibility(View.GONE);
                layout2.setVisibility(View.VISIBLE);
            }else {
                layout1.setVisibility(View.VISIBLE);
                layout2.setVisibility(View.GONE);

            }

Sorry about the lengthy post, I wasn't sure best way of explaining my problem. 对冗长的帖子感到抱歉,我不确定解释问题的最佳方法。 In case my post isn't clear, what I want to do is save the state of fragment layouts in tab 1 so when user changes tabs eg to tab 2 or 3 and comes back to tab 1, I want the layout that was shown when they left tab 1 to be shown. 如果我的帖子不清楚,我想做的是将片段布局的状态保存在选项卡1中,因此当用户将选项卡更改为例如选项卡2或3并返回到选项卡1时,我希望显示的布局为他们离开了标签1进行显示。

If my question/post is not clear please let me know and thanks in advance, I hope I have explained myself properly. 如果我的问题/帖子不清楚,请提前告知我,谢谢,我希望我已经正确地解释了自己。

EDITED: 编辑:

Global variable: 全局变量:

public MenuItem  settings;

  if (name.getText().toString().equals("name")){
                    layout1.setVisibility(View.GONE);
                    layout2.setVisibility(View.VISIBLE);
                    settings.setEnabled(true);
                }else {
                    layout1.setVisibility(View.VISIBLE);
                    layout2.setVisibility(View.GONE);
                     settings.setEnabled(false);

                }

EDITED 2 编辑2

public class FragmentA extends Fragment {

    public boolean shouldShowItem = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        if (name.getText().toString().equals("name")){
            layout1.setVisibility(View.GONE);
            layout2.setVisibility(View.VISIBLE);
            shouldShowItem = true;
        }else {
            layout1.setVisibility(View.VISIBLE);
            layout2.setVisibility(View.GONE);
            shouldShowItem = false;

        }


        return inflater.inflate(R.layout.fragmenta, container, false);

    }

    @Override
    public boolean onPrepareOptionsMenu (Menu menu) {
        if (shouldShowItem)
            menu.getItem(item_index).setEnabled(false);
        return true;
    }

You can use 您可以使用

ViewPager.setOffscreenPageLimit(int offset)

In your case, you want to specify 2, so that when you are on the third page, the first one is not destroyed, and vice-versa. 在您的情况下,您想指定2,这样当您在第三页上时,第一页不会被破坏,反之亦然。

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);

Regarding the menu: 关于菜单:

Take a look at the API 看一下API

public boolean onPrepareOptionsMenu (Menu menu)

Prepare the Screen's standard options menu to be displayed. 准备要显示的屏幕标准选项菜单。 This is called right before the menu is shown, every time it is shown. 在显示菜单之前,每次显示菜单时都会调用此方法。 You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents. 您可以使用此方法有效地启用/禁用项目,或者以其他方式动态修改内容。

So you should handle that from the Activity. 因此,您应该在“活动”中进行处理。

@Override
 public boolean onPrepareOptionsMenu (Menu menu) {
     if (shouldShowItem)
         menu.getItem(item_index).setEnabled(false);
     return true;
 }

On changes, you need to call 进行更改时,您需要致电

invalidateOptionsMenu()

and the the menu will be redraw 菜单将重新绘制

give Memory to Your ViewPager: 1. 给您的ViewPager内存:1。

MyAdapter mAdapter= new MyAdapter(getActivity().getSupportFragmentManager());
  mAdapter.contents.add(new Fragment1());
  mAdapter.contents.add(new Fragment2());
  mAdapter.contents.add(new Fragment3());
  mPager.setAdapter(mAdapter);
  1. This is adapter for your pager 这是您的传呼机的适配器
 class MyAdapter extends FragmentStatePagerAdapter { String[] pageTitle = {"Dummy 1", "Dummy 2", "Dummy 3"}; public JantriPagerAdapter(FragmentManager fm) { super(fm); } ArrayList<Fragment> contents = new ArrayList<>(); @Override public Fragment getItem(int position) { return contents.get(position); } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { return pageTitle[position]; } } 
  1. Now create three fragment name 现在创建三个片段名称

    class Fragment1 extends Fragment{ Fragment1类扩展Fragment {

    } . }。 .

  2. Now your state is automatically saved 现在,您的状态已自动保存

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

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