zoukankan      html  css  js  c++  java
  • Fragment之间的交互

    通常,一个活动可能包含一个或多个协同工作的Fragment以向用户展现一致的UI。在这种情况下,Fragment之间就需要彼此通信并交换数据,这是非常重要的。例如,一个Fragment可能包含了一个条目列表(如来自一个RSS提要的帖子)。当用户轻点Fragment上的某个条目时,所选条目的详细信息可能会显示在另一个Fragment上。
    下面的“试一试”介绍了一个Fragment如何访问另一个Fragment中的视图。

    (1) 使用上一节创建的项目,向Fragment1.xml文件中添加如下所示的粗体代码:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3.    xmlns:android="http://schemas.android.com/apk/res/android"
    4.    android:orientation="vertical"
    5.    android:layout_width="fill_parent"
    6.    android:layout_height="fill_parent"
    7.    android:background="#00FF00"
    8.    >
    9. <TextView
    10.    android:id="@+id/lblFragment1"
    11.    android:layout_width="fill_parent"
    12.    android:layout_height="wrap_content"
    13.    android:text="This is fragment #1" />
    14. </LinearLayout>
    (2) fragment2.xml文件中添加如下所示的粗体代码:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3.    xmlns:android="http://schemas.android.com/apk/res/android"
    4.    android:orientation="vertical"
    5.    android:layout_width="fill_parent"
    6.    android:layout_height="fill_parent"
    7.    android:background="#FFFE00"
    8.    >
    9. <TextView
    10.    android:layout_width="fill_parent"
    11.    android:layout_height="wrap_content"
    12.    android:text="This is fragment #2" />

    13. <Button android:id="@+id/btnGetText"
    14.      android:layout_width="wrap_content"
    15.      android:layout_height="wrap_content"
    16.      android:text="Get text in Fragment #1" />
    17. </LinearLayout>
    (3) 修改MainActivity.java文件,将前几节添加的代码注释掉。修改后的代码如下所示:
    1. package net.learn2develop.Fragments;

    2. import net.learn2develop.Fragments.R;
    3. import android.app.Activity;
    4. import android.os.Bundle;

    5. public class MainActivity extends Activity {
    6.    /** Called when the activity is first created. */
    7.    @Override
    8.    public void onCreate(Bundle savedInstanceState) {
    9.      super.onCreate(savedInstanceState);
    10.      setContentView(R.layout.main);
    11.    }
    12. }
    (4) Fragment2.java文件中添加如下所示的粗体代码:
    1. package net.learn2develop.Fragments;

    2. import android.app.Fragment;
    3. import android.os.Bundle;
    4. import android.view.LayoutInflater;
    5. import android.view.View;
    6. import android.view.ViewGroup;
    7. import android.widget.Button;
    8. import android.widget.TextView;
    9. import android.widget.Toast;

    10. public class Fragment2 extends Fragment {
    11.    @Override
    12.    public View onCreateView(LayoutInflater inflater,
    13.    ViewGroup container, Bundle savedInstanceState) {
    14.      // Inflate the layout for this fragment
    15.      return Inflater.inflate(R.layout.fragment2,
    16.         container, false);
    17.    }
    18.    
    19.    @Override
    20.    public void onStart() {
    21.      super.onStart();
    22.      //---Button view---
    23.      Button btnGetText = (Button)
    24.        getActivity().findViewById(R.id.btnGetText);
    25.      btnGetText.setOnClickListener(new View.OnClickListener() {
    26.        public void onClick(View v) {
    27.          TextView lbl = (TextView)
    28.            getActivity().findViewById(R.id.lblFragment1);
    29.          Toast.makeText(getActivity(), lbl.getText(),
    30.            Toast.LENGTH_SHORT).show();
    31.        }
    32.      });
    33.    }
    34. }
    (5) 将这两个Fragment放到main.xml中:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3.    xmlns:android="http://schemas.android.com/apk/res/android"
    4.    android:orientation="horizontal"
    5.    android:layout_width="match_parent"
    6.    android:layout_height="match_parent">
    7.    <fragment
    8.      android:name="net.learn2develop.Fragments.Fragment1"
    9.      android:id="@+id/fragment1"
    10.      android:layout_weight="1"
    11.      android:layout_width="0px"
    12.      android:layout_height="match_parent" />
    13.    <fragment
    14.      android:name="net.learn2develop.Fragments.Fragment2"
    15.      android:id="@+id/fragment2"
    16.      android:layout_weight="1"
    17.      android:layout_width="0px"
    18.      android:layout_height="match_parent" />
    19. </LinearLayout>
    (6) 按下F11键在Android模拟器上调试应用。在右边第2个Fragment中单击按钮,将会看到Toast类显示出文本This is fragment #1(如图2-13所示)

    示例说明

    由于Fragment被嵌入到了活动中,因此可以通过getActivity()方法获取Fragment当前所嵌入到的活动,然后使用findViewById()方法找到Fragment中包含的视图:
    1.  TextView lbl = (TextView)
    2.        getActivity().findViewById(R.id.lblFragment1);
    3.      Toast.makeText(getActivity(), lbl.getText(),
    4.  Toast.LENGTH_SHORT).show();
  • 相关阅读:
    Oracle Dataguard原理
    [转]TOKUDB® VS. INNODB FLASH MEMORY
    [转]什么是简约设计
    [转]DAS、NAS、SAN存储系统分析
    [转]ocp|ocm考证系列文章!
    [转]数据库范式的设计
    Block Media Recovery, BMR
    [转]开启闪回以及闪回的四种原理
    [转]Oracle DB 执行表空间时间点恢复
    Losing All Members of an Online Redo Log Group
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3254430.html
Copyright © 2011-2022 走看看