Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.15 KB | None | 0 0
  1. <android.support.design.widget.CoordinatorLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:fitsSystemWindows="true"
  8. tools:context="com.example.user.myapplication.ScrollingActivity">
  9.  
  10. <android.support.design.widget.AppBarLayout
  11. android:id="@+id/app_bar"
  12. android:layout_width="match_parent"
  13. android:layout_height="@dimen/app_bar_height"
  14. android:fitsSystemWindows="true"
  15. android:theme="@style/AppTheme.AppBarOverlay">
  16.  
  17. <android.support.design.widget.CollapsingToolbarLayout
  18. android:id="@+id/toolbar_layout"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:fitsSystemWindows="true"
  22. app:contentScrim="?attr/colorPrimary"
  23. app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
  24.  
  25. <android.support.v7.widget.Toolbar
  26. android:id="@+id/toolbar"
  27. android:layout_width="match_parent"
  28. android:layout_height="?attr/actionBarSize"
  29. app:layout_collapseMode="pin"
  30. app:popupTheme="@style/AppTheme.PopupOverlay"/>
  31.  
  32. </android.support.design.widget.CollapsingToolbarLayout>
  33. </android.support.design.widget.AppBarLayout>
  34.  
  35. <android.support.v7.widget.RecyclerView
  36. android:id="@+id/nestedView"
  37. android:layout_width="match_parent"
  38. android:layout_height="match_parent"
  39. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  40.  
  41. <LinearLayout
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:orientation="horizontal"
  45. app:layout_anchor="@id/app_bar"
  46. app:layout_anchorGravity="bottom|end">
  47.  
  48. <Button
  49. android:id="@+id/disableNestedScrollingButton"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="disable"/>
  53.  
  54. <Button
  55. android:id="@+id/enableNestedScrollingButton"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:text="enable"
  59. />
  60. </LinearLayout>
  61.  
  62. </android.support.design.widget.CoordinatorLayout>
  63.  
  64. public class ScrollingActivity extends AppCompatActivity {
  65.  
  66. @Override
  67. protected void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. setContentView(R.layout.activity_scrolling);
  70. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  71. setSupportActionBar(toolbar);
  72. final RecyclerView nestedView = (RecyclerView) findViewById(R.id.nestedView);
  73. findViewById(R.id.disableNestedScrollingButton).setOnClickListener(new OnClickListener() {
  74. @Override
  75. public void onClick(final View v) {
  76. nestedView.setNestedScrollingEnabled(false);
  77. }
  78. });
  79. findViewById(R.id.enableNestedScrollingButton).setOnClickListener(new OnClickListener() {
  80. @Override
  81. public void onClick(final View v) {
  82. nestedView.setNestedScrollingEnabled(true);
  83. }
  84. });
  85. nestedView.setLayoutManager(new LinearLayoutManager(this));
  86. nestedView.setAdapter(new Adapter() {
  87. @Override
  88. public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
  89. return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
  90. android.R.layout.simple_list_item_1,
  91. parent,
  92. false)) {
  93. };
  94. }
  95.  
  96. @Override
  97. public void onBindViewHolder(final ViewHolder holder, final int position) {
  98. ((TextView) holder.itemView.findViewById(android.R.id.text1)).setText("item " + position);
  99. }
  100.  
  101. @Override
  102. public int getItemCount() {
  103. return 100;
  104. }
  105. });
  106. }
  107.  
  108. }
  109.  
  110. <android.support.design.widget.CoordinatorLayout
  111. xmlns:android="http://schemas.android.com/apk/res/android"
  112. xmlns:app="http://schemas.android.com/apk/res-auto"
  113. xmlns:tools="http://schemas.android.com/tools"
  114. android:layout_width="match_parent"
  115. android:layout_height="match_parent"
  116. android:fitsSystemWindows="true"
  117. tools:context="com.example.user.myapplication.ScrollingActivity">
  118.  
  119. <android.support.design.widget.AppBarLayout
  120. android:id="@+id/app_bar"
  121. android:layout_width="match_parent"
  122. android:layout_height="@dimen/app_bar_height"
  123. android:fitsSystemWindows="true"
  124. android:theme="@style/AppTheme.AppBarOverlay">
  125.  
  126. <android.support.design.widget.CollapsingToolbarLayout
  127. android:id="@+id/toolbar_layout"
  128. android:layout_width="match_parent"
  129. android:layout_height="match_parent"
  130. android:fitsSystemWindows="true"
  131. app:contentScrim="?attr/colorPrimary"
  132. app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
  133.  
  134. <android.support.v7.widget.Toolbar
  135. android:id="@+id/toolbar"
  136. android:layout_width="match_parent"
  137. android:layout_height="?attr/actionBarSize"
  138. app:layout_collapseMode="pin"
  139. app:popupTheme="@style/AppTheme.PopupOverlay"/>
  140.  
  141. </android.support.design.widget.CollapsingToolbarLayout>
  142. </android.support.design.widget.AppBarLayout>
  143.  
  144. <android.support.v4.widget.NestedScrollView
  145. android:layout_width="match_parent"
  146. android:layout_height="match_parent"
  147. android:layout_gravity="fill_vertical"
  148. android:fillViewport="true"
  149. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  150.  
  151. <RelativeLayout
  152. android:layout_width="match_parent"
  153. android:layout_height="match_parent">
  154.  
  155. <android.support.v7.widget.RecyclerView
  156. android:id="@+id/nestedView"
  157. android:layout_width="match_parent"
  158. android:layout_height="match_parent"
  159. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  160.  
  161. </RelativeLayout>
  162.  
  163. </android.support.v4.widget.NestedScrollView>
  164.  
  165. <LinearLayout
  166. android:layout_width="wrap_content"
  167. android:layout_height="wrap_content"
  168. android:orientation="horizontal"
  169. app:layout_anchor="@id/app_bar"
  170. app:layout_anchorGravity="bottom|end">
  171.  
  172. <Button
  173. android:id="@+id/disableNestedScrollingButton"
  174. android:layout_width="wrap_content"
  175. android:layout_height="wrap_content"
  176. android:text="disable"/>
  177.  
  178. <Button
  179. android:id="@+id/enableNestedScrollingButton"
  180. android:layout_width="wrap_content"
  181. android:layout_height="wrap_content"
  182. android:text="enable"
  183. />
  184. </LinearLayout>
  185.  
  186. </android.support.design.widget.CoordinatorLayout>
  187.  
  188. <android.support.design.widget.CoordinatorLayout
  189. xmlns:android="http://schemas.android.com/apk/res/android"
  190. xmlns:app="http://schemas.android.com/apk/res-auto"
  191. xmlns:tools="http://schemas.android.com/tools"
  192. android:layout_width="match_parent"
  193. android:layout_height="match_parent"
  194. android:fitsSystemWindows="true"
  195. tools:context="com.example.user.myapplication.ScrollingActivity">
  196.  
  197. <android.support.design.widget.AppBarLayout
  198. android:id="@+id/app_bar"
  199. android:layout_width="match_parent"
  200. android:layout_height="@dimen/app_bar_height"
  201. android:fitsSystemWindows="true"
  202. android:theme="@style/AppTheme.AppBarOverlay">
  203.  
  204. <android.support.v7.widget.Toolbar
  205. android:id="@+id/toolbar"
  206. android:layout_width="match_parent"
  207. android:layout_height="?attr/actionBarSize"
  208. app:layout_scrollFlags="scroll|enterAlways"
  209. app:popupTheme="@style/AppTheme.PopupOverlay"
  210. app:title="Title" />
  211.  
  212. </android.support.design.widget.AppBarLayout>
  213.  
  214. <android.support.v7.widget.RecyclerView
  215. android:id="@+id/nestedView"
  216. android:layout_width="match_parent"
  217. android:layout_height="match_parent"
  218. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  219.  
  220. <LinearLayout
  221. android:layout_width="wrap_content"
  222. android:layout_height="wrap_content"
  223. android:orientation="horizontal"
  224. app:layout_anchor="@id/app_bar"
  225. app:layout_anchorGravity="bottom|end">
  226.  
  227. <Button
  228. android:id="@+id/disableNestedScrollingButton"
  229. android:layout_width="wrap_content"
  230. android:layout_height="wrap_content"
  231. android:text="disable"/>
  232.  
  233. <Button
  234. android:id="@+id/enableNestedScrollingButton"
  235. android:layout_width="wrap_content"
  236. android:layout_height="wrap_content"
  237. android:text="enable"
  238. />
  239. </LinearLayout>
  240. </android.support.design.widget.CoordinatorLayout>
  241.  
  242. // To disable collapsing
  243. AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
  244. params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
  245. toolbar.setLayoutParams(params);
  246.  
  247. // To enable collapsing
  248. AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
  249. params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL|AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
  250. toolbar.setLayoutParams(params);
  251.  
  252. android:nestedScrollingEnabled="false"
  253.  
  254. app:behavior_overlapTop = "24dp"
  255.  
  256. <android.support.design.widget.CoordinatorLayout
  257. android:layout_width="match_parent"
  258. android:layout_height="match_parent"
  259. android:background="@color/background"
  260. android:fitsSystemWindows="true">
  261.  
  262. <android.support.design.widget.AppBarLayout
  263. android:id="@+id/app_bar"
  264. android:layout_width="match_parent"
  265. android:layout_height="@dimen/app_bar_height"
  266. android:fitsSystemWindows="true"
  267. android:theme="@style/AppTheme.AppBarOverlay">
  268.  
  269. <android.support.design.widget.CollapsingToolbarLayout
  270. android:id="@+id/toolbar_layout"
  271. android:layout_width="match_parent"
  272. android:layout_height="match_parent"
  273. android:fitsSystemWindows="true"
  274. app:contentScrim="?attr/colorPrimary"
  275. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  276.  
  277. <android.support.v7.widget.Toolbar
  278. android:id="@+id/toolbar"
  279. android:layout_width="match_parent"
  280. android:layout_height="?attr/actionBarSize"
  281. app:layout_collapseMode="pin"
  282. app:popupTheme="@style/AppTheme.PopupOverlay"
  283. app:title="Title" />
  284.  
  285. </android.support.design.widget.CollapsingToolbarLayout>
  286. </android.support.design.widget.AppBarLayout>
  287.  
  288.  
  289. <android.support.v4.widget.NestedScrollView
  290. android:layout_width="match_parent"
  291. android:layout_height="match_parent"
  292. android:layout_marginLeft="10dp"
  293. android:layout_marginRight="10dp"
  294. android:background="@android:color/transparent"
  295. app:behavior_overlapTop="@dimen/behavior_overlap_top"
  296. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  297.  
  298. <LinearLayout
  299. android:id="@+id/linearLayout"
  300. android:layout_width="match_parent"
  301. android:layout_height="match_parent"
  302. android:orientation="vertical">
  303.  
  304. <android.support.v7.widget.RecyclerView
  305. android:id="@+id/recycler_view
  306. android:layout_width="match_parent"
  307. android:layout_height="wrap_content"
  308. android:layout_margin="@dimen/text_min_padding"
  309. android:nestedScrollingEnabled="false"
  310. android:scrollbarSize="2dp"
  311. android:scrollbarStyle="outsideInset"
  312. android:scrollbarThumbVertical="@color/colorAccent"
  313. android:scrollbars="vertical" />
  314.  
  315. </LinearLayout>
  316.  
  317. </android.support.v4.widget.NestedScrollView>
  318.  
  319. </android.support.design.widget.CoordinatorLayout>
  320.  
  321. lockAppBarClosed();
  322. ViewCompat.setNestedScrollingEnabled(recyclerView, false); // to lock the CollapsingToolbarLayout
  323.  
  324. private void setAppBarDragging(final boolean isEnabled) {
  325. CoordinatorLayout.LayoutParams params =
  326. (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
  327. AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
  328. behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
  329. @Override
  330. public boolean canDrag(AppBarLayout appBarLayout) {
  331. return isEnabled;
  332. }
  333. });
  334. params.setBehavior(behavior);
  335. }
  336.  
  337. public void unlockAppBarOpen() {
  338. appBarLayout.setExpanded(true, false);
  339. appBarLayout.setActivated(true);
  340. setAppBarDragging(false);
  341. }
  342.  
  343. public void lockAppBarClosed() {
  344. appBarLayout.setExpanded(false, false);
  345. appBarLayout.setActivated(false);
  346. setAppBarDragging(false);
  347.  
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement