- Notifications
You must be signed in to change notification settings - Fork72
Android sliding panel that is part of the view hierarchy, not above it.
License
PierfrancescoSoffritti/sliding-panel
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A ViewGroup that implements a sliding panel that is part of the view hierarchy, not above it.
All other implementations of thebottom sheet pattern andsliding panel pattern implement a panel that sits above all the other Views of the app. When the panel is collapsed (but visible) the only way to set its position is by using a peek factor (its distance from the bottom of the screen).
With this library the sliding panel is placed exactly where it is supposed to be in the view hierarchy, just like it would be in a vertical (or horizontal)LinearLayout
. It doesn't sit above other Views.
SlidingPanel
is a ViewGroup exendingFrameLayout
.
It has exacly two children: anon sliding view and asliding view.
- Thenon sliding view is just a view that doesn't move, positioned as if
SlidingPanel
was aLinearLayout
. - Thesliding view is a View that can be dragged by the user. It slides over the non sliding view, both vertically and horizontally.
The sliding view can becollapsed orexpanded.
- Whencollapsed, the sliding view is exactly where it would be if
SlidingPanel
was a LinearLayout. - Whenexpanded, the sliding view is positioned to exactly cover the non sliding view. (Therefore the maximum amount of movement allowed to the sliding view is equal to the height (or width) of the non sliding view)
You can download the apk of the sample appat this link, oron the PlayStore.
The code of the sample app is availableat this link.
Having the sample apps installed is a good way to be notified of new releases. Although watching this repository will allow GitHub to email you whenever a new release is published.
The Gradle dependency is available viajCenter. jCenter is the default Maven repository used by Android Studio.
The minimum API level supported by this library is API 15.
dependencies { implementation 'com.psoffritti.slidingpanel:core:1.0.0'}
In order to start using the library you need to add aSlidingPanel
to your layout.
<com.psoffritti.slidingpanel.SlidingPanelxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/sliding_panel"android:layout_width="match_parent"android:layout_height="match_parent"app:orientation="vertical"app:nonSlidingView="@id/non_sliding_view"app:slidingView="@id/sliding_view"app:elevation="4dp" > <TextViewandroid:id="@+id/non_sliding_view"android:layout_width="match_parent"android:layout_height="150dp"android:gravity="center"android:text="non sliding view"android:background="#af4448" /> <TextViewandroid:id="@+id/sliding_view"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="sliding view"android:background="#e57373" /></com.psoffritti.slidingpanel.SlidingPanel>
non_sliding_view
andsliding_view
can be whatever View or ViewGroup you need.
If you want to listen to slide events, you can add aOnSlideListener
to theSlidingPanel
.
sliding_panel.addSlideListener { slidingPanel, state, currentSlide->when(state) {PanelState.COLLAPSED-> { }PanelState.EXPANDED-> { }PanelState.SLIDING-> { } }}
SlidingPanel
has a set of attributes that you can set to customize its behviour. Some of this attributes are mandatory.
Mandatory: yes -- Value: view reference -- Default:null
This mandatory attribute is used to tellSlidingPanel
which of its two children is the sliding view. If this attribute is not setSlidingPanel
will throw an Excpetion.
<com.psoffritti.slidingpanel.SlidingPanel ...app:slidingView="@id/sliding_view" > ...</com.psoffritti.slidingpanel.SlidingPanel>
Mandatory: yes -- Value: view reference -- Default:null
This mandatory attribute is used to tellSlidingPanel
which of its two children is the non sliding view. If this attribute is not setSlidingPanel
will throw an Excpetion.
<com.psoffritti.slidingpanel.SlidingPanel ...app:nonSlidingView="@id/non_sliding_view" > ...</com.psoffritti.slidingpanel.SlidingPanel>
Mandatory: no -- Value: view reference -- Default: slidingView
This attribute is used to tellSlidingPanel
which View should be used to drag the sliding view. If not set this value defaults to the sliding view. Therefore the whole panel will be sensible to dragging.
Note that if the whole panel is draggable it won't be possible to use scrollable views inside the sliding view.
<com.psoffritti.slidingpanel.SlidingPanel ...app:dragView="@id/drag_view" > ...</com.psoffritti.slidingpanel.SlidingPanel>
Mandatory: no -- Value: view reference -- Default:null
When collapsed, the sliding view is shifted down (or right) by an amount equal to the height (or width) of the non sliding view. Therefore, when collapsed, the bottom (or right) part of the sliding view will be out of the screen.
This attribute is used to tellSlidingPanel
that we want a view to be shifted up (or left) so that it is always visible.
See the screenshots below to better understand. In the first onefitToScreenView
is set, in the second one it isn't.
Notice the white text at the bottom of the screen. It is not visible in the second screen, it is visible only when the panel is expanded.
The sample app hasan example specific for this attribute.
<com.psoffritti.slidingpanel.SlidingPanel ...app:fitToScreenView="@id/fit_to_screen_view" > ...</com.psoffritti.slidingpanel.SlidingPanel>
Mandatory: no -- Value:vertical
|horizontal
-- Default:vertical
This attribute is used to set the orientation ofSlidingPanel
in the same way that it is used for aLinearLayout
. By default it is set tovertical
.
<com.psoffritti.slidingpanel.SlidingPanel ...app:orientation="horizontal" > ...</com.psoffritti.slidingpanel.SlidingPanel>
Mandatory: no -- Value: dimension -- Default:4dp
This attribute is used to set the length of the shadow drawn to the top (or left) side of the sliding view.
<com.psoffritti.slidingpanel.SlidingPanel ...app:elevation="10dp" > ...</com.psoffritti.slidingpanel.SlidingPanel>
You can interact withSlidingPanel
programmatically through its API.
SlidingPanel
has a state that can be one of:EXPANDED
,COLLAPSED
andSLIDING
.
You canget
the state but are not allowed to set it directly.
To programmatically change the state of the panel you should use theslideTo
method.
This method takes as argument one of the possible states of the panel:EXPANDED
,COLLAPSED
. If you try to passSLIDING
the panel will throw anIllegalArgumentException
.
When this method is called the panel will automatically animate to to match the state passed as argument.
You can set the duration of the slide animation using theslideDuration
property.
This property affects:
- the duration of the slide triggered by
slideTo
. - the speed at which the panel autocompletes the slides when the user stops dragging it before reaching the
EXPANDED
orCOLLAPSED
state.
You can use the constants defined inSlidingPanel
(SlidingPanel.SLIDE_DURATION_SHORT
,SlidingPanel.SLIDE_DURATION_LONG
) or set it to an arbitrary duration in millisecond.
sliding_panel.slideDuration=SlidingPanel.SLIDE_DURATION_SHORTsliding_panel.slideDuration=SlidingPanel.SLIDE_DURATION_LONG
Sometimes it is usefull to change the dragView at runtime.
An example is give in the sample app, in the "advanced example". In this case a list is shown when the panel is expanded, therefore the drag view has to be changed. Otherwise the list wouldn't be scrollable.
Use thesetDragView(view: View)
method to programmatically set the drag view.
You can listen to slide events, by adding anOnSlideListener
to theSlidingPanel
.
sliding_panel.addSlideListener { slidingPanel, state, currentSlide->when(state) {PanelState.COLLAPSED-> { }PanelState.EXPANDED-> { }PanelState.SLIDING-> { } }}
or
sliding_panel.addSlideListener(object:SlidingPanel.OnSlideListener {overridefunonSlide(slidingPanel:SlidingPanel,state:PanelState,currentSlide:Float) { }})
- The first argument is a referece to the
SlidingPanel
emitting the event. - The second argument is the current state of the panel.
- The third argument is a value between 0 and 1. The value is 0 when the state is
COLLAPSED
and 1 whenEXPANDED
.
For any question feel free toopen an issue on the GitHub repository.
About
Android sliding panel that is part of the view hierarchy, not above it.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.