Skip to content

Latest commit

Β 

History

History
289 lines (241 loc) Β· 10.1 KB

README.md

File metadata and controls

289 lines (241 loc) Β· 10.1 KB

AndroidSwipeBackLayout πŸ“•

An library that can make your Fragment & Activity swipeable

logo_git

Gradle Dependency πŸ”¨

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

dependencies {
	implementation 'com.github.dattran2k:AndroidSwipeBackLayout:1.0.5'
}

Example 😎

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

facebook telegram tiktok shared element custom1 custom2

Usage

Step 1. Implementation

....

dependencies {
    implementation 'com.github.dattran2k:AndroidSwipeBackLayout:1.0.5'
}

Step 2. Extends your Activity

class YourActivity : SwipeActivity() {
    private lateinit var binding: YourActivityBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = YourActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}

Step 3. xml

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>

Step 4. Theme

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>

Documentation πŸ“„

1. Activity

1.1. Extends SwipeActivity

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>

1.2. Without Extends SwipeActivity, you can wrap you root view with SwipeLayout in xml

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) {

    }
}

1.3. Without Extends SwipeActivity, you can wrap you root view with SwipeLayout in Activity

Check my SwipeBackActivity

2. Fragment, there are 2 ways to implement

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

2.1. Extends SwipeBackFragment

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>

2.2. Wrap your root view in xml

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>

Update config

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()
}

Warning

Your root views must me single child :
ConstraintLayout
FrameLayout
LinearLayout
.......
If your Fragment or Activity have Scrollable view, make sure it's your root view
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

Custom Config

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.
.... .... .... .....

View all config here

Download example project ⬇️

git clone https://github.com/dattran2k/AndroidSwipeBackLayout.git

Feedback πŸ“¬

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

Authors πŸ‘·