Java Android:约束布局中的多个片段

Java Android:约束布局中的多个片段,java,android,fragment,constraints,Java,Android,Fragment,Constraints,首先,感谢您花一些时间通读这个问题,并指出我的代码中最有可能出现的明显错误。我做了一些研究,没有发现类似的问题——所以就在这里 我正在尝试创建一个android屏幕,屏幕上显示3个片段,如下所示:(左上角的第一个片段作为搜索片段,第二个片段(在其下方作为回收器布局)和第三个细节片段,一旦从回收器列表中单击某个对象,该片段就会膨胀 页面布局示例: 我使用两个具有布局权重的碎片容器的线性布局完成了这项工作,因此,当在第一个碎片中单击搜索按钮时,回收器列表将占据搜索碎片的位置,一旦单击,将在右侧的细

首先,感谢您花一些时间通读这个问题,并指出我的代码中最有可能出现的明显错误。我做了一些研究,没有发现类似的问题——所以就在这里

我正在尝试创建一个android屏幕,屏幕上显示3个片段,如下所示:(左上角的第一个片段作为搜索片段,第二个片段(在其下方作为回收器布局)和第三个细节片段,一旦从回收器列表中单击某个对象,该片段就会膨胀

页面布局示例:

我使用两个具有布局权重的碎片容器的线性布局完成了这项工作,因此,当在第一个碎片中单击搜索按钮时,回收器列表将占据搜索碎片的位置,一旦单击,将在右侧的细节容器充气,但理想情况下,我希望所有三个碎片都在t处出现在屏幕上他同时也是

下面是您想要使用的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>
这是搜索片段中的onClick方法。我尝试过从SilveryListActivity重写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:致命异常:main 流程:com.swimlabs.pcms.progressionapp,PID:21931 java.lang.RuntimeException:无法启动活动组件信息{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行


如果你说不出我是个新手,但我非常感谢你们提供的任何资源,甚至是一般性的建议,可以为我指明解决问题的正确方向。我不希望这里有人为我编写代码。谢谢,请告诉我是否可以包含任何其他信息。

如果必须将一个片段替换为另一个片段,请使用
。re放置
而不是
。添加
…如果不是这样,请忽略,抱歉,不能发表评论

现在很容易。您可以随时在约束布局上轻松膨胀碎片

在.xml文件中,用ID(如上所述,没有片段视图)说明三种不同的约束布局:constraint1、constraint2、constraint3。设计可以是您喜欢的

然后使用如下代码在这些约束上膨胀片段:

FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
Fragment fragment=new YourFragment1(); 
fragmentTransaction.replace(R.id.constraint1,fragment,null).commit();

更改片段名称和相应的约束条件,以便使这些片段膨胀。

尝试从布局中删除视图标记,或减少边距或布局高度,因为它们看起来非常像711dp或938 dp。好的,您也在其他地方使用过它们。只需尝试使用较小的dp,如200-300dp,而不是700或900。不要使用直接类似于“如上所述”的选项。很容易链接到“共享”链接以获得任何答案作为参考。此外,如果您指的是AndroidManifest.xml,请将其拼写出来。
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
Fragment fragment=new YourFragment1(); 
fragmentTransaction.replace(R.id.constraint1,fragment,null).commit();