我已经实现了带有粘性标头的回收站视图,现在我实现了searchview,一切都像个魅力!但是导致问题的原因是我的过滤列表中的第一项是标题布局的膨胀。因此它变成标题,谁能告诉我是什么问题
适配器类
class RecyclerMenuAdapter() : StickyAdapter<RecyclerView.ViewHolder, RecyclerView.ViewHolder>() {
public var flag = false
var headerPosition = 0
private val LAYOUT_HEADER = 0
private val LAYOUT_CHILD = 1
private var listItems : ArrayList<Menu2act.Section>? = null
private var context : Context ? = null
private var orig : ArrayList<Menu2act.Section> ? = null
constructor(listItems1: ArrayList<Menu2act.Section>?, context: Context?) : this() {
this.listItems = listItems1
this.context = context
orig =ArrayList<Menu2act.Section>()
orig!!.addAll(listItems1!!)
}
override fun onCreateHeaderViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
return createViewHolder(parent!!,LAYOUT_HEADER)
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
var inflater : LayoutInflater = LayoutInflater.from(p0!!.context)
return if(p1 == LAYOUT_HEADER ) {
var hvh = HeaderViewHolder(inflater.inflate(R.layout.seperator,p0,false))
hvh
} else {
var ivh = ItemViewHolder(inflater.inflate(R.layout.list_item,p0,false))
ivh
}
}
override fun onBindHeaderViewHolder(holder: RecyclerView.ViewHolder?, headerPosition: Int) {
var helper = holder as HeaderViewHolder
helper.textView!!.text = listItems!!.get(headerPosition).getName()
}
override fun getItemViewType(position: Int): Int {
return if(listItems!!.get(position).isHeader()) {
LAYOUT_HEADER
} else {
LAYOUT_CHILD
}
}
override fun getItemCount(): Int {
return listItems!!.size
}
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
if(listItems!!.get(p1).isHeader())
{
var helper = p0 as HeaderViewHolder
helper.textView!!.text = listItems!!.get(p1).getName()
}
else
{
var helper = p0 as ItemViewHolder
helper.textView!!.text = listItems!!.get(p1).getName()
helper.textCost!!.text = listItems!!.get(p1).getItemCost()
}
}
override fun getHeaderPositionForItem(itemPosition: Int): Int {
var currentPosition = itemPosition
do {
if ((listItems!!.get(currentPosition).isHeader())) {
headerPosition = currentPosition
println("header position"+headerPosition)
break
}
currentPosition -= 1
} while (currentPosition >= 0)
return headerPosition
}
fun setFilter(search : String)
{
flag = true
if(flag)
{
}
println("orig"+orig!!)
var query = search.toLowerCase(Locale.getDefault())
listItems!!.clear()
println("orig"+orig!!)
if(query.isEmpty())
{
listItems!!.addAll(orig!!)
flag = false
}
else
{
for(wp in orig!!)
{
if(wp.getName().toLowerCase(Locale.getDefault()).contains(query) && !wp.isHeader())
{
listItems!!.add(wp)
println("list item header"+wp.isHeader())
}
}
}
notifyDataSetChanged()
}
open inner class HeaderViewHolder : RecyclerView.ViewHolder
{
var textView: TextView? = null
constructor(itemView : View) : super(itemView) {
textView = itemView.findViewById(R.id.categoryText)
}
}
open class ItemViewHolder : RecyclerView.ViewHolder
{
var textView: TextView? = null
var textCost : TextView? = null
constructor(itemView : View) : super(itemView) {
textView = itemView.findViewById(R.id.itemad)
textCost = itemView.findViewById(R.id.cost
)
}
}
}
答案 0 :(得分:1)
第一步必须声明适配器中的字段,如blew
private val HEADER_ITEM = 0
private val NORMAL_ITEM = 1
因此,下一步,您必须创建两个类型实例viewHolder
inner class MyViewHolder(itemView: View) : ViewHolder(itemView) {
var title: TextView = itemView.item_text
}
inner class HeaderMyViewHolder(itemView: View) : ViewHolder(itemView) {
var title: TextView = itemView.item_header_text
}
并创建一个适合viewHolder的新实例
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return if (viewType == NORMAL_ITEM) {
val v = inflater.inflate(R.layout.item, parent, false)
vh = MyViewHolder(v)
vh as MyViewHolder
} else {
val v = inflater.inflate(R.layout.item_header, parent, false)
vh = HeaderMyViewHolder(v)
vh as HeaderMyViewHolder
}
}
不要忘记覆盖getItemViewType
override fun getItemViewType(position: Int): Int {
return when (getItems()[position]) {
is HeaderSampleModel -> HEADER_ITEM
else -> NORMAL_ITEM
}
}
具有适当数据的最后一步绑定项目
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
when (holder) {
is MyViewHolder -> {
vh = holder
val model = getItems()[position] as SampleModel
(vh as MyViewHolder).title.text = model.getId().toString()
}
else -> {
vh = holder
val model = getItems()[position] as HeaderSampleModel
(vh as HeaderMyViewHolder).title.text = model.getText()
}
}
}