Önceki derslerde fragmentler için veri alışverişinin nasıl olacağını adım adım anlatmıştım. Bu derste öğrendiğimiz tüm işlemleri sırasıyla kullanarak sil baştan bir uygulama yaptım.
Umarım aklınıza yatmayan kısımlar bu dersten sonra çözüme kavuşmuştur.
Bu örnek için yazılan kodlar aşağıdadır…
Umarım faydalı bir ders olmuştur…
1 2 3 4 |
public interface MyListener { void sayilariTopla(int sayi1, int sayi2); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public class MainActivity extends AppCompatActivity implements MyListener { FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager=getFragmentManager(); addFragmentA(); addFragmentB(); } private void addFragmentA() { FragmentA fragmentA=new FragmentA(); FragmentTransaction transaction=manager.beginTransaction(); transaction.add(R.id.containerA, fragmentA, "fragA"); transaction.commit(); } private void addFragmentB() { FragmentB fragmentB=new FragmentB(); FragmentTransaction transaction=manager.beginTransaction(); transaction.add(R.id.containerB, fragmentB, "fragB"); transaction.commit(); } @Override public void sayilariTopla(int sayi1, int sayi2) { FragmentB fragmentb= (FragmentB) manager.findFragmentByTag("fragB"); fragmentb.sayilariTopla(sayi1, sayi2); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.example.emrealtunbilek.fragmentverialisverisitekrar; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; public class FragmentA extends Fragment { EditText sayi1; EditText sayi2; Button sayilariGOnder; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v= inflater.inflate(R.layout.fragment_a, container, false); sayi1= (EditText) v.findViewById(R.id.etSayi1); sayi2= (EditText) v.findViewById(R.id.etSayi2); sayilariGOnder= (Button) v.findViewById(R.id.btnActivityeSayilariGonder); sayilariGOnder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendDatatoActivity(); } }); return v; } private void sendDatatoActivity() { int s1=Integer.parseInt(sayi1.getText().toString()) ; int s2=Integer.valueOf(sayi2.getText().toString()) ; MyListener mylistener = (MyListener) getActivity(); mylistener.sayilariTopla(s1, s2); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class FragmentB extends Fragment { TextView sonuc; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_b, container, false); sonuc= (TextView) v.findViewById(R.id.tvSonucFragment); return v; } public void sayilariTopla(int sayi1, int sayi2) { int toplam=sayi1+sayi2; sonuc.setText("TOPLAM = "+ toplam); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.emrealtunbilek.fragmentverialisverisitekrar.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/containerA" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/containerA" android:id="@+id/containerB" android:layout_centerHorizontal="true"> </FrameLayout> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?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" android:background="#0ec" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" android:inputType="number" android:id="@+id/etSayi1" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" android:inputType="number" android:id="@+id/etSayi2" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Activity'e Gönder" android:id="@+id/btnActivityeSayilariGonder" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#033" android:orientation="vertical"> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:id="@+id/tvSonucFragment" android:textColor="@android:color/white" android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> </RelativeLayout> |