-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
272 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.hoc.viewpager2 | ||
|
||
import com.github.kittinunf.fuel.core.FuelError | ||
import com.github.kittinunf.fuel.coroutines.awaitObjectResult | ||
import com.github.kittinunf.fuel.httpGet | ||
import com.github.kittinunf.fuel.serialization.kotlinxDeserializerOf | ||
import com.github.kittinunf.result.Result | ||
import kotlinx.coroutines.delay | ||
import kotlinx.serialization.ImplicitReflectionSerializer | ||
import kotlinx.serialization.internal.ArrayListSerializer | ||
|
||
private const val URL = "https://hoc081098.github.io/hoc081098.github.io/data.json" | ||
|
||
@ImplicitReflectionSerializer | ||
suspend fun getViewPagerItems(): Result<List<ViewPagerItem>, FuelError> { | ||
delay(2_000) | ||
|
||
return URL | ||
.httpGet() | ||
.awaitObjectResult(kotlinxDeserializerOf(ArrayListSerializer(ViewPagerItem.serializer()))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.hoc.viewpager2 | ||
|
||
import android.view.View | ||
import androidx.viewpager2.widget.ViewPager2 | ||
|
||
class FlipTransformer(private val viewPager: ViewPager2) : ViewPager2.PageTransformer { | ||
override fun transformPage(page: View, position: Float) { | ||
val rotation = -180f * position | ||
|
||
page.visibility = if (rotation > 90f || rotation < -90f) View.INVISIBLE else View.VISIBLE | ||
page.pivotX = page.width * 0.5f | ||
page.pivotY = page.height * 0.5f | ||
|
||
when (viewPager.orientation) { | ||
ViewPager2.ORIENTATION_VERTICAL -> page.rotationX = rotation | ||
ViewPager2.ORIENTATION_HORIZONTAL -> page.rotationY = rotation | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,89 @@ | ||
package com.hoc.viewpager2 | ||
|
||
import android.graphics.Color | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.view.Menu | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.viewpager2.widget.ViewPager2 | ||
import androidx.viewpager2.widget.ViewPager2.ORIENTATION_HORIZONTAL | ||
import androidx.viewpager2.widget.ViewPager2.ORIENTATION_VERTICAL | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import kotlinx.serialization.ImplicitReflectionSerializer | ||
import kotlin.LazyThreadSafetyMode.NONE | ||
|
||
@ExperimentalCoroutinesApi | ||
@ImplicitReflectionSerializer | ||
class MainActivity : AppCompatActivity() { | ||
private val scope = MainScope() | ||
private val adapter by lazy(NONE) { ViewPagerAdapter(GlideApp.with(this)) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
view_pager.adapter = adapter | ||
|
||
adapter.submitList( | ||
listOf( | ||
ViewPagerItem( | ||
id = "0", | ||
imageUrl = "https://cdn-images-1.medium.com/max/600/1*OFJKA8dRYZSb-Kprx-VReg.png", | ||
name = "Android Jetpack" | ||
), | ||
ViewPagerItem( | ||
id = "1", | ||
imageUrl = "https://avatars3.githubusercontent.com/u/36917223?s=400&v=4", | ||
name = "hoc081098" | ||
), | ||
ViewPagerItem( | ||
id = "2", | ||
imageUrl = "https://cms-assets.tutsplus.com/uploads/users/369/posts/31577/preview_image/Building-reactive-Android-apps-with-RxJava-Kotlin.png", | ||
name = "RxKotlin" | ||
), | ||
ViewPagerItem( | ||
id = "3", | ||
imageUrl = "https://res.infoq.com/presentations/netflix-functional-rx/en/slides/sl74.jpg", | ||
name = "Functional Reactive Programming" | ||
view_pager.run { | ||
adapter = this@MainActivity.adapter | ||
orientation = ViewPager2.ORIENTATION_HORIZONTAL | ||
registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { | ||
override fun onPageSelected(position: Int) { | ||
Toast.makeText(this@MainActivity, "Selected $position", Toast.LENGTH_SHORT).show() | ||
} | ||
}) | ||
setPageTransformer(FlipTransformer(this)) | ||
} | ||
|
||
GlideApp | ||
.with(this) | ||
.asGif() | ||
.load(R.drawable.loading_indicator) | ||
.into(image_loading) | ||
|
||
getItems() | ||
} | ||
|
||
private fun getItems() { | ||
scope.launch { | ||
image_loading.visibility = View.VISIBLE | ||
getViewPagerItems() | ||
.fold( | ||
{ | ||
adapter.submitList(it) | ||
}, | ||
{ | ||
Toast | ||
.makeText( | ||
this@MainActivity, | ||
it.message ?: "An unexpected error occurred", | ||
Toast.LENGTH_SHORT | ||
) | ||
.show() | ||
} | ||
) | ||
) | ||
) | ||
image_loading.visibility = View.GONE | ||
} | ||
} | ||
|
||
override fun onCreateOptionsMenu(menu: Menu?) = menuInflater.inflate(R.menu.menu_main, menu).let { false } //TODO | ||
|
||
override fun onOptionsItemSelected(item: MenuItem?): Boolean { | ||
if (item?.itemId == R.id.action_change_orientation) { | ||
view_pager.orientation = when (view_pager.orientation) { | ||
ORIENTATION_HORIZONTAL -> ORIENTATION_VERTICAL | ||
ORIENTATION_VERTICAL -> ORIENTATION_HORIZONTAL | ||
else -> error(":(") | ||
} | ||
} | ||
return super.onOptionsItemSelected(item) | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
scope.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package com.hoc.viewpager2 | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ViewPagerItem( | ||
val imageUrl: String, | ||
@SerialName("image_url") val imageUrl: String, | ||
val name: String, | ||
val id: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="#D81B60" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z" /> | ||
</vector> |
Oops, something went wrong.