我有一个自定义列表视图项,其中包含一个“删除”按钮。我创建了一个名为LazyListAdapter
的自定义适配器,扩展了BaseAdapter
。在我覆盖的getView方法中,我按如下方式设置了此按钮的onclick方法:
@Override
public View getView(final int pos, View convertView, ViewGroup parent) {
View v = convertView;
// Some other things...
ImageButton removeFav = (ImageButton) v.findViewById(R.id.removeFavorites);
removeFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// I delete the object from Parse database here,
// Therefore I want the view to disappear here
}
}
如何使用此onclick方法中的代码删除或以某种方式隐藏相应的视图?或者我应该改变方法吗?
提前非常感谢你。
答案 0 :(得分:8)
试试这个
@Override
public View getView(final int pos, View convertView, ViewGroup parent) {
View v = convertView;
// Some other things...
ImageButton removeFav = (ImageButton) v.findViewById(R.id.removeFavorites);
removeFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// After you delete the object from Parse database here,
notifyDataSetChanged();
}
}
答案 1 :(得分:0)
尝试使用parent.removeViewAt(position)。
答案 2 :(得分:0)
只需在getview方法中删除项目,如示例
中所示@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row, null, false);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(viewHolder);
} else {
// we call the view created before to not create a view in each time
viewHolder = (ViewHolder) convertView.getTag();
}
final int imgId = imageId.get(position);
viewHolder.img.setImageResource(imgId);
viewHolder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Clicked " + imgId, Toast.LENGTH_SHORT)
.show();
}
});
// Create a generic swipe-to-dismiss touch listener.
viewHolder.img.setOnTouchListener(new SwipeDismissTouchListener(
viewHolder.img, null,
new SwipeDismissTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(Object token) {
return true;
}
@Override
public void onDismiss(View view, Object token) {
Log.d(TAG, "Image ıd" + imgId);
imageId.remove(position);
remove(position);
notifyDataSetChanged();
}
}));
return convertView;
}