Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 活动中的许多框架布局_Android_Android Fragments - Fatal编程技术网

Android 活动中的许多框架布局

Android 活动中的许多框架布局,android,android-fragments,Android,Android Fragments,我试图在活动中添加很多框架布局。它看起来像: 屏幕上显示多少取决于屏幕分辨率,因此我想添加十几个片段,并使活动可滚动。我希望它看起来像列表视图。现在我有一些框架布局,并一个接一个地添加片段。如果有人知道怎么做,请帮助我。谢谢 FragmentManager fragmentManager = getSupportFragmentManager(); Fragment s1 = new SingleCourseF

我试图在活动中添加很多框架布局。它看起来像:

屏幕上显示多少取决于屏幕分辨率,因此我想添加十几个片段,并使活动可滚动。我希望它看起来像列表视图。现在我有一些框架布局,并一个接一个地添加片段。如果有人知道怎么做,请帮助我。谢谢

                    FragmentManager fragmentManager = getSupportFragmentManager();

                    Fragment s1 = new SingleCourseFragment();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    FrameLayout container = findViewById(R.id.fragmentContainer);
                    fragmentTransaction.add(R.id.fragmentContainer, s1);
                    fragmentTransaction.commit();

                    Fragment s2 = new SingleCourseFragment();
                    FragmentTransaction fragmentTransaction2 = fragmentManager.beginTransaction();
                    fragmentTransaction2.add(R.id.fragmentContainer2, s2);
                    fragmentTransaction2.commit();
                    FrameLayout container2 = findViewById(R.id.fragmentContainer2);


                    Fragment s3 = new SingleCourseFragment();
                    FragmentTransaction fragmentTransaction3 = fragmentManager.beginTransaction();
                    fragmentTransaction3.add(R.id.fragmentContainer3, s3);
                    fragmentTransaction3.commit();
                    FrameLayout container3 = findViewById(R.id.fragmentContainer3);


                    Fragment s4 = new SingleCourseFragment();
                    FragmentTransaction fragmentTransaction4 = fragmentManager.beginTransaction();
                    fragmentTransaction4.add(R.id.fragmentContainer4, s4);
                    fragmentTransaction4.commit();
                    FrameLayout container4 = findViewById(R.id.fragmentContainer4);

                    container.setVisibility(View.VISIBLE);
                    container2.setVisibility(View.VISIBLE);
                    container3.setVisibility(View.VISIBLE);
                    container4.setVisibility(View.VISIBLE);
XML


如果我理解正确,您只想使布局可滚动

您可以将顶层布局设置为滚动视图,如下所示

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:fillViewport="true">

<!-- Then put your layout here as such-->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <!-- other widgets in your layout -->

</RelativeLayout>

</ScrollView>

更新 您可以尝试添加xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:fillViewport="true">

 <!-- Add a linear layout-->
 <LinearLayout
    android:id="@+id/list_of_frags"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 </LinearLayout>


</ScrollView>

//Begin the transaction
LinearLayout layout = (LinearLayout)findViewById(R.id.list_of_frags);

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

//You can create an array with the container
ArrayList<int> listOfContainers = ArrayList<int>()
listOfContainers.add(R.id.fragmentContainer1)
listOfContainers.add(R.id.fragmentContainer2)
listOfContainers.add(R.id.fragmentContainer3)
listOfContainers.add(R.id.fragmentContainer4)

Int AmountOfFragments = 4

//Replace the contents of the container with the new fragmentTest
for (int i = 0; i < AmountOfFragments; i++){

  //Or You can create the FrameLayout programmatically as below
  FrameLayout frame = new FrameLayout(this);
  frame.setId(i);
  layout.addView(frame);
  ft.add(i, new SingleCourseFragment());

  //if you do the above you don't need this line
  ft.add(listOfContainers[i], new SingleCourseFragment());

}

//Complete the changes
ft.commit();
//开始交易
LinearLayout layout=(LinearLayout)findViewById(R.id.list\u of\u frags);
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
//您可以使用容器创建一个数组
ArrayList listOfContainers=ArrayList()
容器列表。添加(R.id.fragmentContainer1)
容器列表。添加(R.id.fragmentContainer2)
容器列表。添加(R.id.fragmentContainer3)
容器列表。添加(R.id.fragmentContainer4)
Int amountofffragments=4
//用新的碎片测试替换容器中的内容
对于(int i=0;i
我没有详细解释。我正在寻找一种方法,可以在活动中添加很多框架布局,而不必像我的代码中那样逐个添加,而是一次添加十几个片段,使其看起来像列表视图。我明白了,你能检查我更新的答案吗?我没有测试过它,因为它工作得很好,这几乎是我所需要的,但还有一件事。在程序的一次运行中,我需要显示几个片段,而在另一次运行中,我需要显示十几个片段。因此,如果我理解正确,我必须将十几个片段永久地添加到content_main.xml中?不一定,您可以根据需要以编程方式创建框架布局。检查我的更新答案这就是我的意思,但碎片出现在顶部,而不是一个在另一个下面,我无法修复它。听说过回收站吗?我不知道它是什么,但我会读到的。谢谢
//Begin the transaction
LinearLayout layout = (LinearLayout)findViewById(R.id.list_of_frags);

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

//You can create an array with the container
ArrayList<int> listOfContainers = ArrayList<int>()
listOfContainers.add(R.id.fragmentContainer1)
listOfContainers.add(R.id.fragmentContainer2)
listOfContainers.add(R.id.fragmentContainer3)
listOfContainers.add(R.id.fragmentContainer4)

Int AmountOfFragments = 4

//Replace the contents of the container with the new fragmentTest
for (int i = 0; i < AmountOfFragments; i++){

  //Or You can create the FrameLayout programmatically as below
  FrameLayout frame = new FrameLayout(this);
  frame.setId(i);
  layout.addView(frame);
  ft.add(i, new SingleCourseFragment());

  //if you do the above you don't need this line
  ft.add(listOfContainers[i], new SingleCourseFragment());

}

//Complete the changes
ft.commit();