Cas d'utilisation que j'essaye d'implémenter ::
Chaque fragment a un widget unique
my_fragment1
a edittext
my_fragment2
a button
my_fragment3
a TextView
button
, le texte du edittext
doit être affiché dans le textview
Ce que j'ai essayé jusqu'à présent, j'ai construit la plupart du scénario ci-dessous
Top_Fragment.Java
public class Top_Fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.my_fragment1, container, false);
return view;
}
}
Middle_Fragment.Java
package com.example.deleteme;
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;
public class Middle_Fragment extends Fragment{
View view;
Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment2, container, false);
btn=(Button) view.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return view;
}
}
Bottom_Fragment.Java
public class Bottom_Fragment extends Fragment{
View view;
TextView display_text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment3, container,false);
display_text=(TextView) view.findViewById(R.id.editText1);
return view;
}
public void setName(String Name){
display_text.setText("Result::" + Name);
}
}
MainActivity.Java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Top_Fragment frg=new Top_Fragment();//create the fragment instance for the top fragment
Middle_Fragment frg1=new Middle_Fragment();//create the fragment instance for the middle fragment
Bottom_Fragment frg2=new Bottom_Fragment();//create the fragment instance for the bottom fragment
FragmentManager manager=getSupportFragmentManager();//create an instance of fragment manager
FragmentTransaction transaction=manager.beginTransaction();//create an instance of Fragment-transaction
transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
transaction.commit();
}
}
activity_main.xml
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
tools:context=".MainActivity"
Android:background="@color/black">
<FrameLayout
Android:id="@+id/My_Container_1_ID"
Android:layout_width="fill_parent"
Android:layout_height="150dp"
Android:background="@color/yellow">
</FrameLayout>
<FrameLayout
Android:id="@+id/My_Container_2_ID"
Android:layout_width="fill_parent"
Android:layout_height="150dp"
Android:layout_alignParentLeft="true"
Android:layout_below="@+id/My_Container_1_ID"
Android:background="@color/Orange" >
</FrameLayout>
<FrameLayout
Android:id="@+id/My_Container_3_ID"
Android:layout_width="fill_parent"
Android:layout_height="150dp"
Android:layout_alignParentLeft="true"
Android:layout_below="@+id/My_Container_2_ID"
Android:background="@color/purple" >
</FrameLayout>
</RelativeLayout>
my_fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:background="@color/green" >
<EditText
Android:id="@+id/editText1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerHorizontal="true"
Android:layout_centerVertical="true"
Android:ems="10"
Android:textColor="#000000"
Android:singleLine="true" >
<requestFocus />
</EditText>
</RelativeLayout>
my_fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical"
Android:background="@color/pink">
<Button
Android:id="@+id/button1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerHorizontal="true"
Android:layout_centerVertical="true"
Android:background="@color/black"
Android:text="Button"
Android:textColor="#FFFFFF" />
</RelativeLayout>
my_fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
<TextView
Android:id="@+id/textView1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerHorizontal="true"
Android:layout_centerVertical="true"
Android:text="TextView"
Android:textColor="#000000"
Android:textSize="30dp" />
</RelativeLayout>
Ma sortie est comme ci-dessous ::
Ce que j'ai du mal à réaliser ::
edit text
à textview
en cliquant sur le button
Des idées?
Toutes les communications de fragment à fragment se font par le biais de l'activité associée. Deux fragments ne doivent jamais communiquer directement.
http://developer.Android.com/training/basics/fragments/communicating.html
test.Java
// dans votre cas, son MainActivity
public class test extends FragmentActivity implements textEntered {
String value;
boolean check = false;
BottomFragment frg2;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Top_Fragment frg = new Top_Fragment();
frg2 = new BottomFragment();
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
transaction.commit();
}
@Override
public void setValue(String editextvalue) {
value = editextvalue;
if (frg2 != null) {
frg2.setName(value);
} else {
Toast.makeText(getApplicationContext(), "fragment 2 is null", 1000).show();
}
}
}
Top_Fragment.Java
public class Top_Fragment extends Fragment {
textEntered mCallback;
Button b;
EditText ed;
public interface textEntered {
public void setValue(String editextvalue);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.my_fragment1, container, false);
ed = (EditText) view.findViewById(R.id.editText1);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
b = (Button) getView().findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = ed.getText().toString();
mCallback.setValue(s);
}
});
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (textEntered) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() +
" must implement textEntered");
}
}
}
my_fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" >
<EditText
Android:id="@+id/editText1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerHorizontal="true"
Android:layout_centerVertical="true"
Android:ems="10"
Android:textColor="#000000"
Android:singleLine="true" >
<requestFocus />
</EditText>
<Button
Android:id="@+id/button1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@+id/editText1"
Android:layout_centerHorizontal="true"
Android:layout_marginTop="20dp"
Android:text="Button" />
</RelativeLayout>
Remplacer par
display_text=(TextView) view.findViewById(R.id.textView1);
// id is textView 1 not editText1
dans BottomFragment
casser
Communication entre les fragments
Il peut y avoir de nombreux scénarios où la communication entre les fragments est requise. Vous devez transmettre des données entre les fragments lors d'un événement de clic de bouton. Vous pouvez également utiliser Android barre d'outils pour basculer entre les fragments. Lorsque vous ajoutez des boutons à votre barre d'outils, vous devez modifier dynamiquement l'écran à l'aide de fragment.
Créer une interface qui nous aidera à communiquer
Communicate.Java
package com.example.amaanmemon.testfragment;
interface Communicate {
public void sendData();
}
TopFragment.Java
package com.example.amaanmemon.testfragment;
import Android.support.v4.app.Fragment;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.EditText;
public class TopFragment extends Fragment {
EditText firstName;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.my_fragment1, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
firstName = (EditText) getActivity().findViewById(R.id.editText1);
}
public String getData(){
return firstName.getText().toString();
}
}
MiddleFragment.Java
package com.example.amaanmemon.testfragment;
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;
public class MiddleFragment extends Fragment implements OnClickListener{
View view;
Button btn;
Communicate cm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment2, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
cm = (Communicate) getActivity();
btn = (Button) getActivity().findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
cm.sendData();
}
}
BottomFragment.Java
package com.example.amaanmemon.testfragment;
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;
public class BottomFragment extends Fragment{
int count;
View view;
TextView display_text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment3, container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
display_text = (TextView)getActivity().findViewById(R.id.textView1);
}
public void incrementData(String displayText){
display_text.setText(displayText);
}
}
MainActivity.Java
package com.example.amaanmemon.testfragment;
import Android.support.v4.app.FragmentActivity;
import Android.os.Bundle;
import Android.support.v4.app.FragmentManager;
import Android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity implements Communicate{
TopFragment frg;
MiddleFragment frg1;
BottomFragment frg2;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frg = new TopFragment();
frg1 = new MiddleFragment();
frg2 = new BottomFragment();
FragmentManager manager=getSupportFragmentManager();
transaction=manager.beginTransaction();
transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
transaction.commit();
}
@Override
public void sendData() {
String temp = frg.getData();
frg2.incrementData(temp);
}
}
Vous pouvez copier des fichiers xml à partir de la question. vous pouvez regarder la sortie ci-dessous.
Vous pouvez utiliser l'activité pour cela.
dans le onClick du fragment du bas, vous pouvez faire quelque chose comme
((MainActivity) getActivity()).doIt();
Et faites une méthode doIt
dans votre MainActivity
peut-être quelque chose comme ça
public void doIt(){
frg2.setName(frg.getText())
}
et dans le fragment supérieur créez une méthode getText
qui retourne le texte de EditText