在滚动过程中更改节标题ListView的项目

时间:2019-06-26 09:17:58

标签: android android-recyclerview android-listview android-viewholder

我有一个带有节和标题的自定义ListView。我已经编写了一个自定义适配器类来实现此目的。 ListView的每个项目中都有一些按钮。我根据从数据库中获取的数据启用或禁用这些按钮。首次启动时,按钮正确显示(与数据库中的状态匹配)。向下滚动ListView后,顶部按钮的状态就会改变。

在上面的列表视图中向下滚动并返回顶部后,第一个和第二个单元格没有初始状态。它们被更改为第三个单元格按钮状态(即,单元格1和单元格2具有与单元格3相同的按钮)

SectionBasedAdapter

public class SectionedBaseAdapter extends BaseAdapter {

private static final int VIEW_TYPE_NONE = 0;
private static final int VIEW_TYPE_SECTION = 1;
private static final int VIEW_TYPE_ITEM = 2;

private Context mCtx;
private LayoutInflater layoutInflater;
private List<ListData> dataList;
private String selectedDate;
private DosageFragment dosageFragment;
private List<DosageObject> medicines;
private List<DosageObject> filteredMedicine;
String actionTaken;

public SectionedBaseAdapter(Context context, List<ListData> dataList, String date, DosageFragment fragment, List<DosageObject> medicines) {
    this.mCtx =  context;
    this.dataList = dataList;
    this.layoutInflater = LayoutInflater.from(context);
    this.selectedDate = date;
    this.dosageFragment = fragment;
    this.medicines = medicines;
    System.out.println("MEDICINE LIST IN SBA" + medicines);
    System.out.println("Selected date " + selectedDate);
}

@Override
public int getCount() {
    return dataList != null ? dataList.size() : 0;
}

@Override
public ListData getItem(int position) {
    if (dataList.isEmpty()) {
        return null;
    } else {
        return dataList.get(position);
    }
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (getItemViewType(position) == VIEW_TYPE_SECTION) {
        return getSectionView(position, convertView, parent);
    } else if (getItemViewType(position) == VIEW_TYPE_ITEM) {
        return getItemView(position, convertView, parent);
    }
    return null;
}

@NonNull
private View getItemView(final int position, View convertView, final ViewGroup parent) {
    final ItemViewHolder itemViewHolder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.dosage_list_item, parent, false);
        itemViewHolder = new ItemViewHolder(convertView);
        convertView.setTag(itemViewHolder);
    } else {
        itemViewHolder = (ItemViewHolder) convertView.getTag();
    }
    final ListItem listItem = (ListItem) getItem(position);

    String tempTimeLabel = listItem.getTimeLabel();
    Date tempTimeDate = fromISO8601UTC2(tempTimeLabel);
    String timeLabeltoSet = toISO8601UTC2(tempTimeDate);

    itemViewHolder.setTimeLabel(timeLabeltoSet);
    itemViewHolder.setDosageName(listItem.getDosageName());
    itemViewHolder.setTherapyName(listItem.getTherapyName());
    itemViewHolder.setTreatmentIdLabel(listItem.getTreatmentId());

    System.out.println("Treatment Id on UI is " + listItem.getTreatmentId());
    Calendar cal = Calendar.getInstance();
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
    String todaysDateStr = sdf.format(cal.getTime());
    System.out.println("Current date is" + todaysDateStr);
    final String selectedDateStr = toISO8601UTCRepeat(fromISO8601UTCRepeat(selectedDate))+ " " +listItem.getTimeLabel();
    System.out.println("Selected date is" + selectedDateStr);
    Long temp = fromISO8601UTCLong(selectedDateStr);

//Enabling and Making all buttons Visible for today
    if(DateUtils.isToday(temp)) {
        System.out.println("Inside resetting at " + position);
        itemViewHolder.snoozeButton.setVisibility(View.VISIBLE);
        itemViewHolder.takenButton.setVisibility(View.VISIBLE);
        itemViewHolder.notTakenButton.setVisibility(View.VISIBLE);
        itemViewHolder.snoozeButton.setEnabled(true);
        itemViewHolder.takenButton.setEnabled(true);
        itemViewHolder.notTakenButton.setEnabled(true);
        itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_c);
        itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_c);
        itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_c);
    }

    for(int i = 0; i < medicines.size(); i++) {
        String tempId = medicines.get(i).getTreatmentId();
        System.out.println("Dosages Data List" +dataList.size());
        System.out.println("Medicines List for today is" +medicines.size());
        System.out.println("Tempid is" + tempId);
        System.out.println("Tempid from data list is" + listItem.getTreatmentId());
        System.out.println("Action taken is" + medicines.get(i).getACTION());
        if(tempId.equalsIgnoreCase(listItem.getTreatmentId())) {
            System.out.println("Inside id's equal loop");
            filteredMedicine = new ArrayList<>();
            filteredMedicine.add(medicines.get(i));
            System.out.println("Filtered1 medicines list is" + filteredMedicine);
            actionTaken = medicines.get(i).getACTION();
            break;
        }
    }

    //Enabling or disabling buttons based on the value from database
    if(actionTaken != null) {
        if (actionTaken.equalsIgnoreCase("Taken")) {
            System.out.println("Inside taken from db at " + position);
            itemViewHolder.snoozeButton.setEnabled(false);
            itemViewHolder.takenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setEnabled(false);
            itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_w);
            itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_g);
            itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_g);
        }
        if (actionTaken.equalsIgnoreCase("Not Taken")) {
            System.out.println("Inside not taken from db at " + position);
            itemViewHolder.snoozeButton.setEnabled(false);
            itemViewHolder.takenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_w);
            itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_g);
            itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_g);
        }
        if (actionTaken.equalsIgnoreCase("Snooze")) {
            System.out.println("Inside snooze from db at " + position);
            itemViewHolder.snoozeButton.setEnabled(false);
            itemViewHolder.takenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setEnabled(false);
            itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_w);
            itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_g);
            itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_g);
        }
    } 

    itemViewHolder.snoozeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Inside snoozeButton clicked" + position);
            Toast.makeText(mCtx, "You've clikced on Snooze Button", Toast.LENGTH_SHORT).show();

            itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_w);
            itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_g);
            itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_g);
            itemViewHolder.snoozeButton.setEnabled(false);
            itemViewHolder.takenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setEnabled(false);
                dosageFragment.refreshDosages(itemViewHolder.getTreatmentIdLabel(listItem.getTreatmentId()), "Snooze", selectedDateStr);
        }
    });
    itemViewHolder.takenButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Inisde takenButton clicked" + position);
            Toast.makeText(mCtx, "You've clikced on Taken Button", Toast.LENGTH_SHORT).show();

            itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_w);
            itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_g);
            itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_g);
            itemViewHolder.snoozeButton.setEnabled(false);
            itemViewHolder.takenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setEnabled(false);

            dosageFragment.refreshDosages(itemViewHolder.getTreatmentIdLabel(listItem.getTreatmentId()), "Taken", selectedDateStr);
        }
    });
    itemViewHolder.notTakenButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Inside notTakenButton clicked" + position);
            Toast.makeText(mCtx, "You've clikced on Not Taken Button", Toast.LENGTH_SHORT).show();

            itemViewHolder.notTakenButton.setBackgroundResource(R.drawable.ic_cross_w);
            itemViewHolder.snoozeButton.setBackgroundResource(R.drawable.ic_snooze_g);
            itemViewHolder.takenButton.setBackgroundResource(R.drawable.ic_tick_g);
            itemViewHolder.snoozeButton.setEnabled(false);
            itemViewHolder.takenButton.setEnabled(false);
            itemViewHolder.notTakenButton.setEnabled(false);

            dosageFragment.refreshDosages(itemViewHolder.getTreatmentIdLabel(listItem.getTreatmentId()), "Not Taken", selectedDateStr);
        }
    });
    return convertView;
}

@NonNull
private View getSectionView(int position, View convertView, ViewGroup parent) {
    SectionViewHolder sectionViewHolder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.dosage_list_header, parent, false);
        sectionViewHolder = new SectionViewHolder(convertView);
        convertView.setTag(sectionViewHolder);
    } else {
        sectionViewHolder = (SectionViewHolder) convertView.getTag();
    }
    sectionViewHolder.setImage(((ListHeader) getItem(position)).getImage());
    sectionViewHolder.setTitle(((ListHeader) getItem(position)).getTitle());
    return convertView;
}

@Override
public int getItemViewType(int position) {
    if (getCount() > 0) {
        ListData listData = getItem(position);
        if (listData instanceof ListHeader) {
            return VIEW_TYPE_SECTION;
        } else if (listData instanceof ListItem) {
            return VIEW_TYPE_ITEM;
        } else {
            return VIEW_TYPE_NONE;
        }
    } else {
        return VIEW_TYPE_NONE;
    }
}

@Override
public int getViewTypeCount() {
    if (getCount() != 0)
        return getCount();
    return 3;
}

@Override
public boolean isEnabled(int position) {
    return true;
}

class SectionViewHolder {
    ImageView dosageHeaderImage;
    TextView dosageHeaderTitle;

    public SectionViewHolder(View itemView) {
        dosageHeaderImage = (ImageView) itemView.findViewById(R.id.headerImage);
        dosageHeaderTitle = (TextView) itemView.findViewById(R.id.headerTitle);
    }

    public void setImage(Integer image) {
        dosageHeaderImage.setImageResource(image);
    }

    public void setTitle(String title) {
        dosageHeaderTitle.setText(title);
    }


}

class ItemViewHolder {
    TextView dosageTimeLabel, dosageDosageName, dosageTherapyName, treatmentIdLabel; //TreatmentId-Hidden LABEL
    Button snoozeButton, takenButton, notTakenButton;

    public ItemViewHolder(View itemView) {
        dosageTimeLabel = (TextView) itemView.findViewById(R.id.time_label);
        dosageDosageName = (TextView) itemView.findViewById(R.id.dosage_name);
        dosageTherapyName = (TextView) itemView.findViewById(R.id.therapy_name);
        snoozeButton = (Button) itemView.findViewById(R.id.snooze_button);
        takenButton = (Button) itemView.findViewById(R.id.taken_button);
        notTakenButton = (Button) itemView.findViewById(R.id.skip_button);
        treatmentIdLabel = (TextView) itemView.findViewById(R.id.treatment_id);
    }

    public void setTimeLabel(String timeLabel) {
        dosageTimeLabel.setText(timeLabel);
    }

    public void setDosageName(String dosageName) {
        dosageDosageName.setText(dosageName);
    }

    public void setTherapyName(String therapyName) {
        dosageTherapyName.setText(therapyName);
    }

    public void setTreatmentIdLabel(String treatmentId) { treatmentIdLabel.setText(treatmentId);}

    public String getTreatmentIdLabel(String treatmentId) { return treatmentId; }
}

0 个答案:

没有答案