Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <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"
- android:fitsSystemWindows="true"
- tools:context="com.example.user.myapplication.ScrollingActivity">
- <android.support.design.widget.AppBarLayout
- android:id="@+id/app_bar"
- android:layout_width="match_parent"
- android:layout_height="@dimen/app_bar_height"
- android:fitsSystemWindows="true"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.design.widget.CollapsingToolbarLayout
- android:id="@+id/toolbar_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- app:contentScrim="?attr/colorPrimary"
- app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- app:layout_collapseMode="pin"
- app:popupTheme="@style/AppTheme.PopupOverlay"/>
- </android.support.design.widget.CollapsingToolbarLayout>
- </android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.RecyclerView
- android:id="@+id/nestedView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- app:layout_anchor="@id/app_bar"
- app:layout_anchorGravity="bottom|end">
- <Button
- android:id="@+id/disableNestedScrollingButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="disable"/>
- <Button
- android:id="@+id/enableNestedScrollingButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="enable"
- />
- </LinearLayout>
- </android.support.design.widget.CoordinatorLayout>
- public class ScrollingActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_scrolling);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- final RecyclerView nestedView = (RecyclerView) findViewById(R.id.nestedView);
- findViewById(R.id.disableNestedScrollingButton).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(final View v) {
- nestedView.setNestedScrollingEnabled(false);
- }
- });
- findViewById(R.id.enableNestedScrollingButton).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(final View v) {
- nestedView.setNestedScrollingEnabled(true);
- }
- });
- nestedView.setLayoutManager(new LinearLayoutManager(this));
- nestedView.setAdapter(new Adapter() {
- @Override
- public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
- return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
- android.R.layout.simple_list_item_1,
- parent,
- false)) {
- };
- }
- @Override
- public void onBindViewHolder(final ViewHolder holder, final int position) {
- ((TextView) holder.itemView.findViewById(android.R.id.text1)).setText("item " + position);
- }
- @Override
- public int getItemCount() {
- return 100;
- }
- });
- }
- }
- <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"
- android:fitsSystemWindows="true"
- tools:context="com.example.user.myapplication.ScrollingActivity">
- <android.support.design.widget.AppBarLayout
- android:id="@+id/app_bar"
- android:layout_width="match_parent"
- android:layout_height="@dimen/app_bar_height"
- android:fitsSystemWindows="true"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.design.widget.CollapsingToolbarLayout
- android:id="@+id/toolbar_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- app:contentScrim="?attr/colorPrimary"
- app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- app:layout_collapseMode="pin"
- app:popupTheme="@style/AppTheme.PopupOverlay"/>
- </android.support.design.widget.CollapsingToolbarLayout>
- </android.support.design.widget.AppBarLayout>
- <android.support.v4.widget.NestedScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="fill_vertical"
- android:fillViewport="true"
- app:layout_behavior="@string/appbar_scrolling_view_behavior">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/nestedView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
- </RelativeLayout>
- </android.support.v4.widget.NestedScrollView>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- app:layout_anchor="@id/app_bar"
- app:layout_anchorGravity="bottom|end">
- <Button
- android:id="@+id/disableNestedScrollingButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="disable"/>
- <Button
- android:id="@+id/enableNestedScrollingButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="enable"
- />
- </LinearLayout>
- </android.support.design.widget.CoordinatorLayout>
- <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"
- android:fitsSystemWindows="true"
- tools:context="com.example.user.myapplication.ScrollingActivity">
- <android.support.design.widget.AppBarLayout
- android:id="@+id/app_bar"
- android:layout_width="match_parent"
- android:layout_height="@dimen/app_bar_height"
- android:fitsSystemWindows="true"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- app:layout_scrollFlags="scroll|enterAlways"
- app:popupTheme="@style/AppTheme.PopupOverlay"
- app:title="Title" />
- </android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.RecyclerView
- android:id="@+id/nestedView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- app:layout_anchor="@id/app_bar"
- app:layout_anchorGravity="bottom|end">
- <Button
- android:id="@+id/disableNestedScrollingButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="disable"/>
- <Button
- android:id="@+id/enableNestedScrollingButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="enable"
- />
- </LinearLayout>
- </android.support.design.widget.CoordinatorLayout>
- // To disable collapsing
- AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
- params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
- toolbar.setLayoutParams(params);
- // To enable collapsing
- AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
- params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL|AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
- toolbar.setLayoutParams(params);
- android:nestedScrollingEnabled="false"
- app:behavior_overlapTop = "24dp"
- <android.support.design.widget.CoordinatorLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/background"
- android:fitsSystemWindows="true">
- <android.support.design.widget.AppBarLayout
- android:id="@+id/app_bar"
- android:layout_width="match_parent"
- android:layout_height="@dimen/app_bar_height"
- android:fitsSystemWindows="true"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.design.widget.CollapsingToolbarLayout
- android:id="@+id/toolbar_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- app:contentScrim="?attr/colorPrimary"
- app:layout_scrollFlags="scroll|exitUntilCollapsed">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- app:layout_collapseMode="pin"
- app:popupTheme="@style/AppTheme.PopupOverlay"
- app:title="Title" />
- </android.support.design.widget.CollapsingToolbarLayout>
- </android.support.design.widget.AppBarLayout>
- <android.support.v4.widget.NestedScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="@android:color/transparent"
- app:behavior_overlapTop="@dimen/behavior_overlap_top"
- app:layout_behavior="@string/appbar_scrolling_view_behavior">
- <LinearLayout
- android:id="@+id/linearLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/recycler_view
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/text_min_padding"
- android:nestedScrollingEnabled="false"
- android:scrollbarSize="2dp"
- android:scrollbarStyle="outsideInset"
- android:scrollbarThumbVertical="@color/colorAccent"
- android:scrollbars="vertical" />
- </LinearLayout>
- </android.support.v4.widget.NestedScrollView>
- </android.support.design.widget.CoordinatorLayout>
- lockAppBarClosed();
- ViewCompat.setNestedScrollingEnabled(recyclerView, false); // to lock the CollapsingToolbarLayout
- private void setAppBarDragging(final boolean isEnabled) {
- CoordinatorLayout.LayoutParams params =
- (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
- AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
- behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
- @Override
- public boolean canDrag(AppBarLayout appBarLayout) {
- return isEnabled;
- }
- });
- params.setBehavior(behavior);
- }
- public void unlockAppBarOpen() {
- appBarLayout.setExpanded(true, false);
- appBarLayout.setActivated(true);
- setAppBarDragging(false);
- }
- public void lockAppBarClosed() {
- appBarLayout.setExpanded(false, false);
- appBarLayout.setActivated(false);
- setAppBarDragging(false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement