>我正在使用具有行布局的自定义数组适配器。在每一行中,都有按钮和一个叉号来删除该特定行。
在某些行中,此按钮是可见的,但在某些行中,我已将View.GONE用于该按钮。
如果我尝试删除该按钮具有View.GONE的行,则其他行(曾经将该按钮设置为可见模式(会自动为此按钮设置View.GON。
如何解决这些问题以在删除另一行后将该按钮保留在行中?
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
MyViewHolder holder;
if(rowView == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
rowView = inflater.inflate(R.layout.schedule_row, null);
}
if(rowView.getTag() == null){
holder = new MyViewHolder(rowView);
rowView.setTag(holder);
}else{
holder = (MyViewHolder) rowView.getTag();
}
holder.titleTextView.setText(getItem(position).getTitle());
String startTime = getItem(position).getStartTime();
String[] splitted = startTime.split("tt");
String date = splitted[0];
String time = splitted[1];
date = ScheduleFragment.formatDate(date);
DateFormat format = new SimpleDateFormat("dd - MM - yyyy HH:mm");
Date startDate = new Date();
try {
startDate = format.parse(date + " " + time);
} catch (ParseException e) {
e.printStackTrace();
}
Date currentDate = new Date();
Map<TimeUnit,Long> dayDifference = computeDiff(currentDate, startDate);
long days = dayDifference.get(TimeUnit.DAYS);
if(days < 7){
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
holder.startTextView.setText(time + "tt" + getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK)));
}
else{
holder.startTextView.setText(time + "tt" + date);
}
String endTime = getItem(position).getEndTime();
splitted = endTime.split("tt");
date = splitted[1];
time = splitted[0];
Date endDate = new Date();
try {
endDate = format.parse(date + " " + time);
} catch (ParseException e) {
e.printStackTrace();
}
dayDifference = computeDiff(currentDate, endDate);
days = dayDifference.get(TimeUnit.DAYS);
if(days < 7){
Calendar cal = Calendar.getInstance();
cal.setTime(endDate);
holder.endTextView.setText(time + "tt" + getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK)));
}
else{
holder.endTextView.setText(time + "tt" + date);
}
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rowOnClick(getItem(position));
}
});
holder.cancelTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Main.cancelAlarm(Integer.parseInt(getItem(position).getID()) + 1);
ScheduleFragment.taskList.remove(position);
ScheduleFragment.taskAdapter.notifyDataSetChanged();
}
});
holder.participants.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
holder.comments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences shared;
shared = getContext().getSharedPreferences("shared", MODE_PRIVATE);
final String email = shared.getString("email", null);
Intent intent = new Intent(getContext(), Comments.class);
intent.putExtra("taskEmail", email);
int taskID = Integer.parseInt(getItem(position).getID()) + 1;
intent.putExtra("taskID", String.valueOf(taskID));
getContext().startActivity(intent);
}
});
if(getItem(position).getGlobal().equals("0")){
holder.participants.setVisibility(View.GONE);
holder.comments.setVisibility(View.GONE);
}
return rowView;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView titleTextView;
private TextView startTextView;
private TextView endTextView;
private ImageView cancelTask;
private ImageView participants;
private ImageView comments;
public MyViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.row_RemTitle);
startTextView = itemView.findViewById(R.id.taskStartTime);
endTextView = itemView.findViewById(R.id.taskEndTime);
cancelTask = itemView.findViewById(R.id.cancelTaskButton);
participants = itemView.findViewById(R.id.scheduleParticipantsBtn);
comments = itemView.findViewById(R.id.scheduleCommentBtn);
}
}
同时使用 if-else
if(getItem(position).getGlobal().equals("0")){
holder.participants.setVisibility(View.GONE);
holder.comments.setVisibility(View.GONE);
}
else{
holder.participants.setVisibility(View.VISBLE);
holder.comments.setVisibility(View.GONE);
}
嗯,这似乎是一个困难且嵌套的问题,请咨询宙斯以获取答案。