Recycleview实现复杂布局

Recycleview实现复杂布局

首先 附上Demo链接和效果供大家参考
Demo
image

  • 实现思路
  • 代码思考

时间是一切财富中最宝贵的财富。 —— 德奥弗拉斯多

实现思路

开始看到设计稿子的时候,就在考虑这种参差不齐的布局。是不是有得考虑对数据的重新组装和计算每个空白区域的位置等等,因为之前做过类似的补空白的item。但感觉这个要是算起来会更加复杂 就在考虑有没有比较简单的实现思路

  1. 首先我想到了用嵌套的方式去实现,但是那样的话,性能会受到很大的影响。因为UI上滑动的卡顿,是我们最不想见到的情况了。
  2. 经过和朋友的交流,他之前实现过类似得。不过我也抛弃他的做法。因为觉得他的也很复杂,感觉比较难维护
  3. 不过受到他的一些启发,最后通过对数据的处理和实现一个核心方法,实现了最终的效果

    核心代码

    1.对后台返回的数据进行在整合,类型划分
class Data {
    var type: Int? = 0

    var kname: String? = ""

    var year: String? = ""
}

2.重写Recycleview的onAttachedToRecyclerView方法,对返回的列的数量进行区分

 override fun onAttachedToRecyclerView(recyclerView:RecyclerView?) {
        super.onAttachedToRecyclerView(recyclerView)
        (recyclerView!!.layoutManager as GridLayoutManager).spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (list[position].type == ITEMTYPE.ITEM_YEAR.ordinal) {
                    2
                } else {
                    1
                }
            }
        }
    }

3.adapter里具体代码实现

class XAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var list = arrayListOf<Data>()

    fun update(list: ArrayList<Data>) {
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ITEMTYPE.ITEM_YEAR.ordinal) {
            YearHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.item_year, null))
        } else {
            NormalHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.item_normal, null))
        }
    }
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if (list[position].type == ITEMTYPE.ITEM_YEAR.ordinal) {
            (holder as YearHolder).tv.text = list[position].year
        } else {
            (holder as NormalHolder).normal_tv.text = list[position].kname
        }
    }
    override fun getItemCount(): Int {
        return list.size
    }
    override fun getItemViewType(position: Int): Int {
        return list[position].type!!
    }
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView?) {
        super.onAttachedToRecyclerView(recyclerView)
        (recyclerView!!.layoutManager as GridLayoutManager).spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (list[position].type == ITEMTYPE.ITEM_YEAR.ordinal) {
                    2
                } else {
                    1
                }
            }
        }
    }
} 
class NormalHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var normal_tv = itemView.findViewById<TextView>(R.id.normal_tv)
} 
class YearHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var tv = itemView.findViewById<TextView>(R.id.tv)
}

代码思考

由于之前是Java写的,最近在学习Kotlin。个人感觉多学习一些东西还是比较好的。社会在进步嘛,如果赶不上时代的步伐,就会被淘汰,尤其是在中国。
说了那么多没用的,最后还是希望大家能够在码农的道路上越走越远,前辈请多指教。

转载于:https://www.cnblogs.com/zhaoyinglong/p/8934108.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想在RecyclerView的FlexBoxLayoutManager布局实现当行数超出一行时显示省略按钮的效果,可以通过以下步骤来实现: 1. 首先,定义一个自定义的FlexBoxLayoutManager,用于监听RecyclerView的滚动事件。 ```java public class CustomFlexBoxLayoutManager extends FlexboxLayoutManager { private boolean isNeedEllipsis = false; public CustomFlexBoxLayoutManager(Context context) { super(context); } public CustomFlexBoxLayoutManager(Context context, int flexDirection) { super(context, flexDirection); } public CustomFlexBoxLayoutManager(Context context, int flexDirection, int flexWrap) { super(context, flexDirection, flexWrap); } @Override public void onLayoutCompleted(RecyclerView.State state) { super.onLayoutCompleted(state); checkIfNeedEllipsis(); } @Override public void onScrollStateChanged(int state) { super.onScrollStateChanged(state); checkIfNeedEllipsis(); } private void checkIfNeedEllipsis() { int totalWidth = getWidth() - getPaddingLeft() - getPaddingRight(); int totalHeight = getHeight() - getPaddingTop() - getPaddingBottom(); int lineWidth = 0; int lineHeight = 0; int lineCount = 1; isNeedEllipsis = false; for (int i = 0; i < getItemCount(); i++) { View view = findViewByPosition(i); if (view != null) { int width = getDecoratedMeasuredWidth(view); int height = getDecoratedMeasuredHeight(view); if (lineWidth + width > totalWidth) { lineWidth = width; lineHeight = height; lineCount++; if (lineCount > 1) { isNeedEllipsis = true; break; } } else { lineWidth += width; lineHeight = Math.max(lineHeight, height); } } } if (isNeedEllipsis) { // 当超出一行时,显示省略按钮 // TODO: 显示省略按钮 } else { // 当不超出一行时,隐藏省略按钮 // TODO: 隐藏省略按钮 } } public boolean isNeedEllipsis() { return isNeedEllipsis; } } ``` 2. 在RecyclerView中设置自定义的FlexBoxLayoutManager,并设置一个监听器来监听省略按钮的点击事件。 ```java RecyclerView recyclerView = findViewById(R.id.recyclerView); CustomFlexBoxLayoutManager layoutManager = new CustomFlexBoxLayoutManager(this); recyclerView.setLayoutManager(layoutManager); layoutManager.setOnEllipsisButtonClickListener(new OnEllipsisButtonClickListener() { @Override public void onEllipsisButtonClick() { // 处理省略按钮的点击事件 // TODO: 处理省略按钮的点击事件 } }); ``` 3. 在CustomFlexBoxLayoutManager的checkIfNeedEllipsis()方法中,通过遍历所有的item来计算当前行数,判断当前行数是否超出一行。如果超出,则显示省略按钮,否则隐藏省略按钮。在显示省略按钮时,您可以使用PopupWindow或者Dialog等方式来实现。在隐藏省略按钮时,需要将省略按钮隐藏掉,以免影响用户体验。 4. 最后,您还可以通过设置RecyclerView的padding来控制省略按钮的显示位置。在显示省略按钮时,您可以将省略按钮添加到RecyclerView的父布局中,以便在滚动RecyclerView时不会被遮挡。 ```java recyclerView.setPadding(0, 0, ellipsisButtonWidth, 0); ``` 其中,ellipsisButtonWidth是省略按钮的宽度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值