首页 > 解决方案 > 活动中有很多框架布局

问题描述

我正在尝试向活动添加很多框架布局。看起来像:

照片1

照片2

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

                    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"?>
<RelativeLayout 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:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<EditText
    android:id="@+id/startET"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:inputType="text"
    android:layout_margin="20dp"
    android:enabled="false"
    android:gravity="center"
    android:background="@drawable/rounded_border_edittext"
    android:hint="@string/start"/>

<EditText
    android:id="@+id/endET"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/startET"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    android:enabled="false"
    android:gravity="center"
    android:background="@drawable/rounded_border_edittext"
    android:inputType="text"/>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentContainer"
    android:layout_below="@+id/endET"
    android:visibility="invisible">
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentContainer2"
    android:layout_below="@+id/fragmentContainer"
    android:visibility="invisible">
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentContainer3"
    android:layout_below="@+id/fragmentContainer2"
    android:visibility="invisible">
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentContainer4"
    android:layout_below="@+id/fragmentContainer3"
    android:visibility="invisible">
</FrameLayout>

</RelativeLayout>

标签: androidandroid-fragments

解决方案


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

您可以将顶级布局设置为 ScrollView,如下所示

<?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();

推荐阅读