How to mimic Google Maps' bottom-sheet 3 phases behavior?

后端 未结 4 576
青春惊慌失措
青春惊慌失措 2020-12-04 05:11

Background

I\'m assigned to make a UI that behaves similar to how Google Maps shows a bottom-sheet for a found result.

It has three different phases:

4条回答
  •  一整个雨季
    2020-12-04 05:28

    I also had to implement a view similar to how Google Maps shows a bottom-sheet for a found result.

    Here's how mine looks:

    At first, I defined a bottom sheet with a header and scrollable content but the layout_height did not seem to wrap the content neither of the header nor the scrollable content despite specifying wrap_content.

    That problem went away when I used LinearLayout instead of ConstraintLayout for the CoordinatorLayout's child layout (and for its children).

    activity_main.xml

    
    
    
        

    MainActivity.java

    package com.example.bottomsheetwithscrollablecontent;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import com.google.android.material.bottomsheet.BottomSheetBehavior;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.coordinatorlayout.widget.CoordinatorLayout;
    
    public class MainActivity extends AppCompatActivity {
        private CoordinatorLayout layout_coordinator;
        private View layout_coordinator_child;
        private View layout_bottom_sheet_header;
    
        private BottomSheetBehavior behavior;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            layout_coordinator = findViewById(R.id.layout_coordinator);
            layout_coordinator_child = layout_coordinator.findViewById(R.id.layout_coordinator_child);
            layout_bottom_sheet_header = layout_coordinator.findViewById(R.id.layout_bottom_sheet_header);
    
            behavior = BottomSheetBehavior.from(layout_coordinator_child);
    
            Button buttonPeek = findViewById(R.id.buttonPeek);
            buttonPeek.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    behavior.setPeekHeight(layout_bottom_sheet_header.getHeight());
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            });
    
            Button buttonExpand = findViewById(R.id.buttonExpand);
            buttonExpand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            });
    
            Button buttonClose = findViewById(R.id.buttonClose);
            buttonClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                }
            });
        }
    }
    

    app/build.gradle

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.example.bottomsheetwithscrollablecontent"
            minSdkVersion 24
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
        implementation "com.google.android.material:material:1.1.0-alpha04"
    }
    

提交回复
热议问题