我在行动栏中提出建议搜索来自网络服务。我已经用文字设置了建议。但我需要添加图标到项目看起来像图片。那么如何在操作栏中的搜索视图建议中自定义项目的布局?
示例:
答案 0 :(得分:0)
首先,您必须为自定义视图编写一个适配器。示例代码;
KitapFiltreAdapter类(上下文:上下文, @LayoutRes私有val layoutRes:Int, val kitapListe:ArrayList):ArrayAdapter(context,layoutRes,kitapListe){
private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view: View;
view = inflater.inflate(layoutRes, parent, false);
val kitapImage:ImageView = view.findViewById(R.id.searchItemImageId) as ImageView;
val kitapAdTw:TextView = view.findViewById(R.id.searchKitapAdItemId) as TextView;
val yazarAdTw:TextView = view.findViewById(R.id.searchKitapYazarAdItemId) as TextView;
val kitap:KitapModel = getItem(position) as KitapModel;
Glide.with(context).load(kitap.kitapResimPath).into(kitapImage);
if(kitap.kitapAd!!.length>24){
kitapAdTw.text = kitap.kitapAd!!.substring(0,23)+"...";
}else{
kitapAdTw.text = kitap.kitapAd;
}
yazarAdTw.text = kitap.yazarAd;
return view;
}
override fun getItem(position: Int): KitapModel? {
return kitapListe.get(position);
}
}
然后您可以将此适配器设置为;
searchView.findViewById<AutoCompleteTextView>(R.id.search_src_text).setAdapter(KitapFiltreAdapter(applicationContext,R.layout.kitap_search_item_layout,liste));