Guest User

Untitled

a guest
Jun 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".activities.MainActivity">
  7.  
  8. <android.support.design.widget.AppBarLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="@dimen/app_bar_height"
  11. android:theme="@style/AppTheme.AppBarOverlay"
  12. android:fitsSystemWindows="false">
  13.  
  14. <com.example.study.helpers.NoAnimCollapsibleConstraintLayout
  15. xmlns:android="http://schemas.android.com/apk/res/android"
  16. xmlns:app="http://schemas.android.com/apk/res-auto"
  17. xmlns:tools="http://schemas.android.com/tools"
  18. android:id="@+id/root"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:fitsSystemWindows="true"
  22. android:minHeight="40dp"
  23. app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"/>
  24.  
  25. </android.support.design.widget.AppBarLayout>
  26.  
  27. <include layout="@layout/content_main" />
  28.  
  29. <android.support.design.widget.FloatingActionButton
  30. android:id="@+id/fab"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_gravity="bottom|end"
  34. android:layout_margin="@dimen/fab_margin"
  35. android:src="@drawable/ic_border_color_white_24dp"/>
  36.  
  37. </android.support.design.widget.CoordinatorLayout>
  38.  
  39. <?xml version="1.0" encoding="utf-8"?>
  40. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  41. xmlns:app="http://schemas.android.com/apk/res-auto"
  42. xmlns:tools="http://schemas.android.com/tools"
  43. android:layout_width="match_parent"
  44. android:layout_height="match_parent"
  45. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  46. tools:context=".activities.MainActivity"
  47. tools:showIn="@layout/activity_main">
  48.  
  49.  
  50. <TextView
  51. android:id="@+id/textView3"
  52. android:layout_width="0dp"
  53. android:layout_height="0dp"
  54. android:layout_marginBottom="8dp"
  55. android:layout_marginEnd="8dp"
  56. android:layout_marginLeft="8dp"
  57. android:layout_marginRight="8dp"
  58. android:layout_marginStart="8dp"
  59. android:layout_marginTop="8dp"
  60. android:text="@string/some_long_text"
  61. app:layout_constraintBottom_toBottomOf="parent"
  62. app:layout_constraintEnd_toEndOf="parent"
  63. app:layout_constraintStart_toStartOf="parent"
  64. app:layout_constraintTop_toTopOf="parent" />
  65. </android.support.constraint.ConstraintLayout>
  66.  
  67. import android.content.Context;
  68. import android.support.constraint.ConstraintLayout;
  69. import android.support.constraint.ConstraintSet;
  70. import android.support.design.widget.AppBarLayout;
  71. import android.util.AttributeSet;
  72. import android.util.Log;
  73. import android.widget.Toast;
  74. import com.example.study.R;
  75.  
  76. public class NoAnimCollapsibleConstraintLayout extends ConstraintLayout implements AppBarLayout.OnOffsetChangedListener {
  77.  
  78. private static final String TAG = NoAnimCollapsibleConstraintLayout.class.getSimpleName();
  79. private float mTransitionThreshold = 0.35f;
  80. private int mLastPosition = 0;
  81. private boolean mToolbarOpen = true;
  82.  
  83. private ConstraintSet mOpenToolbarSet = new ConstraintSet();
  84. private ConstraintSet mCloseToolbarSet = new ConstraintSet();
  85.  
  86. public NoAnimCollapsibleConstraintLayout(Context context) { super(context, null); }
  87.  
  88. public NoAnimCollapsibleConstraintLayout(Context context, AttributeSet attrs) { super(context, attrs, 0); }
  89.  
  90. public NoAnimCollapsibleConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
  91.  
  92. @Override
  93. protected void onAttachedToWindow() {
  94. super.onAttachedToWindow();
  95. if (getParent() instanceof AppBarLayout){
  96. Log.d(TAG, "Instance AppBarLayout");
  97. AppBarLayout appBarLayout = (AppBarLayout)getParent();
  98. appBarLayout.addOnOffsetChangedListener(this);
  99. mOpenToolbarSet.clone(getContext(), R.layout.open);
  100. mCloseToolbarSet.clone(getContext(), R.layout.close);
  101.  
  102. }else {
  103. Log.d(TAG, "Instance if Not Parent");
  104. }
  105. }
  106.  
  107. @Override
  108. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  109. Log.d(TAG, "onOffsetChanged");
  110. if (mLastPosition == verticalOffset){
  111. return;
  112. }
  113.  
  114. mLastPosition = verticalOffset;
  115. Float progress = ( Math.abs(verticalOffset / (float) (appBarLayout.getHeight()))) ;
  116.  
  117. AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) getLayoutParams();
  118. params.topMargin = -verticalOffset;
  119. setLayoutParams(params);
  120.  
  121. if ( mToolbarOpen && progress > mTransitionThreshold) {
  122. Log.d(TAG, "Apply close.xml");
  123. mCloseToolbarSet.applyTo(this);
  124. mToolbarOpen = false;
  125. }else if ( !mToolbarOpen && progress < mTransitionThreshold){
  126. Log.d(TAG, "Apply open.xml");
  127. mOpenToolbarSet.applyTo(this);
  128. mToolbarOpen = true;
  129.  
  130. }
  131. }
  132. }
  133.  
  134. <?xml version="1.0" encoding="utf-8"?>
  135. <android.support.constraint.ConstraintLayout
  136. xmlns:android="http://schemas.android.com/apk/res/android"
  137. xmlns:app="http://schemas.android.com/apk/res-auto"
  138. xmlns:tools="http://schemas.android.com/tools"
  139. android:id="@+id/open"
  140. android:layout_width="match_parent"
  141. android:layout_height="match_parent">
  142.  
  143. <ImageView
  144. android:id="@+id/imageView2"
  145. android:layout_width="0dp"
  146. android:layout_height="0dp"
  147. android:layout_marginBottom="8dp"
  148. android:layout_marginEnd="8dp"
  149. android:layout_marginLeft="8dp"
  150. android:layout_marginRight="8dp"
  151. android:layout_marginStart="8dp"
  152. android:layout_marginTop="8dp"
  153. android:contentDescription="@string/app_name"
  154. app:layout_constraintBottom_toBottomOf="parent"
  155. app:layout_constraintEnd_toEndOf="parent"
  156. app:layout_constraintStart_toStartOf="parent"
  157. app:layout_constraintTop_toTopOf="parent"
  158. app:srcCompat="@drawable/study" />
  159.  
  160. </android.support.constraint.ConstraintLayout>
  161.  
  162. <?xml version="1.0" encoding="utf-8"?>
  163. <android.support.constraint.ConstraintLayout
  164. xmlns:android="http://schemas.android.com/apk/res/android"
  165. xmlns:app="http://schemas.android.com/apk/res-auto"
  166. xmlns:tools="http://schemas.android.com/tools"
  167. android:id="@+id/close"
  168. android:layout_width="match_parent"
  169. android:layout_height="match_parent">
  170.  
  171.  
  172. <TextView
  173. android:id="@+id/textView4"
  174. android:layout_width="0dp"
  175. android:layout_height="30dp"
  176. android:layout_marginEnd="8dp"
  177. android:layout_marginLeft="8dp"
  178. android:layout_marginRight="8dp"
  179. android:layout_marginStart="8dp"
  180. android:layout_marginTop="8dp"
  181. android:text="@string/app_name"
  182. android:textColor="@android:color/white"
  183. android:textSize="@dimen/text_size_16"
  184. app:layout_constraintEnd_toEndOf="parent"
  185. app:layout_constraintStart_toStartOf="parent"
  186. app:layout_constraintTop_toTopOf="parent" />
  187. </android.support.constraint.ConstraintLayout>
Add Comment
Please, Sign In to add comment