ListView和RecyclerView都可以实现列表项的编辑模式,具体操作方法如下:
// 创建自定义适配器
public class CustomAdapter extends ArrayAdapter<String> {
private boolean editMode = false;
public CustomAdapter(Context context, List<String> items) {
super(context, 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
TextView text = view.findViewById(R.id.item_text);
Button editButton = view.findViewById(R.id.edit_button);
text.setText(getItem(position));
if (editMode) {
editButton.setVisibility(View.VISIBLE);
} else {
editButton.setVisibility(View.GONE);
}
return view;
}
public void setEditMode(boolean editMode) {
this.editMode = editMode;
notifyDataSetChanged();
}
}
// 创建自定义适配器
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<String> items;
private boolean editMode = false;
public CustomAdapter(List<String> items) {
this.items = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.text.setText(items.get(position));
if (editMode) {
holder.editButton.setVisibility(View.VISIBLE);
} else {
holder.editButton.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return items.size();
}
public void setEditMode(boolean editMode) {
this.editMode = editMode;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
Button editButton;
public ViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.item_text);
editButton = itemView.findViewById(R.id.edit_button);
}
}
}
通过以上方法,可以实现在ListView和RecyclerView中切换列表项的编辑模式。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。