android-shard
android-shard copied to clipboard
'Fragments' with a simpler api built on top of the android architecture components
Shard
Shards are 'Fragments' with a simpler api built on top of the android architecture components.
Stability: All important features are implemented but there may be some api changes before 1.0.0.
Download
def shard_version = '1.0.0-beta03'
// The core lib
implementation "me.tatarka.shard:shard:$shard_version"
// For use with appcompat
implementation "me.tatarka.shard:shard-appcompat:$shard_version"
// For use with the material design lib
implementation "me.tatarka.shard:shard-host-ui:$shard_version"
// For use with the android architecture navigation component
implementation "me.tatarka.shard:shard-nav:$shard_version"
// For use in a ViewPager
implementation "me.tatarka.shard:shard-pager:$shard_version"
// For use in a ViewPager2
implementation "me.tatarka.shard:shard-pager2:$shard_version"
// For using androidx.transition
implementation "me.tatarka.shard:shard-transition:$shard_version"
// For interop with fragments
implementation "me.tatarka.shard:shard-fragment-interop:$shard_version"
Kotlin
There's a few additional artifacts for kotlin extensions.
implementation "me.tatarka.shard:shard-ktx:$shard_version"
implementation "me.tatarka.shard:shard-host-ui-ktx:$shard_version"
SNAPSHOT
You can also follow the bleeding-edge with SNAPSHOT releases.
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
def shard_version = '1.0.0-beta03-SNAPSHOT'
Usage
Creating a shard is as simple as
class MyShard: Shard() {
const val REQUEST_CODE = 1
// get a ViewModel
val vm by viewModels<MyViewModel>()
override fun onCreate() {
setContentView(R.layout.my_shard)
// find a view
val name: TextView = requireViewById(R.id.name)
// listen with LiveData
vm.name.observe(this, Observer { value -> name.text = value })
// handle back presses
onBackPressedDispatcher.addCallback(this, enabled = true) {
remove()
}
// start an activity for result
activityCallbacks.addOnActivityResultCallback(REQUEST_CODE) { resultCode, data -> }
requireViewById<Button>(R.id.button).setOnClickListener {
activityCallbacks.startActivityForResult(Intent(context, MyActivity::class.java), REQUEST_CODE)
}
}
}
<me.tatarka.shard.wiget.ShardHost
android:id="@+id/host"
android:name="com.example.MyShard"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/my_shard" />
Full Documentation
- Getting Started
- Implementing Shards
- Hosting Shards
- Transition Animations
- Migrating from Fragments