2013-03-16 129 views
5

我在Android編程方面處於初級水平,所以我需要您的真誠幫助。任何人都可以幫助我。Android中的片段 - 片段通信

我試圖用片段

建立滑動UI

所以我的實際疑問是...我有一個Fragment(說Fragment A)它有一個TextView和它Button和另一Fragment(說Fragment乙)。它有一個TextView在裏面。我需要顯示Fragment ATextView的內容Fragment B的TextView,當我按Button FRAGMENT A 我嘗試了很多方法,不幸的是我沒有得到正確的輸出。 我相信你們的人們都知道這件事。請幫幫我。

謝謝

+0

您應該將文本存儲在父Activity中,並在加載它時將其傳遞給FRAGMENT B. – dmaxi 2013-03-16 10:02:38

+0

@dmaxi但我不知道如何在父Activity中存儲文本 – sam 2013-03-16 10:05:35

+0

創建一個String類型的成員變量,不要忘記將其保存到Bundle中的onSaveInstanceState()回調中。您還應該閱讀Activity和Fragment的JavaDoc。 – dmaxi 2013-03-16 10:17:19

回答

14

它應該做的事想監聽,所以碎片仍然沒有互相依賴,並且可以在一個或兩個窗格模式下使用。活動應處理這兩個片段的偵聽器。

這裏是活動的兩個片段的例子:

package com.example; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 

import com.example.fragment.FragmentA; 
import com.example.fragment.FragmentA.TextChangeListener; 
import com.example.fragment.FragmentB; 

public class ActivityAB extends FragmentActivity { 

    FragmentA fragmentA; 
    FragmentB fragmentB; 

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

     FragmentManager manager = getSupportFragmentManager(); 
     fragmentA = (FragmentA) manager.findFragmentById(R.id.fragmentA); 
     fragmentB = (FragmentB) manager.findFragmentById(R.id.fragmentB); 

     fragmentA.setTextChangeListener(new TextChangeListener() { 

      @Override 
      public void onTextChange(CharSequence newText) { 
       fragmentB.updateTextValue(newText); 
      } 
     }); 
    } 

} 

這裏是片段A,其具有用於文本改變事件自定義偵聽。

package com.example.fragment; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

import com.example.R; 

public class FragmentA extends Fragment { 

    TextChangeListener listener; 

    public interface TextChangeListener { 
     public void onTextChange(CharSequence newText); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_a, container, false); 

     Button btn = (Button) view.findViewById(R.id.button1); 
     final TextView textView = (TextView) view.findViewById(R.id.textView1); 

     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (null != listener) { 
        listener.onTextChange(textView.getText()); 
       } 

      } 
     }); 
     return view; 
    } 

    public void setTextChangeListener(TextChangeListener listener) { 
     this.listener = listener; 
    } 
} 

這裏是具有公共方法來更新文本字段片段B:

package com.example.fragment; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.example.R; 

public class FragmentB extends Fragment { 

    TextView textView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     textView = (TextView) view.findViewById(R.id.textView1); 
     return view; 
    } 

    public void updateTextValue(CharSequence newText) { 
     textView.setText(newText); 
    } 
} 

ActivityAB XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 

    <fragment 
     android:id="@+id/fragmentA" 
     android:name="com.example.fragment.FragmentA" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <fragment 
     android:id="@+id/fragmentB" 
     android:name="com.example.fragment.FragmentB" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

</LinearLayout> 

片段A的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Show" /> 

</LinearLayout> 

片段B xml佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="(here will be text)" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 
+0

嗨,謝謝你的回覆..但是這不適合我..還有一件事我沒有兩個單獨的片段佈局在活動ab(當然我有兩個片段類和相應的佈局xml文件)...而是我只有一個片段佈局和IAM動態地改變內容..可以請你再試一次我.. – sam 2013-03-16 12:41:54

+0

'onTextChange'方法將在點擊按鈕時調用。因此,使用此回調將片段A替換爲片段B,並設置您接收的文本,例如'fragmentB.updateTextValue(newText); this.showFragmentB();' – andrew 2013-03-16 13:10:02

+0

這個建議不適用於一般情況。如果你正在使用子片段呢?父級片段和子片段可能需要彼此進行通信,而無需一直到包含的活動完成。在某些情況下,根活動可能包含整個應用程序(即:基於選項卡的應用程序)。每一個子組件之間的每一次通信都不應該通過一個地方路由(主要違反單一責任反模式) – Marchy 2014-03-31 20:38:35

0

兩個片段之間的溝通應該總是通過,以保持片段之間的鬆耦合的片段的活性,所以,如果你想從一個片段一個發送一些數據,你可以創建監聽另一個片段B和在片段活動中實現它,並從fragmentA內部使用該偵聽器觸發事件​​並將數據發送到片段活動。現在片段活動也將具有片段B的對象,因此片段活動可以直接調用片段的方法B併發送數據到片段B ...

2

聽起來好像您的設置可能是這樣的 - MainActivity W/FragmentA [加]和FragmentB [添加]。對於這種情況,我會通過MainActivity委託兩個片段之間的消息/動作。允許MainActivity處理消息/操作也有助於在碎片之間更復雜的情況下可能需要的'特殊'編排。例如:

public interface FragmentCommunicator 
{ 
    public void sendMessage (String broadcastMessage); 
    public void takeAction (String name, int action, Fragment frag); 
} 

public class MainActivity extends Activity implements FragmentCommunicator 
{ 
    Fragment fragA; 
    Fragment fragB; 
    ... 
    @Override 
    public void sendMessage (String msg) 
    { 
     if (msg.Contains("Send To fragA")) 
      fragA.receiveMsg(msg); 
     ... 
    } 
    @Override 
    public void takeAction (String name, int action, Fragment frag) 
    { 
    if (action == CLOSE) 
      removeFragment(name, frag); 
     ... 
    } 
} 

public class FragmentA extends Fragment 
{ 
    ... 
@Override 
public void onClick(View v) 
{ 
    FragmentCommunicator inter = (FragmentCommunicator) getActivity(); 
    inter.sendMessage("the txt/information/etc you want to send to fragB");  
} 
} 

public class FragmentB extends Fragment 
{ 
    ... 
    public void receiveMsg (String msg) 
    { 
     textView.setText(msg); 
    } 
} 

我忘了張貼FragmentA的發送部分。 希望這有助於。

-1

它實際上很簡單,只要按照下列步驟操作:

  1. 所以你創建的兩個片段A和B,現在創建他們的大腦,即Java類文件。讓我們稱之爲JCLASS A(用於片段A)和JCLASS B(對於片段B)

2.You得到了在你破片的按鈕,所以去JCLASS A,並在獲取字符串文本的用戶類型和按鈕按下這樣的事件: //只是覆蓋onCreate方法。

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.<name of your fragment A.xml file>,container,false); 

    userInput = (EditText)view.findViewById(R.id.<Id that you gave to EditText field in which user enters text>); 

    Button myButton = (Button)view.findViewById(R.id.<id of your Button>); 
    myButton.setOnClickListener(
      new Button.OnClickListener(){ 
       public void onClick(View v){ 
        JClassB.setText(userInput.getText().toString());/*Replace JClassB with the java class of Fragment B*/ 

       } 
      } 
    ); 

    return view; 
} 
  • 最後在JCLASS B(對於B片段的java文件):

    公共類BottomSectionFrag延伸片段{

    private static TextView userText; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        View view = inflater.inflate(R.layout.fragmentB, container, false); 
        userText = (TextView)view.findViewById(R.id.userText); 
        return view; 
    } 
    public static void setText(String text){ 
        userText .setText(text); 
    
    } 
    

    }

  • 瞧!

    P.S.這是我在stackOverFlow上的第一篇文章:D

    和平。

    0

    我喜歡這個活動是片段到片段通信的中間人。但我喜歡的另一件事是使用event bus架構,它也提供瞭解耦。