在LinearLayout中的现有片段下动态添加片段

时间:2015-01-11 12:57:27

标签: android android-layout android-fragments fragment

我正在制作一个活动,其中有一个基于用户输入的地址的评论部分。 用户将通过按下按钮添加新的评论部分。这是一个非固定数量的部分,所以我最初将此部分添加为xml中的片段。 我有一个onClick函数,它将另一个片段添加到linearlayout。

我的问题是新的片段总是添加到线性布局的顶部,即在现有片段/片段之上(即新片段将向下推送所有其他片段)。

如何添加新片段以使其显示在现有片段下方?

.java文件中的代码:

public class SpecialReportAdden extends ActionBarActivity {
int numOfFragments;
LinearLayout addenHolder;


TextView report;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_special_report_adden);
    numOfFragments=1;

    Button addenSave = (Button)findViewById(R.id.btnAddendumSave);
    Button addenAddComment = (Button)findViewById(R.id.btnAddendumAddComment);
    addenHolder =(LinearLayout)findViewById(R.id.AddenLinLayHolder);

    addenSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //this should save all comments including those added in dynamically produced fragments

            //save database

            Intent i = new Intent (SpecialReportAdden.this, MenuPage.class);
            startActivity(i);
        }
    });


    addenAddComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("number of Fragments at start of OnClick", Integer.toString(numOfFragments));
            Fragment newAddenFrag = addNewfragment(numOfFragments);
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.AddenLinLayInnerHolder, newAddenFrag);
            ft.addToBackStack(null);
            ft.commit();

            numOfFragments++;
            Log.i("number of Fragments updated", Integer.toString(numOfFragments));
        }
    });
}


public Fragment addNewfragment(int number) {
    AddendumLinInputFragment adden = new AddendumLinInputFragment();
    TextView tx = (TextView) findViewById(R.id.txtVAddendumFragReportNum);
    tx.setText("Report "+number+" :");
    tx.setTextColor(Color.BLACK);
    TextView address = (TextView)findViewById(R.id.txtVAddendumFragAddress);
    address.setText("A new address");
    address.setTextColor(Color.BLUE);
    return  adden;
}

.java活动的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.danielt.pestcontrol.SpecialReportAdden"
android:id="@+id/RellayAddenComments">

<TextView
    android:id="@+id/txtVAddendumTitle" android:text="Service Report Addendum" android:textStyle="bold" android:layout_centerHorizontal="true" android:elegantTextHeight="true" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"/>

<TextView
    android:id="@+id/txtVAddendumTechName"
    android:layout_below="@+id/txtVAddendumTitle"
    android:layout_marginTop="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Technician Name: "
    android:textSize="18sp"
    />

<TextView
    android:id="@+id/txtVAddendumDate"
    android:layout_below="@+id/txtVAddendumTechName"
    android:layout_marginTop="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Date: "
    android:textSize="18sp"
    />
<ScrollView
    android:id="@+id/scrlVAddendum"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtVAddendumDate">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/AddenLinLayHolder"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/AddenLinLayInnerHolder"
            android:orientation="vertical">


            <fragment
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:name="com.example.danielt.pestcontrol.AddendumLinInputFragment"
                android:id="@+id/addendum1CommentsFragment"
                android:layout_below="@+id/txtVAddendum1Date"
                android:layout_marginTop="24dp"
                tools:layout="@layout/fragment_addendum_lin_input" />
        </LinearLayout>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add another Addendum Comment"
            android:id="@+id/btnAddendumAddComment"
            android:layout_below="@+id/scrlVAddendum"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="36dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save / Update Comments"
            android:id="@+id/btnAddendumSave"
            android:layout_below="@+id/btnAddendumAddComment"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="44dp" />

    </LinearLayout>


</ScrollView>

我没有添加片段代码,但如果有人想看到它我会的。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您不需要片段只保留评论视图。只需夸大新的评论视图并​​将其添加到父视图中。

LayoutInflater inflater = LayoutInflater.from(context);
View inflatedLayout= inflater.inflate(R.layout.yourLayout, container, false);
container.addView(inflatedLayout);

其中,容器是包含所有注释的视图。

关于添加片段,根据Fragments documentation

  

如果您要将多个片段添加到同一容器中,那么   添加它们的顺序决定了它们在中的显示顺序   查看层次结构

因此,如果它的行为不同,则可能与操作系统版本或错误有关:)