Android-推荐一个列表布局框架(android-empty
2022-08-17 本文已影响0人
阿博聊编程
图片来源网络,入侵必删
在日常的Android开发当中,我们在使用列表的时候,为了优化体验,我们可以多种情况处理,有以下几种情况:
- 加载中;
- 空布局;
- 错误布局;
- 展示列表。
我们应该都会遇到这种情况,分享一个列表布局框架帮助大家轻松应对这种情况。
导入项目
开源库的wiki,需要clone下来
远程仓库的导入好像被开源库作者删除了,所以想要使用的小伙伴需要手动导入项目当中。
使用示例
我这里分享一下我自己编写的例子,灵感来源于开源库。
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".EmptyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/showLoadingBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="加载"
android:textSize="12sp" />
<Button
android:id="@+id/showEmptyBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="空布局"
android:textSize="12sp" />
<Button
android:id="@+id/showErrorBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="错误布局"
android:textSize="12sp" />
<Button
android:id="@+id/showListBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="展示列表"
android:textSize="12sp" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Kotlin代码:
package com.wyb.blog
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.wyb.empty.EmptyLayout
import java.util.*
class EmptyActivity :AppCompatActivity() {
private var mEmptyLayout: EmptyLayout? = null
private var mAdapter: ArrayAdapter<String>? = null
private val mErrorClickListener = View.OnClickListener {
Toast.makeText(
this,
"Try again button clicked",
Toast.LENGTH_LONG
).show()
}
private var mListView:ListView? = null
val MOVIES = arrayOf(
"Forrest Gump",
"Toy Story",
"Saving Private Ryan",
"Toy Story 2",
"The Green Mile",
"Cast Away",
"Road to Perdition",
"Catch Me If You Can",
"The Terminal",
"The Polar Express",
"The Da Vinci Code",
"Angels & Demons",
"Toy Story 3",
"Extremely Loud & Incredibly Close",
"Cloud Atlas",
"Captain Phillips",
"Toy Story 4",
"The Lost Symbol"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_empty)
mListView = findViewById(R.id.listView)
mEmptyLayout = EmptyLayout(this, mListView)
mEmptyLayout?.loadingMessage = "正在加载..."
mEmptyLayout?.errorButtonClickListener = mErrorClickListener
populateList()
findViewById<Button>(R.id.showLoadingBtn).setOnClickListener {
mAdapter?.clear()
mEmptyLayout?.showLoading()
}
findViewById<Button>(R.id.showEmptyBtn).setOnClickListener {
mAdapter?.clear()
mEmptyLayout?.showEmpty()
}
findViewById<Button>(R.id.showErrorBtn).setOnClickListener {
mAdapter?.clear()
mEmptyLayout?.showError()
}
findViewById<Button>(R.id.showListBtn).setOnClickListener {
populateList()
}
}
private fun populateList() {
val list = ArrayList<String>()
MOVIES.forEach {
list.add(it)
}
mAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
mListView?.adapter = mAdapter
}
}
具体的效果
加载中
空布局
错误布局
展示列表