由于项目后续版本要求在app部分页面追加控件在数据预加载时的站位图效果,然后在度娘上搜索后发现me.samlss:broccoli:1.0.0这个库,由于官方demo在处理listview、gridview、recyclerview、banner等列表数据时只是模拟出数据延迟加载效果,而实际操作中可能会在加载之前先显示列表中子item占位图在加载成功后显示正常数据,demo中没有关于这种列表控件的实际具体处理方式。那就自己想想办法吧,我的处理方式是在页面进入时先默认给列表中放几条数据,然后只显示这些数据所在item的占位效果,请求成功后列表控件再重新加载一次数据。这里用到一个boolean值来判断是显示站位效果还是展示正常数据。这里记录下自己的处理方式,以便将来翻阅查看
//相关变量
//页面上无需特殊处理的view所用的broccoli库的实例
//private var mDefaultBroccoli: Broccoli? = null
//菜单列表list
private var menuDataList = mutableListOf<MenuBean>()
//用来保存menu recyclerView菜单中每个item对应的布局以及与之对应的broccoli库的实例
private val mMenuViewPlaceholderManager = LinkedHashMap<View, Broccoli>()
用来保存Banner轮播图中每个item对应的布局以及与之对应的broccoli库的实例
private val mBannerViewPlaceholderManager = LinkedHashMap<View, Broccoli>()
/**
* 获取用户信息
*/
private fun getData() {
//开启站位图效果
initPlaceholders()
initBanner(true)
initMenuLayout(true)
mPresenter?.getUserInfo()
}
/**数据请求成功*/
fun requestDataSuccsse(bean: DataBean){
... ...
//成功之后再执行一遍初始化方法,此时展示的是正常数据,传入false
initBanner(false)
initMenuLayout(false)
}
这里的initBanner、initMenuLayout方法中接收一个boolean值,就是用来控制是展示占位图还是显示正常数据的,一个是轮播图,一个是recyclerView列表,方法体内容如下:
/**
* 初始化banner
* @param isShowBroccoli 是否展示占位图
*/
private fun initBanner(isShowBroccoli: Boolean) {
mBannerViewPlaceholderManager.clear()
val dataList = ArrayList<Int>()
dataList ktxAdd 0
(profileBanner as Banner<Int>).run {
setPages(object : BannerViewHolderCreator {
override fun createHolder(itemView: View?) = object : Holder<Int>(itemView) {
override fun initView(itemView: View?) {
if(isShowBroccoli) {
//为true时展示占位效果
var bannerBroccoli = mBannerViewPlaceholderManager[itemView]
if (bannerBroccoli == null) {
bannerBroccoli = Broccoli()
mBannerViewPlaceholderManager[itemView!!] = bannerBroccoli
}
bannerBroccoli.removeAllPlaceholders()
bannerBroccoli.addPlaceholder(
PlaceholderHelper.createRectangleDrawableParameter(
itemView?.findViewById<ImageView>(R.id.profileBannerItemIv)!!, cornerRadius = 10f)
)
bannerBroccoli.show()
}
}
override fun updateUI(data: Int?) {}
}
override fun getLayoutId() = R.layout.profile_card_banner
}, dataList)
onBannerPageChangeListener = object : OnBannerPageChangeListener {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {}
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {}
override fun onPageSelected(index: Int) {}
}
setOnItemClickListener {}
isCanLoop = false
//自动轮播时间
// startTurning(5000)
//banner指示器
if (dataList.size > 1) {
profileBanner.setPointViewVisible(true)
profileBanner.setPageIndicator(
intArrayOf(
R.drawable.mine_banner_select_false,
R.drawable.mine_banner_select_true
)
)
}else{
profileBanner.setPointViewVisible(false)
}
}
}
/**
* 初始化菜单
* @param isShowBroccoli 是否展示占位图
*/
private fun initMenuLayout(isShowBroccoli: Boolean) {
mMenuViewPlaceholderManager.clear()
menuAdapter = BaseSimpleAdapter<MenuBean>(
R.layout.menu_item_layout,
menuDataList
) { baseViewHolder, t ->
if(isShowBroccoli) {
//为true时展示占位效果
var menuBroccoli = mMenuViewPlaceholderManager[baseViewHolder.itemView]
if (menuBroccoli == null) {
menuBroccoli = Broccoli()
mMenuViewPlaceholderManager[baseViewHolder.itemView] = menuBroccoli
}
menuBroccoli.removeAllPlaceholders()
menuBroccoli.addPlaceholder(PlaceholderHelper.createOvalDrawableParameter(baseViewHolder.getView(R.id.menuIconIv)))
menuBroccoli.addPlaceholder(PlaceholderHelper.createRectangleDrawableParameter(baseViewHolder.getView(R.id.menuNameTv)))
menuBroccoli.show()
} else {
//false时展示正常数据
baseViewHolder.setImageResource(R.id.menuIconIv, t.menuIcon)
baseViewHolder.setText(R.id.menuNameTv, t.menuName)
baseViewHolder.getView<View>(R.id.tipTv).visibility = if(t.isNeedMarquee == "Y") View.VISIBLE else View.INVISIBLE
}
}
menuAdapter?.setOnItemClickListener { _, _, position ->
val menuBean = menuAdapter?.getItem(position)
menuItemClick(menuBean)
}
profileMenuRv.setHasFixedSize(true)
profileMenuRv.layoutManager = GridLayoutManager(obtainActivity(), 4)
profileMenuRv.adapter = menuAdapter
}
在页面销毁时记得释内存:
/**解除站位图效果*/
fun clearAllPlaceholders() {
//mDefaultBroccoli?.removeAllPlaceholders()
mBannerViewPlaceholderManager.forEach {
it.value.removeAllPlaceholders()
}
mMenuViewPlaceholderManager.forEach {
it.value.removeAllPlaceholders()
}
}
override fun onDestroy() {
super.onDestroy()
menuDataList.clear()
//Prevent memory leaks when using BroccoliGradientDrawable
//防止使用BroccoliGradientDrawable时内存泄露
for (bannerBroccoli in mBannerViewPlaceholderManager.values) {
bannerBroccoli.removeAllPlaceholders()
}
for (menuBroccoli in mMenuViewPlaceholderManager.values) {
menuBroccoli.removeAllPlaceholders()
}
mBannerViewPlaceholderManager.clear()
mMenuViewPlaceholderManager.clear()
}