我有一个带TabLayout / FragmentPagerAdapter的应用程序来控制四个标签。其中三个选项卡工作正常并显示其各自的片段(没有问题)。第四个片段,间歇性地无法显示视图的内容。
我已经测试并且视图的类已加载,但没有内容。片段确实包含嵌套片段(它们不再嵌套),我认为这可能导致问题。下面是带有问题的片段的XML。
<div class="container">
<div class="row">
<div class="col-md-12">
<form method="post" class="form-horizontal" action="<?php echo htmlspecialchars(trim($_SERVER["PHP_SELF"], '.php'));?>">
<fieldset>
<legend>example</legend>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label"></label>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Name</label>
<div class="col-lg-9">
<input type="text" name="name" value="" class="form-control" id="name" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="select1" class="col-lg-2 control-label">example</label>
<div class="col-lg-9">
<select name="select1" class="form-control" id="select1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
</select>
</div>
</div>
<div class="form-group">
<label for="select2" class="col-lg-2 control-label">example</label>
<div class="col-lg-9">
<select name="select2" class="form-control" id="select2">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</div>
</div>
<div class="form-group">
<label for="select3" class="col-lg-2 control-label">example</label>
<div class="col-lg-9">
<select name="select3" class="form-control" id="select3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</div>
</div>
<div class="form-group">
<label for="textArea" class="col-lg-2 control-label">example</label>
<div class="col-lg-9">
<textarea name="textarea" class="form-control" rows="7" id="textArea" placeholder=""></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-9 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Go!</button>
</div>
</div>
</fieldset>
</form>
</div>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
Panel content
</div>
</div>
</div>
</div>
</div>
以下是相关片段的类。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
</FrameLayout>
</LinearLayout>
要清楚。我遇到的问题是在标签之间切换一段时间后。此视图和此视图将不再显示。 TabLayout似乎没有问题,因为它正在加载这个类。我希望有人能够提供一些指导来解决这个问题。
答案 0 :(得分:2)
您需要使用getChildFragmentManager()
代替getFragmentManager()
来放置和管理片段内的片段。
所以
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.fragment_layout, container, false);
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, subFragment1);
fragmentTransaction.commit();
return view;
}
public void showSubFragment2() {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
fragmentTransaction.replace(R.id.fragmentContainer, subFragment2);
fragmentTransaction.commit();
}
public void showSubFragment1() {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left);
fragmentTransaction.replace(R.id.fragmentContainer, subFragment1);
fragmentTransaction.commit();
}
答案 1 :(得分:0)
尝试拨打getChildFragmentManager()
而不是getFragmentManager()
,看看是否有帮助。