您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。