更新片段时为什么getActivity()有效?

时间:2014-11-25 08:43:37

标签: android android-fragments

我有2个简单的xml文件,一个包含<fragment><EditText><Button>,另一个包含<TextView>

对于我的两个类,我的MainActivity设置按钮的onClickListeners,以通过bundle将EditText的输入发送到另一个类FragmentActivity。

我的FragmentActivity看起来像这样:

public class FragmentActivity extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View V = inflater.inflate(R.layout.activity_fragment, container, false);


    Bundle input = this.getArguments();

    if(input != null) {

        String message = input.getString("msg");

        TextView fragText = (TextView) getActivity().findViewById(R.id.fragment_text);

        fragText.setText(message);
    }

    return V;
}

}

最初,我的fragText看起来像这样:

TextView fragText = (TextView) V.findViewById(R.id.fragment_text);

结果是文本永远不会更新。到底发生了什么?

此外,当您在代码中使用View替换片段时,这是否意味着您可以将main_layout.xml视为没有<fragment>,而是拥有其他内容(例如<TextView> )?

修改:很抱歉,发布其他有问题的文件可能并不聪明。

activity_fragment.xml

<LinearLayout 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:orientation="vertical"
    tools:context="com.test.android.testfragapp.FragmentActivity">

    <TextView
        android:id="@+id/fragment_text"
        android:text="This is a fragment."
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

activity_main.xml中

<LinearLayout 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:orientation="vertical">

    <fragment
        class="com.test.android.testfragapp.FragmentActivity"
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="4"
        tools:layout="@layout/activity_fragment"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="5"
        android:orientation="vertical"
        android:gravity="center_horizontal">

        <EditText
            android:id="@+id/input_text"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:gravity="center" />

        <Button
            android:id="@+id/button_display_regular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to display"/>

        <Button
            android:id="@+id/button_display_reverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to reverse"/>

    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // removes the shadow from the action bar for lollipop devices
        if(Integer.valueOf(Build.VERSION.SDK_INT) >= 21)
            getActionBar().setElevation(0);

        final EditText input_text = (EditText) findViewById(R.id.input_text);

        this.findViewById(R.id.button_display_regular).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("msg", input_text.getText().toString());
                placeFragment(bundle);
            }
        });
    }

    // Replace the fragment with the activity_fragment layout
    public void placeFragment(Bundle bundle){
        Fragment fr = new FragmentActivity();

        fr.setArguments(bundle);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_holder, fr);
        fragmentTransaction.commit();
    }
}

2 个答案:

答案 0 :(得分:0)

由于您尚未发布您的xml文件,我需要猜测。 R.layout.activity_fragment真的包含你的TextView吗?从命名我会认为它包含你的片段标签,然后是你的Activity的布局,所以只是错误的。

答案 1 :(得分:0)

我们需要查看您的XML文件才能更好地回答这个问题。我们还需要了解您的Fragment是如何添加到Activity的,但是根据您发布的内容,您似乎正在夸大什么是活动 片段中的布局。

这不是应该如何工作的。我猜你想要的是沿着以下几行

  1. activity_main.xml只包含您的<fragment>代码
  2. 包含my_fragment.xml
  3. TextView
  4. Activity
  5. setContentView(R.layout.activity_main.xml)的{​​{1}}
  6. onCreate() Fragment View fragView = inflater.inflate(R.layout.my_fragment, container, false);。{/ 1}}

    现在,您可以执行onCreateView()

    简而言之,看起来你看到了奇怪的行为,因为你已经混淆了片段布局和活动布局。