Ecco:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#AAFFFF"
android:orientation="horizontal"
android:id="@+id/container"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/barra"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"/>
<LinearLayout
android:id="@+id/display"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity implements Frammento.OnFragmentInteractionListener{
Frammento myFrag;
FragmentManager fragmentManager;
FragmentTransaction transaction;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFrag=new Frammento();
fragmentManager=getSupportFragmentManager();
transaction=fragmentManager.beginTransaction();
transaction.add(R.id.barra,myFrag,"barra");
transaction.commit();
Frammento2 frag2=new Frammento2();
transaction=fragmentManager.beginTransaction();
transaction.add(R.id.display,frag2,"display");
transaction.commit();
}
public void onFragmentInteraction(){
Frammento2 frammento2=(Frammento2)fragmentManager.findFragmentByTag("display");
Button bottone=new Button(getApplicationContext());
LinearLayout mainLayout=(LinearLayout)frammento2.getView().findViewById(R.id.frammento2);
mainLayout.addView(bottone);
}
}
Fragments:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:id="@+id/frammento">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2" />
</ScrollView>
public class Frammento extends Fragment {
OnFragmentInteractionListener mListener;
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.frammento, container, false);
button=(Button)rootView.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mListener.onFragmentInteraction();
}
});
return rootView;
}
@Override
public void onAttach(Context context){
super.onAttach(context);
if(context instanceof OnFragmentInteractionListener){
mListener=(OnFragmentInteractionListener)context;
}
}
public interface OnFragmentInteractionListener{
public void onFragmentInteraction();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:id="@+id/frammento2"
android:background="#FAA">
<TextView
android:text="SECONDO FRAMMENTO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</LinearLayout>
public class Frammento2 extends Fragment {
OnFragmentInteractionListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v=inflater.inflate(R.layout.frammento2,container,false);
return v;
}
@Override
public void onAttach(Context context){
super.onAttach(context);
if(context instanceof OnFragmentInteractionListener){
mListener=(OnFragmentInteractionListener)context;
}
System.out.println("FRAMMENTO 2 ONATTACH");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#AFA"
android:layout_weight="4">
<TextView
android:text="TERZO FRAMMENTO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button4" />
</LinearLayout>
public class Frammento3 extends Fragment {
OnFragmentInteractionListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v=inflater.inflate(R.layout.frammento3,container,false);
return v;
}
@Override
public void onAttach(Context context){
super.onAttach(context);
if(context instanceof OnFragmentInteractionListener){
mListener=(OnFragmentInteractionListener)context;
}
}
}
Nessun commento:
Posta un commento