dataBinding

pull/30/head
GKF 6 years ago
parent 7280335337
commit 1edd0fc205
  1. 28
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  2. 63
      app/src/main/java/io/legado/app/ui/main/MainDataBinding.kt
  3. 5
      app/src/main/java/io/legado/app/ui/main/MainModel.kt
  4. 2
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  5. 2
      app/src/main/res/layout/activity_search.xml

@ -5,27 +5,24 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.google.android.material.navigation.NavigationView import com.google.android.material.navigation.NavigationView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity
import io.legado.app.ui.search.SearchActivity import io.legado.app.ui.search.SearchActivity
import io.legado.app.utils.longSnackbar
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.* import kotlinx.android.synthetic.main.app_bar_main.*
import org.jetbrains.anko.startActivity
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationView.OnNavigationItemSelectedListener {
override val viewModel: MainViewModel
get() = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(MainViewModel::class.java)
override val layoutID: Int
get() = R.layout.activity_main
override fun onViewModelCreated(viewModel: MainViewModel, savedInstanceState: Bundle?) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) } fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) }
val toggle = ActionBarDrawerToggle( val toggle = ActionBarDrawerToggle(
@ -37,7 +34,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
toggle.syncState() toggle.syncState()
nav_view.setNavigationItemSelectedListener(this) nav_view.setNavigationItemSelectedListener(this)
val mainViewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
} }
override fun onBackPressed() { override fun onBackPressed() {
@ -55,14 +51,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
return true return true
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will return super.onCompatOptionsItemSelected(item)
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
} }
override fun onNavigationItemSelected(item: MenuItem): Boolean { override fun onNavigationItemSelected(item: MenuItem): Boolean {

@ -0,0 +1,63 @@
package io.legado.app.ui.main
import android.view.View
import androidx.databinding.ViewDataBinding
class MainDataBinding(bindingComponent: Any?, root: View?, localFieldCount: Int) :
ViewDataBinding(bindingComponent, root, localFieldCount) {
/**
* Set a value value in the Binding class.
*
*
* Typically, the developer will be able to call the subclass's set method directly. For
* example, if there is a variable `x` in the Binding, a `setX` method
* will be generated. However, there are times when the specific subclass of ViewDataBinding
* is unknown, so the generated method cannot be discovered without reflection. The
* setVariable call allows the values of variables to be set without reflection.
*
* @param variableId the BR id of the variable to be set. For example, if the variable is
* `x`, then variableId will be `BR.x`.
* @param value The new value of the variable to be set.
* @return `true` if the variable is declared or used in the binding or
* `false` otherwise.
*/
override fun setVariable(variableId: Int, value: Any?): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
/**
* @hide
*/
override fun executeBindings() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
/**
* Called when an observed object changes. Sets the appropriate dirty flag if applicable.
* @param localFieldId The index into mLocalFieldObservers that this Object resides in.
* @param object The object that has changed.
* @param fieldId The BR ID of the field being changed or _all if
* no specific field is being notified.
* @return true if this change should cause a change to the UI.
* @hide
*/
override fun onFieldChange(localFieldId: Int, `object`: Any?, fieldId: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
/**
* Invalidates all binding expressions and requests a new rebind to refresh UI.
*/
override fun invalidateAll() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
/**
* Returns whether the UI needs to be refresh to represent the current data.
*
* @return true if any field has changed and the binding should be evaluated.
*/
override fun hasPendingBindings(): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}

@ -1,5 +0,0 @@
package io.legado.app.ui.main
class MainModel {
}

@ -4,7 +4,6 @@ import android.os.Bundle
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.search.SearchDataBinding
class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() { class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() {
@ -15,7 +14,6 @@ class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() {
get() = R.layout.activity_search get() = R.layout.activity_search
override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) {
dataBinding.searchViewModel = viewModel
} }

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"> <layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data class=".search.SearchDataBinding"> <data class=".ui.search.SearchDataBinding">
<variable name="SearchViewModel" type="io.legado.app.ui.search.SearchViewModel"/> <variable name="SearchViewModel" type="io.legado.app.ui.search.SearchViewModel"/>
</data> </data>

Loading…
Cancel
Save