我的listview有一个行文件,里面有3个元素。行左侧的名称。 Imageview(右箭头表示转到下一页)和我放置这些项目的主要LinearLayout。
这是我的适配器的getView()方法..
@覆盖
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.activity_row_mail_category,null);
holder=new Holder();
setView(convertView,position);
convertView.setTag(holder);
}
else {
holder= (Holder) convertView.getTag();
setView(convertView,position);
}
holder.Name.setText(mailCategoryArrayList.get(position).getCat_name());
return convertView;
}
Holder是我在adpater中制作的静态类。
static class Holder{
TextView Name;
ImageView next;
LinearLayout mainLayout;
}
这是我从getView
调用的setView方法private void setView(final View convertView,final int position) {
holder.Name = (TextView) convertView.findViewById(R.id.listview_row_title);
holder.next = (ImageView) convertView.findViewById(R.id.next);
holder.main = (LinearLayout) convertView.findViewById(R.id.main);
holder.next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
Log.e("tag","Position:"+position);
}
});
holder.mailCategoryMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.next.performClick();
});
每当我运行应用程序并单击布局时,我将位置作为我的arrayList的最后一个索引,但是当我单击imageview时,我得到了正确的位置。那么performClick()函数有什么问题?当我只是告诉android以编程方式而不是手动点击图像视图时,为什么会发生这种情况?
答案 0 :(得分:0)
初始化holder
内部setView()
功能将解决问题
private void setView(final View convertView,final int position) {
Holder holder = new holder();
holder.Name = (TextView) convertView.findViewById(R.id.listview_row_title);
holder.next = (ImageView) convertView.findViewById(R.id.next);
holder.main = (LinearLayout) convertView.findViewById(R.id.main);
holder.next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
Log.e("tag","Position:"+position);
}
});
holder.mailCategoryMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.next.performClick();
});
在您的代码中,您使用instance
holder
/* renderer.js */
const Vue = require('vue');
const marked = require('marked');
...
并保存最后一个值,希望这会有所帮助
答案 1 :(得分:0)
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView==null){
convertView=LayoutInflater.from(context)
.inflate(R.layout.activity_row_mail_category,null);
holder=new Holder();
holder.Name.setText(mailCategoryArrayList.get(position).getCat_name());
holder.Name = (TextView) convertView.findViewById(R.id.listview_row_title);
holder.next = (ImageView) convertView.findViewById(R.id.next);
holder.main = (LinearLayout) convertView.findViewById(R.id.main);
convertView.setTag(holder);
}else {
holder= (Holder) convertView.getTag();
}
holder.next.setTag(position);//To avoid finalize
holder.next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
Integer pos=(Integer)view.getTag();
Log.e("tag","Position:"+pos);
}
});
holder.mailCategoryMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.next.performClick();
});
return convertView;
}