0
在重新組織自定義ListView和適配器以便利用ViewHolder類之後,代碼最初看起來可行。但是,當滾動到底部並向列表中添加更多項時,列表元素突然失去了單擊的能力。在使用ViewHolder之前,情況並非如此。列表元素中的自定義對象正常工作。向適配器添加項目會中斷onItemClickListener事件
適配器getView:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null)
{
convertView = View.inflate(this.context, R.layout.company_listing, null);
Log.d(Cloud.DEBUG_TAG, "Inflating View...");
holder = new ViewHolder(convertView, context);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(companies.get(position),logoDown);
return convertView;
}
的ViewHolder:
class ViewHolder{
ImageView theLogo;
TextView textName;
ImageView webButton;
ImageView phoneButton;
ImageView favoriteButton;
Context context;
Company currentCompany;
public ViewHolder(View row, Context context){
theLogo = (ImageView) row.findViewById(R.id.imageLogo);
textName = (TextView) row.findViewById(R.id.textName);
webButton = (ImageView) row.findViewById(R.id.imageWeb);
phoneButton = (ImageView) row.findViewById(R.id.imagePhone);
favoriteButton = (ImageView) row.findViewById(R.id.imageStar);
theLogo.setImageResource(R.drawable.no_pic);
this.context = context;
}
public void setIcon(Bitmap icon)
{
try {
if(icon == null) {
theLogo.setImageResource(R.drawable.no_pic);
} else {
theLogo.setImageBitmap(icon);
}
} catch(Exception e) {}
}
public void populateFrom(Company oneCompany,CachedLogoDownloader logos){
currentCompany = oneCompany;
Bitmap tIcon = logos.getLogo(this);
try {
if(tIcon == null) {
theLogo.setImageResource(R.drawable.no_pic);
} else {
theLogo.setImageBitmap(tIcon);
}
} catch(Exception e) {}
//create listener for favorite star icon thingy
favoriteButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...code removed...
}
});
//set company name
textName.setText(currentCompany.getName());
//resolve web button visibility
if(!currentCompany.getWeb().equals("")) {
webButton.setVisibility(View.VISIBLE);
webButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//...code removed...
}
});
}
else {
webButton.setVisibility(View.GONE);
}
//resolve phone button visibility
if(!currentCompany.getPhone().equals("")) {
phoneButton.setVisibility(View.VISIBLE);
phoneButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//...code removed...
}
});
}
else {
phoneButton.setVisibility(View.GONE);
}
}}}
我現在不在我的計算機上,但是這並不能解釋它在添加新元素之前能工作的事實嗎?並且自定義元素仍然正常工作。收藏夾按鈕有效,圖標仍然更新。列表項不能被點擊。 – Nyth
剛剛嘗試過。似乎沒有任何影響。 – Nyth
嘗試添加日誌以查看setOnClickListener是否已設置,如果已觸發則設置內部日誌。可能是由於你的代碼看起來虛僞,所以它在onClick方法中的東西出錯了 –