Permalink
Browse files

Use DataBinding

  • Loading branch information...
msdx committed Dec 20, 2018
1 parent ee53a00 commit b999e30cb7801bbb1cab9ad511cf461d47eed625
@@ -62,6 +62,10 @@ android {
lintOptions {
abortOnError false
}

dataBinding {
enabled = true
}
}

dependencies {
@@ -5,15 +5,14 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import com.githang.gradledoc.BR
import com.githang.gradledoc.Constants
import com.githang.gradledoc.R
import com.githang.gradledoc.app.chapter.ChapterActivity
import com.githang.gradledoc.app.others.AboutActivity
import com.githang.gradledoc.app.others.ContributorsActivity
import com.githang.gradledoc.app.process.ProcessActivity
import com.githang.gradledoc.common.ListActivity
import com.githang.gradledoc.common.recycler.DefaultHolder
import com.umeng.update.UmengUpdateAgent

/**
@@ -22,6 +21,9 @@ import com.umeng.update.UmengUpdateAgent
* @author Geek_Soledad (msdx.android@qq.com)
*/
class ContentsActivity : ListActivity<ChapterUrl>() {
override val itemLayoutId: Int = R.layout.item_contents
override val itemBrId: Int = BR.chapterUrl

private val presenter = ContentPresenter(this)

override fun onCreate(savedInstanceState: Bundle?) {
@@ -74,16 +76,6 @@ class ContentsActivity : ListActivity<ChapterUrl>() {
startActivity(intent)
}

override fun createHolder(parent: ViewGroup): DefaultHolder {
val holder = DefaultHolder(this, R.layout.item_contents, parent)
holder.hold(R.id.text)
return holder
}

override fun bindData(position: Int, holder: DefaultHolder, data: ChapterUrl) {
holder.setText(R.id.text, data.title)
}

override fun onItemClick(view: View, position: Int, item: ChapterUrl) {
val intent = Intent(this, ChapterActivity::class.java)
intent.putExtra(Constants.TITLE, item.title)
@@ -1,5 +1,8 @@
package com.githang.gradledoc.app.others

import android.databinding.BindingAdapter
import android.widget.ImageView
import com.githang.gradledoc.common.ImageLoader
import com.squareup.moshi.Json

/**
@@ -13,4 +16,12 @@ class Contributor(
@Json(name = "login") val name: String? = null,
@Json(name = "avatar_url") val avatar: String? = null,
@Json(name = "contributions") val contributions: Int = 0
)
) {
companion object {
@BindingAdapter(value = ["imageUrl"])
@JvmStatic
fun loadImage(imageView: ImageView, url: String) {
ImageLoader.loadImage(url, imageView)
}
}
}
@@ -1,13 +1,10 @@
package com.githang.gradledoc.app.others

import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import com.githang.gradledoc.BR
import com.githang.gradledoc.Constants
import com.githang.gradledoc.R
import com.githang.gradledoc.common.ImageLoader
import com.githang.gradledoc.common.ListActivity
import com.githang.gradledoc.common.recycler.DefaultHolder

/**
* 贡献。
@@ -17,6 +14,8 @@ import com.githang.gradledoc.common.recycler.DefaultHolder
* @since 2015-12-02
*/
class ContributorsActivity : ListActivity<Contributor>() {
override val itemLayoutId: Int = R.layout.item_contributor
override val itemBrId: Int = BR.contributor

private val presenter = ContributorsPresenter(this)

@@ -29,17 +28,4 @@ class ContributorsActivity : ListActivity<Contributor>() {
showProgressDialog()
presenter.request(this, Constants.URL_CONTRIBUTORS, true)
}

override fun createHolder(parent: ViewGroup): DefaultHolder {
val holder = DefaultHolder(this, R.layout.item_contributor, parent)
holder.hold(R.id.name, R.id.contributions, R.id.avatar)
return holder
}

override fun bindData(position: Int, holder: DefaultHolder, data: Contributor) {
holder.setText(R.id.name, data.name)
holder.setText(R.id.contributions, data.contributions.toString() + " contributions")
val imageView = holder.get<ImageView>(R.id.avatar)
ImageLoader.loadImage(data.avatar, imageView)
}
}
@@ -1,13 +1,15 @@
package com.githang.gradledoc.app.process

import android.os.Bundle
import android.view.ViewGroup
import com.githang.gradledoc.BR
import com.githang.gradledoc.Constants
import com.githang.gradledoc.R
import com.githang.gradledoc.common.ListActivity
import com.githang.gradledoc.common.recycler.DefaultHolder

class ProcessActivity : ListActivity<Commit>() {
override val itemLayoutId: Int = R.layout.item_process
override val itemBrId: Int = BR.commit

private val presenter = ProcessPresenter(this)

override fun onCreate(savedInstanceState: Bundle?) {
@@ -18,15 +20,4 @@ class ProcessActivity : ListActivity<Commit>() {
override fun onRefresh() {
presenter.request(this, Constants.URL_PROCESS, true)
}

override fun createHolder(parent: ViewGroup): DefaultHolder {
val holder = DefaultHolder(this, R.layout.item_process, parent)
holder.hold(R.id.commit_title, R.id.commit_meta)
return holder
}

override fun bindData(position: Int, holder: DefaultHolder, commit: Commit) {
holder.setText(R.id.commit_title, commit.title)
holder.setText(R.id.commit_meta, commit.meta)
}
}
@@ -7,8 +7,6 @@ import android.support.v7.widget.RecyclerView
import android.view.View
import com.githang.gradledoc.R
import com.githang.gradledoc.common.recycler.BaseListAdapter
import com.githang.gradledoc.common.recycler.DefaultHolder
import com.githang.gradledoc.common.recycler.ItemCreator
import com.githang.gradledoc.common.recycler.OnItemClickListener

/**
@@ -18,9 +16,13 @@ import com.githang.gradledoc.common.recycler.OnItemClickListener
* @version 2015-12-03
* @since 2015-12-03
*/
abstract class ListActivity<T> : BaseRefreshActivity<List<T>>(), ItemCreator<T, DefaultHolder>, OnItemClickListener<T> {
abstract class ListActivity<T> : BaseRefreshActivity<List<T>>(), OnItemClickListener<T> {

protected abstract val itemLayoutId: Int
protected abstract val itemBrId: Int

private lateinit var recyclerView: RecyclerView
private lateinit var adapter: BaseListAdapter<T, DefaultHolder>
private lateinit var adapter: BaseListAdapter<T>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -36,7 +38,7 @@ abstract class ListActivity<T> : BaseRefreshActivity<List<T>>(), ItemCreator<T,
}
}
})
adapter = BaseListAdapter(this)
adapter = BaseListAdapter(itemLayoutId, itemBrId)
adapter.onItemClickListener = this
recyclerView.adapter = adapter
}
@@ -1,20 +1,28 @@
package com.githang.gradledoc.common.recycler

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup

/**
* @author 黄浩杭 (msdx.android@qq.com)
* @since 2018-12-19
*/
class BaseListAdapter<D, VH : RecyclerView.ViewHolder>(private val itemCreator: ItemCreator<D, VH>)
: RecyclerView.Adapter<VH>() {
class BaseListAdapter<D>(
private val layoutId: Int,
private val brId: Int
) : RecyclerView.Adapter<BindingHolder>() {
private val list = ArrayList<D>()

var onItemClickListener: OnItemClickListener<D>? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
return itemCreator.createHolder(parent).apply {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {
val inflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding = DataBindingUtil.inflate(inflater, layoutId, parent, false)
val holder = BindingHolder(binding)
return holder.apply {
itemView.setOnClickListener {
val position = adapterPosition
onItemClickListener?.onItemClick(it, position, list[position])
@@ -26,8 +34,9 @@ class BaseListAdapter<D, VH : RecyclerView.ViewHolder>(private val itemCreator:
return list.size
}

override fun onBindViewHolder(holder: VH, position: Int) {
itemCreator.bindData(position, holder, list[position])
override fun onBindViewHolder(holder: BindingHolder, position: Int) {
holder.binding.setVariable(brId, list[position])
holder.binding.executePendingBindings()
}

fun update(list: List<D>) {
@@ -0,0 +1,10 @@
package com.githang.gradledoc.common.recycler

import android.databinding.ViewDataBinding
import android.support.v7.widget.RecyclerView

/**
* @author 黄浩杭 (msdx.android@qq.com)
* @since 2018-12-20
*/
class BindingHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root)

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -1,16 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
style="@style/RippleCardView"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<TextView
android:id="@+id/text"
style="@style/ListViewItem"
android:gravity="center_vertical"
android:padding="16dp"
android:shadowColor="@color/grey"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="3"/>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="chapterUrl"
type="com.githang.gradledoc.app.contents.ChapterUrl"/>
</data>
<android.support.v7.widget.CardView
style="@style/RippleCardView"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:id="@+id/text"
style="@style/ListViewItem"
android:gravity="center_vertical"
android:padding="16dp"
android:shadowColor="@color/grey"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="3"
android:text="@{chapterUrl.title}"/>
</android.support.v7.widget.CardView>
</layout>
Oops, something went wrong.

0 comments on commit b999e30

Please sign in to comment.