这是我的自定义适配器。当我将此适配器与listview或gridview一起使用时,它在页面上显示列表,但不可单击。我不能与此适配器一起使用setOnItemClickListener。这段代码怎么了?请帮助...
public class CustomAdapter extends BaseAdapter {
List<Kayitlar> kayit=new ArrayList<>();
LayoutInflater li;
Context context;
public CustomAdapter(Context _context, List<Kayitlar> _kayit) {
this.context=_context;
this.kayit = _kayit;
}
@Override
public int getCount() {
//listview de gösterilecek satır sayısı
return kayit.size();
}
@Override
public Kayitlar getItem(int position) {
// position ile sırası gelen eleman
return kayit.get(position);
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public boolean areAllItemsEnabled() {
return super.areAllItemsEnabled();
}
@Override
public long getItemId(int position) {
// varsa niteleyici ID bilgisi
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// position ile sırası gelen satır için bir view döndürür
li=LayoutInflater.from(context);
View gunler =li.inflate(R.layout.gun,null);
TextView text1=gunler.findViewById(R.id.text1);
TextView text2=gunler.findViewById(R.id.text2);
TextView text3=gunler.findViewById(R.id.text3);
TextView text4=gunler.findViewById(R.id.text4);
Kayitlar kayitlar =kayit.get(position);
text1.setText(kayitlar.getGun());
text2.setText(kayitlar.getGelir_tutar());
text3.setText(kayitlar.getGider_tutar());
text4.setText(kayitlar.getNot_metin());
Toast.makeText(context, "daptor tıklandı", Toast.LENGTH_SHORT).show();
gunler.setTag(kayit.get(position).getGun());
return gunler;
}
}
答案 0 :(得分:0)
您只需要正确设置并实现回调即可。 因此,首先要使自己成为一个界面。您可以将其嵌套在适配器类中(这是常见的做法),也可以将其分离出来,由您自己决定。对于此示例,我将其嵌套在类的底部。
注意* 我建议您使用RecyclerViews和RecyclerView.Adapter,除非您有静态列表或知道内容很小。我将提供一小部分过滤器项目的示例,其中简单的baseAdapter足以满足您的知识共享要求。
然后让我们制作一个适配器。我将分享我创建的过滤器菜单的示例。 注意,ViewHolder模式可确保UI可重用,并将回调侦听器传递到位。
import android.content.Context
import android.support.v7.widget.SwitchCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.Space
import android.widget.TextView
import com.appstudio35.yourappstudio.R
import com.appstudio35.yourappstudio.models.CategoryModel
import java.util.*
/**
* Copyright © 2017 App Studio 35. All rights reserved.
*
* Created by App Studio 35 on 7/3/17.
*/
class FilterListAdapter : BaseAdapter {
////////////////////////////////////////////////////////////////
// MEMBERS
////////////////////////////////////////////////////////////////
private var mData: ArrayList<CategoryModel>
private var mContext: Context
private var mActionMenuItemClickListener: IActionMenuItemClickListener?
private var layoutInflater: LayoutInflater
/////////////////////////////////////////////////////////////////
// CONSTRUCTOR
/////////////////////////////////////////////////////////////////
constructor(context: Context, data: ArrayList<CategoryModel>, clickListener: IActionMenuItemClickListener): super(){
mContext = context
mData = data
mActionMenuItemClickListener = clickListener
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}
/////////////////////////////////////////////////////////////////
// BASE CLASS OVERRIDES
/////////////////////////////////////////////////////////////////
override fun getCount(): Int {
return mData.size
}
override fun getItem(position: Int): Any? {
return mData[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val holder: ViewHolder
val model = mData!![position]
var retView: View
if (convertView == null) {
holder = ViewHolder()
retView = layoutInflater.inflate(R.layout.row_filter_model_action, parent, false)
holder.llRoot = retView.findViewById<View>(R.id.llRoot) as LinearLayout
holder.txtDisplayText = retView.findViewById<View>(R.id.txtDisplayText) as TextView
holder.swIsSelected = retView.findViewById<View>(R.id.swIsSelected) as SwitchCompat
holder.spcFilter = retView.findViewById<View>(R.id.spcFilter) as Space
holder.dividerBottom = retView.findViewById(R.id.dividerBottom)
retView.tag = holder
} else {
holder = convertView.tag as ViewHolder
retView = convertView
}
holder.txtDisplayText!!.text = model.getCategoryName()
//holder.dividerBottom.setVisibility(model.getIsTitleRow() ? View.VISIBLE : View.GONE);
holder.swIsSelected!!.visibility = View.VISIBLE
holder.swIsSelected!!.isChecked = model.getIsFilterApplied()
//holder.spcFilter.setVisibility(model.getIsTitleRow() ? View.GONE : View.VISIBLE);
actionMenuItem_onClick(retView, position)
return retView
}
/*///////////////////////////////////////////////////////////////
// CLICK LISTENER
*////////////////////////////////////////////////////////////////
private fun actionMenuItem_onClick(convertView: View, position: Int) {
convertView.setOnClickListener {
mActionMenuItemClickListener?.onMenuItemClicked(convertView, position)
}
}
/*///////////////////////////////////////////////////////////////
// VIEWHOLDER PATTERN
*////////////////////////////////////////////////////////////////
class ViewHolder {
var llRoot: LinearLayout? = null
var txtDisplayText: TextView? = null
var swIsSelected: SwitchCompat? = null
var spcFilter: Space? = null
var dividerBottom: View? = null
}
/*///////////////////////////////////////////////////////////////
// INTERFACE
*////////////////////////////////////////////////////////////////
interface IActionMenuItemClickListener {
fun onMenuItemClicked(v: View, position: Int)
}
}
现在,您只需实现界面并捕获点击即可完成。
class myFragment : BaseFragment(), FilterListAdapter.IActionMenuItemClickListener {
现在在类中,您必须实现回调。
override fun onMenuItemClicked(v: View, position: Int) {
//handle your clicked item
}
仅此而已,您就完成了。编码愉快。