यह सोचा था कि श्रोता किया जाना चाहिए ताकि टुकड़े अभी भी एक दूसरे पर निर्भर नहीं कर रहे हैं और एक या दो फलक मोड में इस्तेमाल किया जा सकता। गतिविधि दोनों टुकड़ों के श्रोताओं को संभालना चाहिए।
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);
}
});
}
}
यहाँ टुकड़ा ए, पाठ परिवर्तन ईवेंट के लिए कस्टम श्रोता है कि:
यहाँ दो टुकड़े के साथ गतिविधि का एक उदाहरण है।
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 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>
टुकड़ा एक 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>
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;
}
}
यहाँ टुकड़ा बी पाठ क्षेत्र अद्यतन करने के लिए सार्वजनिक विधि है कि टुकड़ा बी एक्सएमएल लेआउट:
<?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>
आपको टेक्स्ट को मूल गतिविधि में संग्रहीत करना चाहिए और इसे लोड करते समय इसे FRAGMENT B पर पास करना चाहिए। – dmaxi
@dmaxi लेकिन मुझे नहीं पता कि अभिभावक गतिविधि – sam
में टेक्स्ट को कैसे स्टोर किया जाए, एक स्ट्रिंग प्रकार सदस्य चर बनाएँ और इसे SaveInstanceState() कॉलबैक में बंडल में सहेजना न भूलें। आपको गतिविधि और टुकड़े के जावाडोक को भी पढ़ना चाहिए। – dmaxi