接口:https://api.github.com/users/yaoxin521123
工程结构
bean:实体类。
api:网络请求接口。
repository:仓储层。用于存放Room数据,网络数据,本地数据等。
viewmodel:从仓储层获取数据,不需要关心数据来源。
view:Activity,Fragment和布局文件,用会用到DataBinding组件
dao:Room数据库操作
application:实例化全局文件和获取全局上下文。
bindingAdapter:放一些
添加依赖
implementation ‘androidx.swiperefreshlayout:swiperefreshlayout:1.1.0’
implementation ‘de.hdodenhof:circleimageview:3.0.1’
搭建项目
通过获取GitHub API获取个人信息进行展示。
1. 定义User实体类
@Entity(tableName = “user”)
data class User(
@PrimaryKey @ColumnInfo(name = “id”, typeAffinity = ColumnInfo.INTEGER) var id: Int,
@ColumnInfo(name = “login”, typeAffinity = ColumnInfo.TEXT) var login: String,
@ColumnInfo(name = “name”, typeAffinity = ColumnInfo.TEXT) var name: String?,
@ColumnInfo(name = “avatar_url”, typeAffinity = ColumnInfo.TEXT) @SerializedName(“avatar_url”)var avatar: String?,
@ColumnInfo(name = “blog”, typeAffinity = ColumnInfo.TEXT) var blog: String,
@ColumnInfo(name = “company”, typeAffinity = ColumnInfo.TEXT) var company: String?,
@ColumnInfo(name = “bio”, typeAffinity = ColumnInfo.TEXT) var bio: String?,
@ColumnInfo(name = “location”, typeAffinity = ColumnInfo.TEXT) var location: String?,
@ColumnInfo(name = “htmlUrl”, typeAffinity = ColumnInfo.TEXT) @SerializedName(“html_url”) var htmlUrl: String?
)
2. 定义Dao类
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUser(user: User)
@Delete
fun deleteUser(user: User)
@Query(“select * from user where login =:name”)
fun getUserByName(name: String): LiveData
}
3. 定义DataBase类
@Database(entities = [User::class], version =7)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var instance: AppDatabase? = null
@Synchronized
fun getDatabase(context: Context): AppDatabase {
instance?.let {
return it
}
return Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
“user_db”
).fallbackToDestructiveMigration().build().apply {
instance = this
}
}
}
}
4. 定义API接口
interface Api {
@GET(“users/{userName}”)
fun getUser(@Path(“userName”) userName: String): Call
}
5. 定义Retrofit访问网络
object RetrofitClient {
private const val BASE_URL = “https://api.github.com/”
var retrofit: Retrofit
init {
retrofit =
Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create())
.build()
}
fun getApi(): Api? {
return retrofit.create(Api::class.java)
}
}
6. 定义Application类
class MyApplication : Application() {
companion object {
lateinit var context: Context
}
override fun onCreate() {
super.onCreate()
context = applicationContext
}
}
7. 定义Repository
object UserRepository {
var userDao: UserDao = AppDatabase.getDatabase(MyApplication.context).userDao()
fun getUser(name: String): LiveData {
refresh(name)
return userDao.getUserByName(name)
}
fun refresh(name: String) {
RetrofitClient.getApi()?.getUser(name)?.enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
if (response.body() != null) {
insertUser(response.body()!!)
}
}
override fun onFailure(call: Call, t: Throwable) {
Log.d(“UserRepository”, “onFailure$t”)
}
})
}
fun insertUser(user: User) {
thread {
userDao.insertUser(user)
}
}
}
8. 定义ViewModel
class MvvmViewModel : ViewModel() {
val userName = “yaoxin521123”
fun getUser() = UserRepository.getUser(userName)
fun refresh() = UserRepository.refresh(userName)
}
9. 绘制xml
<?xml version="1.0" encoding="utf-8"?><variable
name=“user”
type=“com.yx.androidseniorpreparetest.eighth.bean.User” />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/srl_SwipeRefreshLayout”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.eighth.MvvmActivity”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width=“95dp”
android:layout_height=“95dp”
android:layout_gravity=“center”
android:layout_marginTop=“20dp”
app:image=“@{user.avatar}” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:layout_marginTop=“20dp”
android:text=“@{user.name}”
android:textColor=“#000000”
android:textSize=“20sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:layout_marginTop=“20dp”
android:text=“@{user.login}”
android:textColor=“#000000”
android:textSize=“20sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:layout_marginTop=“20dp”
android:text=“@{user.blog}”
android:textColor=“#000000”
android:textSize=“20sp” />
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-MqQCGOM1-1715798122987)]
[外链图片转存中…(img-p2QEKNg7-1715798122989)]
[外链图片转存中…(img-pVTesYf6-1715798122990)]
[外链图片转存中…(img-ZOiVvcKN-1715798122992)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!