2013-03-16 130 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架构,它也提供了解耦。