【发布时间】:2016-05-07 15:50:24
【问题描述】:
我正在使用带有视图寻呼机的选项卡布局并遇到问题,我不知道如何解决这个问题,我已经在线搜索但没有发现任何有用的东西。
我无法拍摄视频,因此截取了屏幕截图:
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 语句的选项卡。第一个布局包含一个带有按钮的 edittext 字段,暂时选项卡 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,如果文本是“名称”:
if (name.getText().toString().equals("name")){
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
}else {
layout1.setVisibility(View.VISIBLE);
layout2.setVisibility(View.GONE);
}
抱歉,这篇文章太长了,我不确定解释我的问题的最佳方式。如果我的帖子不清楚,我想要做的是将片段布局的状态保存在选项卡 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 的适配器,您应该使用 FragmentStatePagerAdapter 保存对象的状态。为 FragmentStatePagerAdapter 调用 OnPause 和 OnResume 方法
-
我使用的适配器只有getitem和getcount
-
请您指导我/给我一个实现 onpause 和 onresume 的适配器示例
-
你可以在 YouTube 上观看 Slidenerd 的 Android Fragments Tutorial 的教程,并通过 27-30 你肯定会得到答案。如果你需要例子让我知道
-
例子很好,不过,我在看这些tuts,真的很有用
标签: android android-layout android-fragments layout tabs