Bu dersimizde app bar layoutun ne olduğundan ve ne işe yaradığından bahsetmeye çalışıcam.
App bar layout (Ana parent olarak Coordinator Layout kullanıldığında) toolbarın ve içindeki view öğelerinin sibling view dediğimiz (Recyclerview, Nested Scroll View) yapıların scroll hareketine yani yukarı aşağı doğru olan hareketlerine tepki vermesini sağlayan yapıdır.
Bu yapıyı kullanırken, scroll flaglerden yardım alırız. Bu flag dediğimiz kontrol değişkenleri sayesinde app bar layoutumuzun scroll hareketinde nasıl davranması gerektiğini belkirtebiliriz.
Ayrıca bu flagleri kullanırken sadece bir tane değil birleştirerek de kullanabiliriz.
Burda dikkat edilmesi gereken şey Ana viewgroup olarak CoordinatorLayout kullanmak ve toolbarı ve içindeki viewları appbarlayoutun içine yazmak. Daha sonra Toolbar’a scroll flagler ekleyerek istediğimiz etkiyi yaratabiliriz. Ayrıca scroll hareketine göre tepki alacağımız viewların da nested scroll view veya recyclerview olması gerekiyor. Bu öğelerin içine de düzgün çalısması için app:layout_behavior=”@string/appbar_scrolling_view_behavior” yazmamız gerekiyor.
Tüm projeyi indirmek için burayı tıklayın….
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("App Bar Layout"); toolbar.inflateMenu(R.menu.menu_main); } } |
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 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/edit" android:orderInCategory="100" android:title="Edit" app:showAsAction="never"/> <item android:id="@+id/discard" android:orderInCategory="100" android:title="Delete" app:showAsAction="never"/> <item android:id="@+id/search" android:orderInCategory="100" android:title="Search" app:showAsAction="never"/> <item android:id="@+id/Exit" android:orderInCategory="100" android:title="Exit" app:showAsAction="never"/> <item android:id="@+id/settings" android:orderInCategory="100" android:title="Settings" app:showAsAction="never"/> </menu> |
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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingTop="10dp"> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> <include layout="@layout/tek_satir"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> |
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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:elevation="5dp" app:cardCornerRadius="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Saçma Başlık" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@string/uzun_yazi" android:textColor="@android:color/black" android:textSize="15sp"/> </LinearLayout> </android.support.v7.widget.CardView> |
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 |
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:elevation="7dp" app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </android.support.design.widget.AppBarLayout> <include layout="@layout/nested_scroll_view" /> </android.support.design.widget.CoordinatorLayout> |