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

标签 android android-layout android-fragments layout tabs

我正在使用带 View 寻呼机的选项卡布局,遇到了一个问题,我不确定如何解决这个问题,我在网上搜索过,但没有找到任何有用的东西。

我没法拍视频,所以截屏了:

enter image description here 这些屏幕截图的场景:

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

问题: 我有 3 个选项卡,当前选项卡 2 和 3 是空的,我正在处理选项卡 1。对于选项卡 1,我放置了两个布局,并使用可见性函数根据 if-else 语句显示/隐藏选项卡。第一个布局包含一个带有按钮的编辑文本字段,暂时选项卡 1 的第二个布局是空的。 if-else 语句检查用户是否在布局 1 中写入了他们的名字,如果是,它将使布局 1 View.GONE 的可见性并使选项卡 2 View.VISIBLE 可见。

但是,当用户输入了正确的名称并且应用程序对其进行了验证时,它会转到布局 2,但是当用户通过滑动或单击选项卡 2 或 3 然后返回选项卡 1 时,布局1 再次出现在顶部,布局 2 消失了。

我想做的是,如果应用程序已验证并在选项卡 1 中显示布局 2,那么我希望应用程序保存此状态,如果用户更改选项卡并返回选项卡 1,我需要布局显示 2 个而不是 1 个。

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

我编写的用于验证用户输入的 if 语句,目前它只会继续布局 2,如果文本是“name”:

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

            }

对于冗长的帖子感到抱歉,我不确定解释我的问题的最佳方式。如果我的帖子不清楚,我想做的是将 fragment 布局的状态保存在选项卡 1 中,这样当用户更改选项卡时,例如转到选项卡 2 或 3 并返回到选项卡 1,我希望显示他们离开选项卡 1 时显示的布局。

如果我的问题/帖子不清楚,请让我知道并提前致谢,我希望我已经正确解释了自己。

编辑:

全局变量:

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

                }

已编辑 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;
    }

最佳答案

为您的 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. 这是寻呼机的适配器
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. 现在创建三个 fragment 名称

    类 Fragment1 扩展 fragment {

    } . .

  2. 现在您的状态已自动保存

关于android - 如何在 fragment android中保存布局状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092513/

相关文章:

android - 如何使倒数计时器在所有 Activity 中显示相同的时间

android - 重复使用 Android Shared Preference 存储会影响性能吗?

java - 启用 html 的 android ListView

android LicenseValidator : Signature verification failed

java - Android 布局与图像和兼容性

android - 如何让表格布局的第一行占据整个空间

android - 如何访问android主项目中模块Jar的类文件

android - 正在添加 TableRow 但不显示

android - 监听器不适用于 fragment 的后栈

android - Google PlaceAutocompleteFragment 因错误而崩溃