Skip to content

Commit

Permalink
viewBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
wangpeng committed Jun 13, 2021
1 parent 7c7421d commit 51ab772
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 166 deletions.
6 changes: 1 addition & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

androidExtensions {
experimental = true
}

kapt {
generateStubs = true
Expand All @@ -23,6 +18,7 @@ android {
versionName rootProject.ext.versionName
applicationId "com.github.iielse.imageviewer.demo"
}
buildFeatures.viewBinding = true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.github.iielse.imageviewer.demo.R
import com.github.iielse.imageviewer.demo.core.ITEM_CLICKED
import com.github.iielse.imageviewer.demo.core.viewer.TransitionViewsRef
import com.github.iielse.imageviewer.demo.core.viewer.TransitionViewsRef.KEY_MAIN
import com.github.iielse.imageviewer.demo.data.MyData
import com.github.iielse.imageviewer.demo.databinding.MainActivityBinding
import com.github.iielse.imageviewer.demo.utils.App
import com.github.iielse.imageviewer.demo.utils.statusBarHeight
import com.github.iielse.imageviewer.utils.Config
import kotlinx.android.synthetic.main.main_activity.*

class MainActivity : AppCompatActivity() {
private val binding by lazy { MainActivityBinding.inflate(layoutInflater) }

private val viewModel by lazy { ViewModelProvider(this).get(TestDataViewModel::class.java) }
private val adapter by lazy { TestDataAdapter() }

override fun onDestroy() {
super.onDestroy()
orientation.setOnClickListener(null)
fullScreen.setOnClickListener(null)
loadAllAtOnce.setOnClickListener(null)
customTransition.setOnClickListener(null)
recyclerView.adapter = null
binding.orientation.setOnClickListener(null)
binding.fullScreen.setOnClickListener(null)
binding.loadAllAtOnce.setOnClickListener(null)
binding.customTransition.setOnClickListener(null)
binding.recyclerView.adapter = null
adapter.setListener(null)
TransitionViewsRef.releaseTransitionViewRef(KEY_MAIN)
}
Expand All @@ -35,7 +36,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
App.context = this.applicationContext // 随便找位置借个全局context用用.
Config.TRANSITION_OFFSET_Y = statusBarHeight()
setContentView(R.layout.main_activity)
setContentView(binding.root)
initialViews()
viewModel.dataList.observe(this, androidx.lifecycle.Observer(adapter::submitList))
}
Expand All @@ -57,34 +58,34 @@ class MainActivity : AppCompatActivity() {
}

private fun initialViews() {
orientation.setOnClickListener {
binding.orientation.setOnClickListener {
val orientationH = ViewerHelper.orientationH
ViewerHelper.orientationH = !orientationH
orientation.text = if (!orientationH) "Horizontal" else "Vertical"
binding.orientation.text = if (!orientationH) "Horizontal" else "Vertical"
Config.VIEWER_ORIENTATION = if (!orientationH) ViewPager2.ORIENTATION_HORIZONTAL else ViewPager2.ORIENTATION_VERTICAL
}
fullScreen.setOnClickListener {
binding.fullScreen.setOnClickListener {
val isFullScreen = ViewerHelper.fullScreen
ViewerHelper.fullScreen = !isFullScreen
fullScreen.text = if (!isFullScreen) "FullScreen(on)" else "FullScreen(off)"
binding.fullScreen.text = if (!isFullScreen) "FullScreen(on)" else "FullScreen(off)"
Config.TRANSITION_OFFSET_Y = if (!isFullScreen) 0 else statusBarHeight()
}
loadAllAtOnce.setOnClickListener {
binding.loadAllAtOnce.setOnClickListener {
val isLoadAllAtOnce = ViewerHelper.loadAllAtOnce
ViewerHelper.loadAllAtOnce = !isLoadAllAtOnce
loadAllAtOnce.text = if (!isLoadAllAtOnce) "LoadAllAtOnce(on)" else "LoadAllAtOnce(off)"
binding.loadAllAtOnce.text = if (!isLoadAllAtOnce) "LoadAllAtOnce(on)" else "LoadAllAtOnce(off)"
}
simplePlayVideo.setOnClickListener {
binding.simplePlayVideo.setOnClickListener {
val isSimplePlayVideo = ViewerHelper.simplePlayVideo
ViewerHelper.simplePlayVideo = !isSimplePlayVideo
simplePlayVideo.text = if (!isSimplePlayVideo) "Video(simple)" else "Video(controlView)"
binding.simplePlayVideo.text = if (!isSimplePlayVideo) "Video(simple)" else "Video(controlView)"
}
customTransition.setOnClickListener {
binding.customTransition.setOnClickListener {
CustomTransitionHelper.show(it)
}

recyclerView.layoutManager = GridLayoutManager(this, 3)
recyclerView.adapter = adapter
binding.recyclerView.layoutManager = GridLayoutManager(this, 3)
binding.recyclerView.adapter = adapter
adapter.setListener(::handleAdapterListener)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ package com.github.iielse.imageviewer.demo.business

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.iielse.imageviewer.demo.R
import com.github.iielse.imageviewer.demo.databinding.ActivityTestBinding
import com.github.iielse.imageviewer.demo.utils.toast
import kotlinx.android.synthetic.main.activity_test.*

class TestActivity : AppCompatActivity() {
private val binding by lazy { ActivityTestBinding.inflate(layoutInflater) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
setContentView(binding.root)

exoVideoView.setOnClickListener {
binding.exoVideoView.setOnClickListener {
toast("video click")
}
exoVideoView.setOnLongClickListener {
binding.exoVideoView.setOnLongClickListener {
toast("video long clicked")
true
}
exoVideoView.prepare("https://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4")
exoVideoView.resume()
binding.exoVideoView.prepare("https://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4")
binding.exoVideoView.resume()
}

override fun onDestroy() {
super.onDestroy()
exoVideoView.release()
binding.exoVideoView.release()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.github.iielse.imageviewer.demo.R
import com.github.iielse.imageviewer.demo.core.AdapterCallback
import com.github.iielse.imageviewer.demo.core.viewer.TransitionViewsRef
import com.github.iielse.imageviewer.demo.core.viewer.TransitionViewsRef.KEY_MAIN
import com.github.iielse.imageviewer.demo.data.MyData
import com.github.iielse.imageviewer.demo.utils.inflate
import kotlinx.android.synthetic.main.item_image.*
import java.util.*

class TestDataAdapter : PagedListAdapter<MyData, RecyclerView.ViewHolder>(provideDiffer()) {
Expand All @@ -27,7 +24,7 @@ class TestDataAdapter : PagedListAdapter<MyData, RecyclerView.ViewHolder>(provid
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return TestDataViewHolder(parent.inflate(R.layout.item_image), listener)
return TestDataViewHolder(parent, listener)
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
Expand All @@ -41,7 +38,7 @@ class TestDataAdapter : PagedListAdapter<MyData, RecyclerView.ViewHolder>(provid
super.onViewAttachedToWindow(holder)
if (holder is TestDataViewHolder) {
(holder.itemView.tag as? MyData?)?.let {
TransitionViewsRef.provideTransitionViewsRef(KEY_MAIN).put(it.id, holder.imageView)
TransitionViewsRef.provideTransitionViewsRef(KEY_MAIN).put(it.id, holder.binding.imageView)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package com.github.iielse.imageviewer.demo.business

import android.view.View
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.github.iielse.imageviewer.demo.core.AdapterCallback
import com.github.iielse.imageviewer.demo.core.ITEM_CLICKED
import com.github.iielse.imageviewer.demo.data.MyData
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_image.*
import kotlinx.android.synthetic.main.item_image.view.*
import com.github.iielse.imageviewer.demo.databinding.ItemImageBinding

class TestDataViewHolder(override val containerView: View, private val listener: AdapterCallback) : RecyclerView.ViewHolder(containerView), LayoutContainer {
class TestDataViewHolder(
parent: ViewGroup,
callback: AdapterCallback,
val binding: ItemImageBinding =
ItemImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
) : RecyclerView.ViewHolder(binding.root) {
init {
// 初始化点击回调
itemView.setOnClickListener {
(it.tag as? MyData?)?.let { listener.invoke(ITEM_CLICKED, it) }
(it.tag as? MyData?)?.let { callback.invoke(ITEM_CLICKED, it) }
}
}

fun bind(item: MyData, pos: Int) {
itemView.tag = item

posTxt.text = when {
binding.posTxt.text = when {
item.subsampling -> "$pos subsampling"
item.url.endsWith(".gif") -> "$pos gif"
item.url.endsWith(".mp4") -> "$pos video"
else -> pos.toString()
}

// 测试 fitXY 的过渡动画效果
itemView.imageView.scaleType = if (pos == 19) ImageView.ScaleType.FIT_XY else ImageView.ScaleType.CENTER_CROP
binding.imageView.scaleType = if (pos == 19) ImageView.ScaleType.FIT_XY else ImageView.ScaleType.CENTER_CROP

Glide.with(imageView).load(item.url).into(imageView)
Glide.with(binding.imageView).load(item.url).into(binding.imageView)
}
}

7 changes: 2 additions & 5 deletions imageviewer/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

androidExtensions {
experimental = true
}

android {
buildToolsVersion rootProject.ext.buildToolsVersion
Expand All @@ -17,6 +12,8 @@ android {
versionName rootProject.ext.versionName
}

buildFeatures.viewBinding = true

kotlinOptions {
jvmTarget = "1.8"
}
Expand Down
Loading

0 comments on commit 51ab772

Please sign in to comment.