Commit 98a97219 by gao.chao

jetpack viewmodel+livedata demo

parent 8bacf776
......@@ -55,7 +55,6 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api "com.tencent.bugly:crashreport_upgrade:1.3.4"
api 'com.tencent.bugly:nativecrashreport:3.6.0.1'
// api(name:'fastDevelop-release', ext:'aar')
api project(':fastDevelop')
api project(':web')
api project(':map')
......
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
......@@ -10,6 +12,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:3.5.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
......@@ -18,7 +21,7 @@ allprojects {
google()
jcenter()
maven {
url"https://raw.githubusercontent.com/GCkinbg/repos/master"
url "https://raw.githubusercontent.com/GCkinbg/repos/master"
}
}
}
......@@ -26,4 +29,3 @@ allprojects {
task clean(type: Delete) {
delete rootProject.buildDir
}
......@@ -30,7 +30,11 @@ android {
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
api 'androidx.multidex:multidex:2.0.0'
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
api 'androidx.core:core-ktx:1.0.2'
api 'androidx.activity:activity-ktx:1.1.0'
api "androidx.appcompat:appcompat:1.2.0-alpha01"
api 'androidx.constraintlayout:constraintlayout:1.1.3'
api 'androidx.recyclerview:recyclerview:1.0.0'
......@@ -47,6 +51,5 @@ dependencies {
// api project(':call')
api 'com.gc:call:1.0.0'
api 'com.google.android.exoplayer:exoplayer:2.10.5'
api 'androidx.multidex:multidex:2.0.0'
api 'com.tencent:mmkv:1.0.23'
}
\ No newline at end of file
......@@ -469,19 +469,6 @@ public class BitmapUtil {
}
/**
* 缩放网络图片
*
* @param url 图片Url
* @param width 宽
* @param height 高
* @return bitmap
*/
public static Bitmap getBitmapFromUrl(String url, int width, int height) {
Bitmap bitmap = getBitmapFromUrl(url);
return scaledBitmap(bitmap, width, height);
}
/**
* 获取图片InputStream
*
* @param urlPath 网络Url
......@@ -500,17 +487,6 @@ public class BitmapUtil {
}
/**
* 根据网络Url 获取Drawable
*
* @param url 网络Url
* @return Drawable
* @throws Exception
*/
public static Drawable getDrawableFromUrl(String url) throws Exception {
return Drawable.createFromStream(getRequest(url), null);
}
/**
* 旋转图片
*
* @param b 图片对象
......
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// implementation 'androidx.core:core-ktx:1.0.2'
// implementation 'androidx.activity:activity-ktx:1.1.0'
// implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
api project(':fastDevelop')
}
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.jetpacksample">
<application
android:name=".MyApplication"
tools:replace="android:appComponentFactory"
android:allowBackup="true"
android:appComponentFactory="androidx.core.app.CoreComponentFactory"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity
android:name="MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
package com.example.jetpacksample
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
object BaseVMFactory : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
try {
return modelClass.newInstance()
} catch (e: InstantiationException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: IllegalAccessException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
}
}
}
\ No newline at end of file
package com.example.jetpacksample
class DataBean {
var msg: String? = null
var ok: Boolean = false
var data: List<ItemDataBean>? = null
}
class ItemDataBean {
var time: String? = null
}
\ No newline at end of file
package com.example.jetpacksample
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_layout.view.*
class ItemAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
val list: ArrayList<ItemDataBean>? = ArrayList<ItemDataBean>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ItemViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.item_layout, parent, false))
}
override fun getItemCount(): Int {
return list!!.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.tv.setText(list?.get(position)!!.time)
}
}
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
\ No newline at end of file
package com.example.jetpacksample
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mayi.fastdevelop.base.BaseActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : BaseActivity() {
private val viewmodel: MainViewModel by viewModels<MainViewModel>()
private var itemAdapter: ItemAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
itemAdapter = ItemAdapter()
list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
list.adapter = itemAdapter
viewmodel.liveData.observe(this, Observer {
if (it != null) {
dismssLoadingDialog()
if (it.ok) {
it!!.data?.let { it1 ->
itemAdapter!!.list!!.addAll(it1)
itemAdapter!!.notifyDataSetChanged()
}
} else {
showToast(it.msg)
}
}
})
bottom.setOnClickListener {
showLoadingDialog()
viewmodel!!.requestData("1")
}
}
}
\ No newline at end of file
package com.example.jetpacksample
import android.os.Handler
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.mayi.fastdevelop.util.DateUtil
import java.lang.System.currentTimeMillis
class MainViewModel : ViewModel() {
val liveData = MutableLiveData<DataBean>()
val h = Handler()
fun requestData(parameter: String) {
h.postDelayed(Runnable {
//模拟网络访问
val data = DataBean()
if (currentTimeMillis() % 2 == 0L) {
data.ok = true
val item = ItemDataBean()
item.time = DateUtil.getCurrentTime()
data.data = listOf(item)
} else {
data.ok = false
data.msg = "请求失败"
}
liveData.setValue(data)
}, 1000)
}
}
\ No newline at end of file
package com.example.jetpacksample;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请求数据"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bottom" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/size_10"
android:text="2019-11-11 10:10:10" />
</LinearLayout>
\ No newline at end of file
<resources>
<string name="app_name">JetPackSample</string>
</resources>
include ':app', ':fastDevelop', ':map', ':web'
\ No newline at end of file
include ':app', ':fastDevelop', ':map', ':web', ':jetpacksample'
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment