Skip to content

Commit

Permalink
优化部分api
Browse files Browse the repository at this point in the history
  • Loading branch information
932707629 committed Aug 15, 2021
1 parent 21ce76c commit 8970b9f
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 186 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/soushin/tinmvvm/app/utils/DataUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.soushin.tinmvvm.app.utils

import com.soushin.tinmvvm.R
import com.soushin.tinmvvm.mvvm.repository.entity.CategoryEntity
import com.soushin.tinmvvm.mvvm.repository.entity.ImageEntity

/**
Expand All @@ -16,7 +17,7 @@ object DataUtils {
data.add(ImageEntity(R.mipmap.ic_launcher))
}
for (i in 1..20){
data.add("这里是一个简单的字符串$i")
data.add(CategoryEntity(id = i,title = "这里是一个简单的字符串$i"))
}
return data
}
Expand Down
22 changes: 8 additions & 14 deletions app/src/main/java/com/soushin/tinmvvm/component/DemoTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@ package com.soushin.tinmvvm.component

import android.graphics.drawable.Drawable
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BindingAdapter
import com.blankj.ALog
import com.bumptech.glide.Glide
import me.soushin.tinmvvm.config.AppComponent
import me.soushin.tinmvvm.utils.AppManager

object DemoTheme {

/*@BindingAdapter("android:text")
@JvmStatic
fun setText(view: TextView, value:String) {
view.text = value
}
@BindingAdapter("android:textColor")
@JvmStatic
fun setTextColor(view: TextView, value:Int){
view.setTextColor(value)
}*/
@BindingAdapter("imageUrl", "error")
fun loadImage(view: ImageView, url: String, error: Drawable) {
Glide.with(view).load(url).error(error).into(view)
@BindingAdapter("imageUrl", "error","placeholder")//,requireAll = false
fun loadImage(view: ImageView, url: String, error: Drawable, placeholder: Drawable) {
ALog.i("当前页面处于什么状态",AppManager.get().currentActivity?.javaClass?.simpleName);
Glide.with(view).load(url).error(error).placeholder(placeholder).into(view)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class PagingSimpleAdapter : PagingDataAdapter<Article,RecyclerView.ViewHolder>(D
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
/**不调用getItem(position)方法会造成无法继续加载数据**/
val dataBean = getItem(position)
(holder as DataViewHolder).binding.tvChapterName.text = "$position ${dataBean?.title}"
val viewHolder=holder as DataViewHolder
viewHolder.binding.tvPosition.text = "$position"
viewHolder.binding.acticle=dataBean//.tvChapterName.text = "$position ${dataBean?.title}"
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import com.chad.library.adapter.base.binder.QuickViewBindingItemBinder
import com.soushin.tinmvvm.databinding.ItemTextBinding
import com.soushin.tinmvvm.mvvm.repository.entity.CategoryEntity

/**
*
* @auther SouShin
* @time 2020/6/28 14:55
*/
class TextItemBinder : QuickViewBindingItemBinder<String, ItemTextBinding>() {
class TextItemBinder : QuickViewBindingItemBinder<CategoryEntity, ItemTextBinding>() {

override fun convert(holder: BinderVBHolder<ItemTextBinding>, data: String) {
holder.viewBinding.tvText.text=data
override fun convert(holder: BinderVBHolder<ItemTextBinding>, data: CategoryEntity) {
holder.viewBinding.text = data
}

override fun onCreateViewBinding(
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/soushin/tinmvvm/mvvm/ui/DemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class DemoActivity : DataBindingActivity<ActivityDemoBinding, DemoViewModel>() {
private val navController by lazy { navHostFragment.navController }
// private val appBarConfiguration by lazy { AppBarConfiguration.Builder(R.id.homeFragment).build() }//配置homeFragment为顶部页面

//主页点回退 将app任务移动到后台
override fun onBackPressed() {
// super.onBackPressed()
moveTaskToBack(true)
}

override fun initView(savedInstanceState: Bundle?) {
mViewData?.apply {
setSupportActionBar(toolbar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import me.soushin.tinmvvm.config.DataBindingConfig
*/
class DataBindingLayoutFragment :
DataBindingFragment<FragmentDataBindingLayoutBinding, DataBindingLayoutViewModel>() {

companion object {
fun newInstance(): DataBindingLayoutFragment {
return DataBindingLayoutFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.soushin.tinmvvm.mvvm.viewmodel
import android.app.Application
import android.view.View
import androidx.databinding.*
import androidx.lifecycle.MutableLiveData
import com.soushin.tinmvvm.R
import com.soushin.tinmvvm.app.throttleClick
import me.soushin.tinmvvm.base.BaseViewModel
Expand All @@ -14,67 +15,36 @@ import kotlin.random.Random
class DataBindingLayoutViewModel(application: Application) :
BaseViewModel<DataBindingLayoutRepository>(application, DataBindingLayoutRepository()){

val firstName by lazy { ObservableField<String>() }
val lastName by lazy { ObservableField<String>() }
val age = ObservableInt()
val arrayMap = ObservableArrayMap<String, Any>().apply {
put("firstName", "Google")
put("lastName", "Inc.")
put("age", 17)
}
val arrayList = ObservableArrayList<Any>().apply {
add("Google")
add("Inc.")
add(17)
}

val user = User()
val user = MutableLiveData(User("","",0))
val firstName = ObservableField<String>("")
val lastName = ObservableField<String>("")
val firstList by lazy { listOf("","","","","","","","") }
val lastList by lazy { listOf("","","","","","","","","","","","") }


fun onViewClick() = throttleClick {
when(it.id){
R.id.fab_change->{
user.age= Random.nextInt(1000);
user.firstName = firstList[Random.nextInt(firstList.size)]
user.lastName = lastList[Random.nextInt(lastList.size)]

R.id.btn_change->{
user.postValue(User(firstList[Random.nextInt(firstList.size)],
lastList[Random.nextInt(lastList.size)],Random.nextInt(1000)))
firstName.set(user.value?.firstName)
lastName.set(user.value?.lastName)
}
}
}



}

data class User(
var firstName: String = "",
var lastName: String = "",
var age: Int = 0,
var imageUrl : String ="https://wx1.sinaimg.cn/mw690/002Po4pSly1gswgs812stj60u01hcndz02.jpg",
var error:Int = R.mipmap.ic_launcher
)


class User : BaseObservable() {

@get:Bindable
var firstName: String = ""
set(value) {
field = value
println("firstName发出更新=$field")
notifyPropertyChanged(BR.DataBindingLayoutViewModel)
}

@get:Bindable
var lastName: String = ""
set(value) {
field = value
println("lastName发出更新=$field")
notifyPropertyChanged(BR.DataBindingLayoutViewModel)
}

@get:Bindable
var age: Int = 0
set(value) {
field = value
println("age发出更新=$field")
notifyPropertyChanged(BR.DataBindingLayoutViewModel)
}

}



169 changes: 71 additions & 98 deletions app/src/main/res/layout/fragment_data_binding_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,112 +5,85 @@

<data>

<import type="androidx.databinding.ObservableMap"/>
<import type="com.soushin.tinmvvm.mvvm.ui.fragment.Fields"/>
<import type="androidx.databinding.ObservableList"/>

<variable
name="DataBindingLayoutViewModel"
type="com.soushin.tinmvvm.mvvm.viewmodel.DataBindingLayoutViewModel" />

<variable
name="userMap"
type="ObservableMap&lt;String, Object>"/>

<variable
name="userList"
type="ObservableList&lt;Object>" />
<variable name="user"
type="com.soushin.tinmvvm.mvvm.viewmodel.User"/>

</data>

<androidx.constraintlayout.widget.ConstraintLayout
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv1"
tools:text="@string/app_name"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginTop="@dimen/dp_10"
android:text="@{String.valueOf(DataBindingLayoutViewModel.arrayMap.lastName)}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv2"
tools:text="@string/app_name"
android:layout_marginLeft="@dimen/dp_10"
android:text="@{String.valueOf(1 + (Integer)DataBindingLayoutViewModel.arrayMap.age)}"
app:layout_constraintLeft_toRightOf="@+id/tv1"
app:layout_constraintTop_toTopOf="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv3"
tools:text="@string/app_name"
android:layout_marginLeft="@dimen/dp_15"
android:text="@{DataBindingLayoutViewModel.arrayList[Fields.LAST_NAME]}"
android:layout_marginTop="@dimen/dp_10"
app:layout_constraintTop_toBottomOf="@+id/tv1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv4"
tools:text="@string/app_name"
android:layout_marginLeft="@dimen/dp_10"
android:text="@{String.valueOf(1 + (Integer)DataBindingLayoutViewModel.arrayList[Fields.AGE])}"
app:layout_constraintLeft_toRightOf="@+id/tv3"
app:layout_constraintTop_toTopOf="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv5"
tools:text="@string/app_name"
android:layout_marginLeft="@dimen/dp_15"
android:text="@{DataBindingLayoutViewModel.user.lastName}"
android:layout_marginTop="@dimen/dp_10"
app:layout_constraintTop_toBottomOf="@+id/tv3"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv6"
tools:text="@string/app_name"
android:layout_marginLeft="@dimen/dp_10"
android:text="@{String.valueOf(1 + (Integer)DataBindingLayoutViewModel.user.age)}"
app:layout_constraintLeft_toRightOf="@+id/tv5"
app:layout_constraintTop_toTopOf="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv1"
android:src="@mipmap/ic_launcher"
app:error="@mipmap/ic_launcher"
app:imageUrl="https://wx1.sinaimg.cn/mw690/002Po4pSly1gswgs812stj60u01hcndz02.jpg"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>


<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_change"
android:onClick="@{DataBindingLayoutViewModel.onViewClick()}"
android:layout_marginBottom="@dimen/dp_20"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="@dimen/dp_15"
android:layout_marginStart="@dimen/dp_15"
android:text="@{DataBindingLayoutViewModel.user.firstName}"
tools:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<androidx.appcompat.widget.AppCompatTextView
tools:text="@string/app_name"
android:text="@{DataBindingLayoutViewModel.user.lastName}"
android:layout_marginStart="@dimen/dp_15"
app:layout_constraintLeft_toRightOf="@+id/tv1"
app:layout_constraintBaseline_toBaselineOf="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>


<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv3"
app:layout_constraintLeft_toLeftOf="@+id/tv1"
app:layout_constraintTop_toBottomOf="@+id/tv1"
android:layout_marginTop="@dimen/dp_10"
android:text="@{DataBindingLayoutViewModel.user.firstName}"
tools:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<androidx.appcompat.widget.AppCompatTextView
tools:text="@string/app_name"
android:text="@{DataBindingLayoutViewModel.user.lastName}"
android:layout_marginStart="@dimen/dp_15"
app:layout_constraintLeft_toRightOf="@+id/tv3"
app:layout_constraintBaseline_toBaselineOf="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_change"
android:text="更换数据"
android:layout_marginRight="@dimen/dp_15"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/tv1"
app:layout_constraintBottom_toBottomOf="@+id/tv3"
android:onClick="@{DataBindingLayoutViewModel.onViewClick()}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv1"
android:src="@mipmap/ic_launcher"
app:error="@{DataBindingLayoutViewModel.user.error}"
app:imageUrl="@{DataBindingLayoutViewModel.user.imageUrl}"
android:layout_marginTop="@dimen/dp_10"
app:layout_constraintTop_toBottomOf="@+id/tv3"
app:layout_constraintLeft_toLeftOf="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</layout>
Loading

0 comments on commit 8970b9f

Please sign in to comment.