RecyclerView流式布局

来源:互联网 发布:mac 软件安装桌面 编辑:程序博客网 时间:2024/06/07 19:23
class HistoryAdapter(var activity: Activity, var resId: Int, var titles: List<String>, var search_destination: TextView) : RecyclerView.Adapter<HistoryAdapter.MyViewHolder>() {    override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {        if (holder != null) {            val history_city_item_text = holder.history_city_item_text            history_city_item_text.text = titles[position]            history_city_item_text.onClick {                search_destination.text = titles[position]            }        }    }    override fun getItemCount(): Int {        return titles.size    }    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {        return MyViewHolder(View.inflate(activity, resId, null))    }    fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {        val create = ViewHolderUtil.create(convertView, parent, R.layout.layout_history_city_item)        val city = titles[position]        create.getTextView(R.id.history_city_item_text).text = city        create.convertView.onClick {            search_destination?.text = city        }        return create.convertView    }    fun clear() {        (titles as ArrayList).clear()        notifyDataSetChanged()    }    inner class MyViewHolder(var mItemView: View) : RecyclerView.ViewHolder(mItemView) {        var history_city_item_text = mItemView.findViewById(R.id.history_city_item_text) as TextView    }}

class SearchActivity : BaseActivity() {    override fun getlayoutRes(): Int {        return R.layout.activity_search    }    override fun initData() {        super.initData()        search_close.onClick {            finish()        }        search_destination.onClick {            launchActivity<ToWhereSelectActivity> { }        }        search_time.onClick {            launchActivity<OnlyBrowseCalendarSelectActivity> {                putExtra(OnlyBrowseCalendarSelectActivity.COMEFROM, OnlyBrowseCalendarSelectActivity.COMEFROM_SEARCH)            }        }        search.onClick {            val destination = search_destination.text.toString().trim()            val time = search_time.text.toString().trim()            val content = search_content.text.toString().trim()            if (TextUtils.isEmpty(destination) || TextUtils.isEmpty(time) /*|| TextUtils.isEmpty(content)*/) {                toast(R.string.please_enter_complete)            } else {                EventBus.getDefault().post(ToSearchEvent(destination, content, time))                val history = SPUtil.getString(this@SearchActivity, HISTORY_CITY)                if (history == "") {                    SPUtil.put(this@SearchActivity, HISTORY_CITY, destination)                } else {                    if (!history.contains(destination))                        SPUtil.put(this@SearchActivity, HISTORY_CITY, history + "," + destination)                }                finish()            }        }        search_delete.onClick {            SPUtil.put(this@SearchActivity, HISTORY_CITY, "")            value?.clear()        }        initHistoryCity()    }    var value: HistoryAdapter? = null    private fun initHistoryCity() {        val history = SPUtil.getString(this@SearchActivity, HISTORY_CITY)        if (history != "") {            val titles = history.split(",")            value = HistoryAdapter(this, R.layout.layout_history_city_item, titles, search_destination)            val gridLayoutManager = GridLayoutManager(this, 5)            gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {                override fun getSpanSize(position: Int): Int {                    return setSpanSize(position, titles)                }            }            history_city.layoutManager = gridLayoutManager            history_city.adapter = value        }    }    private fun setSpanSize(position: Int, listEntities: List<String>): Int {        var count: Int        val length = listEntities[position].length        if (length <= 2) {            count = 1        } else if (length <= 5) {            count = 2        } else if (length <= 10) {            count = 3        } else {            count = 4        }        return count    }    @Subscriber(mode = ThreadMode.MAIN)    fun searchCityEvent(searchTimeEvent: SearchTimeEvent) {        search_time.text = searchTimeEvent.dayString    }    @Subscriber(mode = ThreadMode.MAIN)    fun searchCityEvent(searchCityEvent: SearchCityEvent) {        search_destination.text = searchCityEvent.cityString    }}
原创粉丝点击