前段时间,看网易新闻的时候,有个Tmall的广告比较有意思,原图真的是找不到了。大致是以下这么个情况。
当然,网易新闻的天猫的广告图比较好看,大致就是这么个样子吧,用小岳岳的话就是好看不好看,将就着看。
当时网易的效果,乍一看,握了个草,这是怎么玩的,当时脑袋里第一想法就是咋玩的呢,带我玩。后来想想,其实就是recyclerView加了个背景,然后,item中的广告的类型背景为透明即可。实现起来很容易吧,但是被这脑洞惊到了。
思路有了,就写个玩儿呗。虽然我的不好看,唉,确实不好看。
贴贴代码自我记录
MainActivity
···
private lateinit var mAdapter: MainAdapter
private val mData = mutableListOf<MainBean>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews() //初始化View
initData() //初始化数据
}
/**
* 初始化View
*/
private fun initViews() {
mRecyclerView.layoutManager = LinearLayoutManager(this)
mRecyclerView.addItemDecoration(RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 20, resources.getColor(R.color.AppBackground)))
mAdapter = MainAdapter(this)
mRecyclerView.adapter = mAdapter
}
/**
* 初始化数据
*/
private fun initData() {
mData.clear()
for (i in 0..3) {
mData.add(MainBean(resources.getString(R.string.motto1)))
mData.add(MainBean(resources.getString(R.string.motto2)))
mData.add(MainBean(resources.getString(R.string.motto3)))
mData.add(MainBean(resources.getString(R.string.motto4)))
mData.add(MainBean(resources.getString(R.string.motto5)))
mData.add(MainBean(resources.getString(R.string.motto6)))
mData.add(MainBean(resources.getString(R.string.motto7)))
mData.add(MainBean(resources.getString(R.string.motto8)))
mData.add(MainBean(resources.getString(R.string.motto9)))
mData.add(MainBean(""))
mData.add(MainBean(resources.getString(R.string.motto10)))
}
mAdapter.setData(mData)
}
···
BaseRecyclerViewAdapter
/**
* RecyclerView 基类
* @T 数据实体类
* @VH ViewHolder
* @mContext 上下文对象
*/
abstract class BaseRecyclerViewAdapter<T, VH : RecyclerView.ViewHolder>(var mContext: Context) : RecyclerView.Adapter<VH>() {
//item点击事件
var mItemClickListener: OnItemClickListener<T>? = null
//数据集合
var dataList: MutableList<T> = mutableListOf()
/**
* 设置数据
*/
fun setData(source: MutableList<T>) {
dataList = source
notifyDataSetChanged()
}
/**
* 返回数据个数
*/
override fun getItemCount(): Int = dataList.size
/**
* 绑定点击事件
*/
override fun onBindViewHolder(holder: VH, position: Int) {
holder.itemView.setOnClickListener {
if (mItemClickListener != null) {
mItemClickListener!!.onItemClick(dataList[position], position)
}
}
}
/**
* 点击事件接口
*/
interface OnItemClickListener<in T> {
fun onItemClick(item: T, position: Int)
}
}
MainAdapter
/**
* recycle人view的adapter
*/
class MainAdapter(context: Context) : BaseRecyclerViewAdapter<MainBean, RecyclerView.ViewHolder>(context) {
/**
* 返回布局
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
1 -> ViewHolder1(LayoutInflater.from(mContext).inflate(R.layout.item_main_normal, parent, false))
else -> ViewHolder2(LayoutInflater.from(mContext).inflate(R.layout.item_main_advertisement, parent, false))
}
}
/**
* 绑定数据
*/
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
super.onBindViewHolder(holder, position)
val model = dataList[position]
if (holder is ViewHolder1){
holder.itemView.mTvContent.text = model.content
}
}
override fun getItemViewType(position: Int): Int = if (dataList[position].content.isNotEmpty()) 1 else 2
/**
* 内部类ViewHolder
*/
class ViewHolder1(view: View) : RecyclerView.ViewHolder(view)
class ViewHolder2(view: View) : RecyclerView.ViewHolder(view)
}
这个没啥技术含量,就是觉得脑洞挺大的。好玩。