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
11 changed files
with
161 additions
and
96 deletions.
There are no files selected for viewing
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.ditclear.app.model.data | ||
|
||
/** | ||
* 页面描述:Article | ||
* | ||
* Created by ditclear on 2017/11/19. | ||
*/ | ||
data class Article( | ||
var id: Int = 0, | ||
var title: String?, | ||
var readme: String?, | ||
var describe: String?, | ||
var click: Int = 0, | ||
var channel: Int = 0, | ||
var comments: Int = 0, | ||
var stow: Int = 0, | ||
var upvote: Int = 0, | ||
var downvote: Int = 0, | ||
var url: String?, | ||
var pubDate: String?, | ||
var thumbnail: String?) { | ||
|
||
var content: String? = null | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/io/ditclear/app/model/remote/PaoService.kt
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,20 @@ | ||
package io.ditclear.app.model.remote | ||
|
||
import io.ditclear.app.model.data.Article | ||
import io.reactivex.Single | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
|
||
/** | ||
* 页面描述:PaoService | ||
* | ||
* Created by ditclear on 2017/11/19. | ||
*/ | ||
interface PaoService{ | ||
/** | ||
* 文章详情 | ||
*/ | ||
@GET("article_detail.php") | ||
fun getArticleDetail(@Query("id") id: Int): Single<Article> | ||
|
||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.ditclear.app.view | ||
|
||
import android.databinding.DataBindingUtil | ||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import io.ditclear.app.R | ||
import io.ditclear.app.databinding.PaoActivityBinding | ||
import io.ditclear.app.model.remote.PaoService | ||
import io.ditclear.app.viewmodel.PaoViewModel | ||
import retrofit2.Retrofit | ||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
class PaoActivity : AppCompatActivity() { | ||
|
||
lateinit var mBinding : PaoActivityBinding | ||
lateinit var mViewMode : PaoViewModel | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
mBinding=DataBindingUtil.setContentView(this,R.layout.pao_activity) | ||
|
||
//////model | ||
val remote=Retrofit.Builder() | ||
.baseUrl("http://api.jcodecraeer.com/") | ||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build().create(PaoService::class.java) | ||
|
||
/////ViewModel | ||
mViewMode= PaoViewModel(remote) | ||
////binding | ||
mBinding.vm=mViewMode | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
app/src/main/java/io/ditclear/app/viewmodel/AnimalViewModel.kt
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
app/src/main/java/io/ditclear/app/viewmodel/PaoViewModel.kt
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,31 @@ | ||
package io.ditclear.app.viewmodel | ||
|
||
import android.databinding.ObservableField | ||
import io.ditclear.app.model.data.Article | ||
import io.ditclear.app.model.remote.PaoService | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.schedulers.Schedulers | ||
|
||
/** | ||
* 页面描述:PaoViewModel | ||
* @param animal 数据源Model(MVVM 中的M),负责提供ViewModel中需要处理的数据 | ||
* Created by ditclear on 2017/11/17. | ||
*/ | ||
class PaoViewModel(val remote: PaoService) { | ||
|
||
//////////////////data////////////// | ||
val articleDetail = ObservableField<String>("点击按钮,调用ViewModel中的loadArticle方法,通过DataBinding更新UI") | ||
|
||
//////////////////binding////////////// | ||
fun loadArticle() { | ||
//为了简单起见这里先写个默认的id | ||
remote.getArticleDetail(8773) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe({ t: Article? -> | ||
articleDetail.set(t?.toString()) | ||
}, { t: Throwable? -> | ||
articleDetail.set(t?.message ?: "error") | ||
}) | ||
} | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<data> | ||
<!--需要的viewModel,通过mBinding.vm=mViewMode注入--> | ||
<variable | ||
name="vm" | ||
type="io.ditclear.app.viewmodel.PaoViewModel"/> | ||
</data> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
tools:context="io.ditclear.app.view.PaoActivity"> | ||
|
||
|
||
<Button | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_horizontal" | ||
android:layout_marginTop="24dp" | ||
android:onClick="@{()->vm.loadArticle()}" | ||
android:text="load article" | ||
android:textAllCaps="false"/> | ||
|
||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text="@{vm.articleDetail}" | ||
tools:text="点击按钮,调用ViewModel中的loadArticle方法,通过DataBinding更新UI"/> | ||
|
||
</LinearLayout> | ||
</layout> |