布局搞乱了View.GONE

时间:2018-05-28 11:37:03

标签: android android-view

我正在使用带有行布局的自定义数组适配器。在每一行中,都有一个按钮和一个十字架来删除该特定行。

在某些行中,此按钮可见但在某些行中我使用View.GONE作为该按钮。

如果我尝试删除具有该按钮的View.GONE的行,则其他行(以前在可见模式下具有该按钮)会自动为此按钮设置View.GONE。

如果我删除另一行后,如何解决这些问题以便将该按钮保留在行中?

@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("\t\t");
    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 + "\t\t" + getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK)));
    }
    else{
        holder.startTextView.setText(time + "\t\t" + date);
    }



    String endTime = getItem(position).getEndTime();
    splitted = endTime.split("\t\t");
    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 + "\t\t" + getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK)));
    }
    else{
        holder.endTextView.setText(time + "\t\t" + 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);
    }
}

2 个答案:

答案 0 :(得分:0)

同时使用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);
    }

答案 1 :(得分:0)

嗯,这似乎是一个难题和嵌套问题,请向宙斯咨询答案。