An library that can make your Fragment & Activity swipeable
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.dattran2k:AndroidSwipeBackLayout:1.0.5'
}
I made some big tech app example : Facebook, Telegram, Tiktok, or you change change params dynamically
Download here : Google Play or look at the source code
πΊ Youtube video demo
....
dependencies {
implementation 'com.github.dattran2k:AndroidSwipeBackLayout:1.0.5'
}
class YourActivity : SwipeActivity() {
private lateinit var binding: YourActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = YourActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
}
}
Your root view must be single
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
..................
</androidx.constraintlayout.widget.ConstraintLayout>
Set theme for your activity, this will make your activity have transparent background and see though when swipe
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
Activity
class YourActivity : SwipeActivity() {
private lateinit var binding: YourActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = YourActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
}
}
Xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Activity
class YourActivity : AppCompatActivity(), SwipeListener {
private lateinit var binding: YourActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = YourActivityBinding.inflate(layoutInflater)
// set your config here
binding.root.setLateConfig(getSwipeConfig())
setContentView(binding.root)
}
private fun getSwipeConfig(): SwipeLayoutConfig {
return SwipeLayoutConfig.Builder()
.listener(this)
.position(SwipeDirection.LEFT_TO_RIGHT)
.build()
}
override fun onSwipeStateChanged(state: Int) {
}
override fun onSwipeChange(percent: Float) {
}
override fun onSwipeOpened() {
}
override fun onSwipeClosed() {
finish()
}
override fun onApplyScrim(alpha: Float) {
}
}
First, sorry to say, if you are using navigation component, you can't use this lib because navigation component use replace() fragment by default, that means when you navigate to other fragment, your previous Fragment will run onDestroyView() so you can't see any things previous
Navigate between Fragment using add() will work
Fragment, wrap your root view with wrapSwipeLayout(yourRootView)
class SecondFragment : SwipeBackFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentSecondBinding.inflate(layoutInflater)
return wrapSwipeLayout(binding.root)
}
}
Xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment
class ThirdFragment : BaseFragment<FragmentThirdBinding>(FragmentThirdBinding::inflate),
SwipeListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// set config
binding.container.setLateConfig(getSwipeConfig())
}
private fun getSwipeConfig(): SwipeLayoutConfig {
return SwipeLayoutConfig.Builder()
.listener(this)
.position(SwipeDirection.LEFT_TO_RIGHT)
.build()
}
override fun onSwipeStateChanged(state: Int) {}
override fun onSwipeChange(percent: Float) {}
override fun onSwipeOpened() {}
override fun onSwipeClosed() {
// your code when swipe close
NavigationManager.getInstance().popBackStack()
}
}
Xml
<com.dat.swipe_layout.swipe.SwipeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/container"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.dat.swipe_layout.swipe.SwipeLayout>
You can update your config when ever you want
fun updateConfig(){
val myConfig = getSwipeConfig()
// update config
swipeLayout.setLateConfig(myConfig)
}
fun getSwipeConfig(): SwipeLayoutConfig {
return SwipeLayoutConfig.Builder()
.listener(this)
.position(SwipeDirection.LEFT_TO_RIGHT)
.build()
}
ConstraintLayout
FrameLayout
LinearLayout
.......
NestedScrollView
ViewPager
RecyclerView
WebView
....
I used canScrollVertically()
and canScrollHorizontally()
to check child view can scroll or not
So if you don't do that, your "ScrollAbleView" cant' scroll at all
Parameter | Type | Default value | Description |
---|---|---|---|
swipeDirection |
SwipeDirection | SwipeDirection.LEFT_TO_RIGHT | your swipe direction |
listener |
SwipeListener | null | The swipe listener set by the user to respond to certain events in the sliding mechanism |
scrimColor |
color |
Color.BLACK | color of the background scrim |
scrimStartAlpha |
Float |
1.0f | start alpha value for when your view start scroll ( 0.0f to 1.0f ) |
scrimEndAlpha |
Float |
0.0f | end alpha value for when your view stop draw scrim (0.0f to 1.0f ) |
scrimThreshHold |
Float |
0.0f | scrim only draw when scroll from 1f ->scrimThreshHold |
isFullScreenScrim |
Boolean |
false | When layout draw scrim, it will be draw full screen, but it may reduce app performance useful when you activity or fragment transparent. |
isEnableScrim |
Boolean |
true | this flag for check auto draw scrim |
distanceThreshold |
Float |
0.4f | The minimum viable distance the activity has to be dragged in order to be slinged off the screen, expressed as a percentage of the screen size (width or height). |
velocityThreshold |
Float |
5000f | The velocity threshold at which the slide action is completed regardless of offset distance of the drag. |
touchDisabledViews |
List<View> |
null | Views on which touching should not result in any swiping. |
touchSwipeViews |
List<View> |
null | Views on which touching should not result in any swiping. |
.... |
.... |
.... | ..... |
git clone https://github.com/dattran2k/AndroidSwipeBackLayout.git
If you have any feedback, please reach out to me at trandat728@gmail.com, telegram : @Dat2030
Or my Facebook
I'm just small developer, you can contact me any time if you wanna help or have new idea for this lib
- [@dattran2k]https://github.com/dattran2k)