Skip to content

Repository files navigation

SlidableLayout

License

SlidableLayout is devoted to build a stable, easy-to-use and smooth sliding layout.

中文版

Preview

VerticalHorizontal
SlidableLayoutSlidableLayoutHorizontal
Nested scrollOpposite nested scroll
NestedScrollOppositeNestedScroll

Features

  • Support adapt to the View or Fragment
  • Switch the position of two reusable View in turn when sliding
  • Abundant callback to cover lifecycle
  • Support infinite sliding
  • Support NestedScrolling and can be used with layouts that implement the NestedScrollingParent interface, such as SwipeRefreshLayout.

Usage

write in XML

<!--vertical-->
<com.yy.mobile.widget.SlidableLayout
android:layout_width="match_parent"android:layout_height="match_parent"/>
<!--horizontal-->
<com.yy.mobile.widget.SlidableLayout android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal" />

Implements the NestedScrollingChild interface, SlidableLayout can nest into other refresh layouts, so you can customize your pull-down-refresh and pull-up-load-more behavior.

finish adapter code

classMyAdapter(fm:FragmentManager) : SlideFragmentAdapter(fm) {
privateval data =listOf("a", "b", "c", "d")
privatevar currentIndex =0/** * Whether it can slide in the direction of [direction].*/overridefuncanSlideTo(direction:SlideDirection): Boolean {
val index =when (direction) {
SlideDirection.Next-> currentIndex +1//can slide to next pageSlideDirection.Prev-> currentIndex -1//can slide to previous pageelse-> currentIndex
}
return index in0 until data.size
}
/** * Called by [SlidableLayout] to create the content [Fragment].*/overridefunonCreateFragment(context:Context): Fragment {
returnDemoFragment()
}
/** * Called by [SlidableLayout] when the [fragment] starts to slide to visible. * This method should update the contents of the [fragment] to reflect the * item at the [direction].*/overridefunonBindFragment(fragment:Fragment, direction:SlideDirection) {
val index =when (direction) {
SlideDirection.Next-> currentIndex +1//to next pageSlideDirection.Prev-> currentIndex -1//to previous pageSlideDirection.Origin-> currentIndex
}
//bind data to the ui
(fragment asDemoFragment).currentData = data[index]
}
/** * Called by [SlidableLayout] when the view finishes sliding.*/overridefunfinishSlide(direction:SlideDirection) {
super.finishSlide(direction)
//update current index
currentIndex =when (direction) {
SlideDirection.Next-> currentIndex +1//already to next pageSlideDirection.Prev-> currentIndex -1//already to previous pageSlideDirection.Origin-> currentIndex //rebound to origin page
}
}
}

Call setAdapter to add Fragment into SlideableLayout

slidable_layout.setAdapter(MyAdapter(supportFragmentManager))

The demo provides more detail.

Callback

The View or Fragment created by SlideAdapter can implement the SlidableUI interface:

classDemoFragment : Fragment(), SlidableUI {
overridefunstartVisible(direction:SlideDirection) {
//At the beginning of the slide, the current view will be visible.//Binding data into view can be implemented in this callback,//such as displaying place holder pictures.
}
overridefuncompleteVisible(direction:SlideDirection) {
//After sliding, the current view is completely visible.//You can start the main business in this callback,//such as starting to play video, page exposure statistics...
}
overridefuninvisible(direction:SlideDirection) {
//After sliding, the current view is completely invisible.//You can do some cleaning work in this callback,//such as closing the video player.
}
overridefunpreload(direction:SlideDirection) {
//Have completed a sliding in the direction, and the user is likely to//continue sliding in the same direction. //You can preload the next page in this callback,//such as download the next video or prepare the cover image.
}
}

Install

  1. Add it in your root build.gradle at the end of repositories:

    allprojects {
    repositories {
    ...
    maven { url 'https://jitpack.io' }
    }
    }
  2. Add the dependency

    dependencies {
    implementation 'com.github.YvesCheung:SlidableLayout:1.2.0'
    }

License

Copyright 2019 YvesCheung
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

SlidableLayout is devoted to build a stable, easy-to-use and smooth sliding layout.

Topics

Resources

Stars

412 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages