2013-03-16 8 views
5

Tôi đang ở cấp độ mới bắt đầu trong lập trình Android, vì vậy tôi cần sự giúp đỡ chân thành của bạn cho việc này. Bất cứ ai cũng giúp tôi.Giao tiếp Fragment-Fragment trong Android

Tôi cố gắng để xây dựng một giao diện người dùng TRƯỢT sử dụng mảnh

nên nghi ngờ thực tế của tôi là ... Tôi có một Fragment (nói Fragment A) nó có một TextViewButton trong đó và Fragment khác (nói Fragment B). Nó có một TextView trong đó. Tôi cần hiển thị nội dung của Fragment A trong số TextView trong sốB của TextView, khi tôi nhấn Button trong FRAGMENT A. Tôi đã thử nhiều cách, rất tiếc là tôi không có được kết quả phù hợp. Tôi chắc chắn rằng các bạn biết về nó. Làm ơn giúp tôi.

Cảm ơn bạn

+0

Bạn nên lưu trữ văn bản trong Hoạt động gốc và chuyển nó vào FRAGMENT B khi bạn tải nó. – dmaxi

+0

@dmaxi Nhưng tôi không biết cách lưu trữ văn bản trong hoạt động gốc – sam

+0

Tạo biến thành viên kiểu chuỗi và đừng quên lưu nó vào Nhóm trong cuộc gọi onSaveInstanceState(). Bạn cũng nên đọc JavaDoc của Hoạt động và Phân đoạn. – dmaxi

Trả lời

14

Nó nên được thực hiện nghĩ người nghe, vì vậy Những mảnh vỡ vẫn không phụ thuộc vào nhau và có thể được sử dụng trong một hoặc hai chế độ cửa sổ. Hoạt động sẽ xử lý người nghe của cả hai phần.

Dưới đây là một ví dụ về hoạt động với hai mảnh vỡ:

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); 
      } 
     }); 
    } 

} 

Đây là Fragment A, có người nghe tùy chỉnh cho sự kiện thay đổi văn bản.

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; 
    } 
} 

Đây là Fragment B có phương pháp nào để cập nhật trường văn bản:

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 Layout:

<?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> 

Fragment Một layout 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ố cục phân đoạn 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

hi, cảm ơn câu trả lời của bạn .. nhưng điều này không làm việc cho tôi .. Và một điều nữa tôi không có hai đoạn riêng biệt bố trí trong hoạt động ab (ofcourse tôi có hai phân đoạn lớp và bố trí tương ứng xml file) ... thay vào đó tôi chỉ có một mảnh bố trí và iam động thay đổi nội dung .. bạn có thể thử lại một lần nữa cho tôi .. – sam

+0

Phương thức 'onTextChange' sẽ được gọi trong khi nhấn nút. Vì vậy, hãy sử dụng gọi lại này để thay thế đoạn A bằng đoạn B và cũng đặt văn bản bạn nhận được, ví dụ: 'fragmentB.updateTextValue (newText); this.showFragmentB(); ' – andrew

+0

Lời khuyên này không áp dụng trong trường hợp chung. Còn nếu bạn đang sử dụng phân đoạn phụ? Một đoạn cha và mảnh con cũng có thể cần phải giao tiếp với nhau, mà không cần phải đi tất cả các con đường lên đến hoạt động có chứa để làm như vậy. Trong một số trường hợp, hoạt động gốc có thể chứa toàn bộ ứng dụng (ví dụ: ứng dụng dựa trên tab). Không có cách nào mà mọi giao tiếp đơn lẻ giữa mỗi thành phần phụ nên được định tuyến qua một nơi (vi phạm chính đối với mô hình chống chịu trách nhiệm đơn lẻ) – Marchy

0

Truyền thông giữa hai mảnh nên luôn luôn thông qua hoạt động mảnh của họ để giữ cho một khớp nối lỏng lẻo giữa các mảnh vỡ, Vì vậy, nếu bạn muốn gửi một số dữ liệu từ một đoạn A đến một đoạn B bạn có thể tạo người nghe và Bạn có thể kích hoạt sự kiện bằng cách sử dụng trình lắng nghe đó và gửi dữ liệu đến hoạt động phân mảnh, Vì vậy, bây giờ hoạt động phân đoạn sẽ có đối tượng của đoạn B cũng như hoạt động phân đoạn có thể gọi trực tiếp phương thức của đoạn B và gửi dữ liệu đến đoạn B ...

2

Nghe có vẻ như nếu thiết lập của bạn có thể là một cái gì đó giống như - MainActivity w/FragmentA [thêm] và FragmentB [thêm]. Trong trường hợp này, tôi sẽ ủy nhiệm các thông điệp/hành động giữa hai đoạn thông qua MainActivity. Việc cho phép MainActivity xử lý các thông điệp/hành động cũng tạo điều kiện cho các dàn nhạc 'đặc biệt' có thể cần thiết trong một tình huống phức tạp hơn giữa các mảnh vỡ.Ví dụ:

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); 
    } 
} 

Tôi quên đăng phần gửi FragmentA. Hy vọng điều này sẽ hữu ích.

0

developer.android.com

Để cho phép một Fragment để giao tiếp lên đến Hoạt động của nó, bạn có thể xác định một giao diện trong lớp Fragment và thực hiện nó trong Hoạt động . The Fragment chụp thực hiện giao diện trong phương thức vòng đời onAttach() của nó và sau đó có thể gọi các phương thức Giao diện để giao tiếp với Hoạt động.

Ví dụ: - http://wiki.workassis.com/android-fragment-communication/

ref: - https://developer.android.com/training/basics/fragments/communicating.html

-1

của nó thực sự khá dễ dàng, chỉ cần làm theo các bước sau:

  1. Vì vậy, bạn đã tạo ra hai mảnh A và B, bây giờ tạo ra bộ não của họ, tức là, các tệp lớp Java. Cho phép gọi họ là JClass A (đối với Fragment A) và JClass B (cho Fragment B)

2.Bạn có nút trong bạn Frag A, hãy chuyển đến JClass A, và trong đó nhận được văn bản String người dùng và sự kiện nhấn nút như thế này: // chỉ cần ghi đè lên phương thức 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; 
} 
  1. Và cuối cùng trong bạn JClass B (java file cho đoạn B):

    public class BottomSectionFrag kéo dài Fragment {

    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); 
    
    } 
    

    }

Thì đấy !!

P.S. Đây là bài đăng đầu tiên của tôi trên stackOverFlow: D

Hòa bình.

0

tôi thích hoạt động trở thành người đàn ông trung gian giữa giao đoạn từ phân đoạn đến đoạn. nhưng điều khác tôi thích là sử dụng một kiến ​​trúc event bus mà cũng cung cấp tách.