java - Android:约束布局中的多个片段
问题描述
在此先感谢您花一些时间阅读此问题并指出我的代码中最有可能出现的明显错误。我做了一些研究,但找不到类似的问题——所以就在这里。
我正在尝试创建一个 android 屏幕,该屏幕显示像这样在屏幕上分离的 3 个片段 - (左上角的第一个片段作为搜索片段,第二个(在其下方作为回收器布局)和第三个细节片段,一旦对象膨胀从回收站列表中单击。
页面布局示例:
我已经使用带有 layout_weights 的两个片段容器的线性布局来完成此操作,因此当在第一个片段中单击搜索按钮时,回收器列表会占据搜索片段的位置,并且一旦单击就会膨胀右侧的详细信息容器, 但理想情况下 id 就像所有三个片段同时出现在屏幕上一样。
这是要使用的 XML 测试布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragmentTest1"
android:name="com.swimlabs.pcms.progressionapp.SearchFragment"
android:layout_width="319dp"
android:layout_height="179dp"
android:layout_marginEnd="2dp"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintEnd_toStartOf="@+id/divider2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/divider"
android:layout_width="325dp"
android:layout_height="14dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="200dp"
android:background="?android:attr/listDivider"
app:layout_constraintEnd_toStartOf="@+id/divider2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/divider2"
android:layout_width="14dp"
android:layout_height="711dp"
android:layout_marginEnd="938dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/fragmentTest1"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/fragmentTest2"
android:name="com.swimlabs.pcms.progressionapp.SwimmerListFragment"
android:layout_width="270dp"
android:layout_height="424dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/divider2"
app:layout_constraintStart_toStartOf="parent" />
<fragment
android:id="@+id/fragment5"
android:name="com.swimlabs.pcms.progressionapp.ProgressionFragment"
android:layout_width="923dp"
android:layout_height="640dp"
android:layout_marginBottom="7dp"
android:layout_marginEnd="9dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/divider2" />
</android.support.constraint.ConstraintLayout>
以下是我如何动态创建片段的代码摘录。这是我的单例片段类。这里注释掉的是我的旧代码,它适用于带有 2 个容器的线性布局。
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@LayoutRes
protected int getLayoutResId() {
// return R.layout.master_coach;
return R.layout.test;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
FragmentManager fm = getSupportFragmentManager();
// Fragment fragment = fm.findFragmentById(R.id.fragment_container);
Fragment fragment = fm.findFragmentById(R.id.fragmentTest1);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
// .add(R.id.fragment_container, fragment)
.add(R.id.fragmentTest1, fragment)
.commit();
}
}
}
这是搜索片段中的 onClick 方法。我尝试从 SwimmerListActivity 覆盖 onCreateView 方法,还尝试使用片段管理器,但这些都不起作用。
mSearchButton.setOnClickListener(new View.OnClickListener () {
@Override
public void onClick(View v) {
// Fragment fragment = SwimmerListFragment.newInstance();
// FragmentManager fm =
// getActivity().getSupportFragmentManager();
// fm.beginTransaction()
// .add(R.id.fragmentTest2, fragment)
// .commit();
Intent intent = new Intent(getActivity(),
SwimmerListActivity.class);
startActivity(intent);
}
});
我不断收到这样的错误:
08-13 19:04:59.291 21931-21931/com.swimlabs.pcms.progressionapp E/AndroidRuntime:致命异常:主进程:com.swimlabs.pcms.progressionapp,PID:21931 java.lang.RuntimeException:无法启动活动ComponentInfo{com.swimlabs.pcms.progressionapp/com.swimlabs.pcms.progressionapp.CoachActivity}:android.view.InflateException:二进制 XML 文件第 41 行:二进制 XML 文件第 41 行: 在 android.app 处膨胀类片段时出错。 ActivityThread.performLaunchActivity(ActivityThread.java:2913) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
其中第 41 行是第二个片段开始的 test.xml 行。
如果你不能说我对此很陌生,但我很感激你们提供的任何资源甚至是一般性的建议,它们可以为我指明正确的方向来解决这个问题。我不希望这里有人为我编写代码。谢谢,如果我可以提供任何其他信息,请告诉我。
解决方案
如果您必须将一个片段替换为另一种用途......如果.replace
不是.add
这种情况,请忽略,抱歉不能发表评论
推荐阅读
- c++ - 虚拟继承:没有匹配的调用函数
- typescript - 如何在不安装 NPM 包的情况下在 TypeScript 2 中进行声明合并?
- python - python:数据在采样到一个小数据集后添加了一个额外的列
- javascript - 基于找到的内容的高级查找和替换 - Javascript
- jsf - JSF 关于什么是 UIComponent
- sql - 基本的 sql 语法 如何引用我的数据库?
- javascript - 创建索引时检测到 Mongo 循环依赖
- indexing - 不同宽度的索引寄存器
- python - Django ModelForm 没有将数据保存到数据库,Form.save 不起作用?
- javascript - 使用脚本从 Google 表格在 Google 议程中创建事件时遇到问题