Bu derste fragmentler kullanarak kullanıcı dostu arayüze sahip basit bir listview örneği yaptım.
Örnek biraz uzun oldugundan iki bölümde incelemeye calıstık.
Bu örnekte kullanıcı eğer portrait mod’da ise farklı landscape modda ise farklı arayüzler kullanarak uygulamamızın güzel bir arayüze sahip olmasını sağladık.
Örneğin bitmiş halinin kodları şu şekilde:
1 2 3 4 5 |
public interface MyListener { void sendData(int position); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?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.fragmentflexibleui.MainActivity"> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.emrealtunbilek.fragmentflexibleui.FragmentKonu" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:id="@+id/fragmentKonu" android:layout_alignParentEnd="true" tools:layout="@layout/fragment_konu" /> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:layout_width="0dp" android:layout_height="match_parent" android:name="com.example.emrealtunbilek.fragmentflexibleui.FragmentKonu" android:id="@+id/fragmentKonu" android:layout_weight="2" android:layout_margin="10dp" tools:layout="@layout/fragment_konu" /> <fragment android:layout_width="0dp" android:layout_height="match_parent" android:name="com.example.emrealtunbilek.fragmentflexibleui.FragmentDetay" android:id="@+id/fragmentDetay" android:layout_weight="3" tools:layout="@layout/fragment_aciklama" /> </LinearLayout> |
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 |
public class MainActivity extends AppCompatActivity implements MyListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void sendData(int position) { FragmentDetay fragmentDetay= (FragmentDetay) getFragmentManager().findFragmentById(R.id.fragmentDetay); if(fragmentDetay!=null && fragmentDetay.isVisible()) { fragmentDetay.changeKonu(position); }else { Intent i=new Intent(this,DigerActivity.class); i.putExtra("position", position); startActivity(i); } } } |
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 |
public class FragmentKonu extends Fragment implements AdapterView.OnItemClickListener{ ListView konuListesi; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_konu, container, false); konuListesi= (ListView) v.findViewById(R.id.konuListe); ArrayAdapter adapter=ArrayAdapter.createFromResource(getActivity(),R.array.konular, android.R.layout.simple_list_item_1); konuListesi.setAdapter(adapter); konuListesi.setOnItemClickListener(this); return v; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyListener listener= (MyListener) getActivity(); listener.sendData(position); } } |
1 2 3 4 5 6 7 8 9 10 11 |
<?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"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/konuListe" /> </LinearLayout> |
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 |
public class FragmentDetay extends Fragment { TextView aciklama; String[] detaylar; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_aciklama, container, false); aciklama = (TextView) v.findViewById(R.id.tvAciklama); detaylar=getResources().getStringArray(R.array.aciklamalar); aciklama.setText(detaylar[0]); return v; } public void setData(int position) { Toast.makeText(getActivity(), "tıklanan : "+position+ " :"+detaylar[position], Toast.LENGTH_LONG).show(); aciklama.setText(detaylar[position]); } public void changeKonu(int position) { String[] detaylar=getResources().getStringArray(R.array.aciklamalar); aciklama.setText(detaylar[position]); } } |
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"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scrollView"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="20sp" android:text="" android:id="@+id/tvAciklama" /> </ScrollView> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class DigerActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_diger); Intent intent=getIntent(); int pos=intent.getIntExtra("position",0); FragmentDetay fragmentDetay= (FragmentDetay) getFragmentManager().findFragmentById(R.id.fragmentDigerDetay); fragmentDetay.changeKonu(pos); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="#0ce" android:orientation="vertical" android:padding="10dp" android:weightSum="1"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.emrealtunbilek.fragmentflexibleui.FragmentDetay" android:id="@+id/fragmentDigerDetay" tools:layout="@layout/fragment_aciklama" /> </LinearLayout> |