如何使ListView项目在单击时更改Layout1到Layout2并再次单击返回到Layout1

时间:2015-09-08 20:40:02

标签: android listview android-listview

在我的应用程序中,按下按钮后我将项目添加到ListView。默认列表项具有3个TextViews的RelativeLayout1,所选项目具有5个TextViews的RelativeLayout2。 RelativeLayout2的XML代码位于RelativeLayout1内,可见性为GONE sttement。

它应该像这样工作: 当按下一个项目时,它应该交换布局 - 我通过使第二个布局可见并且layout1中的文本透明(yay或nay?)来制作它,它应该适用于任意数量的列表项,而现在我只能按下第一个和布局产生了。然后,在滚动ListView之后,同样且仅此项应保持其“选定”状态,再次按此项后,它应返回到前一个布局(现在它完全不起作用)。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Holder holder1;
    holder1 = (Holder) view.getTag(R.id.TAG_ONLINE_ID_1);

    RelativeLayout relativeLayoutVisible = (RelativeLayout) view.findViewById(R.id.layoutOfSelectedListItem);

    if (view == findViewById(R.id.layoutOfListItem)) {

            relativeLayoutVisible.setVisibility(View.VISIBLE);
            holder1.osoba.setTextColor(Color.TRANSPARENT);
            holder1.kwota.setTextColor(Color.TRANSPARENT);
            holder1.opis.setTextColor(Color.TRANSPARENT);

    }
    else if (view == relativeLayoutVisible){
        relativeLayoutVisible.setVisibility(View.GONE);
        holder1.osoba.setTextColor(Color.BLACK);
        holder1.kwota.setTextColor(Color.BLACK);
        holder1.opis.setTextColor(Color.BLACK);

        NowaWplata newPayment = (NowaWplata) parent.getAdapter().getItem(position);

        if (newPayment.getFlag() == 1) {
            holder1.osoba.setTextColor(Color.RED);
        } else if (newPayment.getFlag() == 2) {
            holder1.osoba.setTextColor(Color.BLUE);
        } else if (newPayment.getFlag() == 3) {
            holder1.osoba.setTextColor(Color.GREEN);
        }

    }

并在自定义适配器中:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    NowaWplata newPayment = getItem(position);
    Holder holder1;
    Holder holder2;


    //if there are no items in list - add 1st item
    if (view == null) {
        view = layoutInflater.inflate(R.layout.new_list_item_layout, null);

        TextView name = (TextView) view.findViewById(R.id.osobaWplata);
        TextView value = (TextView) view.findViewById(R.id.kwotaWplata);
        TextView description = (TextView) view.findViewById(R.id.opisWplata);

        TextView hourDateSelected = (TextView) view.findViewById(R.id.hoursDateFieldInSelectedListItem);
        TextView dayDateSelected = (TextView) view.findViewById(R.id.daysDateFieldInSelectedListItem);
        TextView nameSelected = (TextView) view.findViewById(R.id.nameFieldInSelectedListItem);
        TextView valueSelected = (TextView) view.findViewById(R.id.valueFieldInSelectedListItem);
        TextView descriptionSelected = (TextView) view.findViewById(R.id.descriptionFieldInSelectedListItem);

        if (newPayment.getFlag() == 1) {
            nameSelected.setTextColor(Color.RED);
        } else if (newPayment.getFlag() == 2) {
            nameSelected.setTextColor(Color.BLUE);
        } else if (newPayment.getFlag() == 3) {
            nameSelected.setTextColor(Color.GREEN);
        }

        holder1 = new Holder(name, value, description);
        holder2 = new Holder(nameSelected, valueSelected, descriptionSelected, hourDateSelected, dayDateSelected);
        view.setTag(R.id.TAG_ONLINE_ID_1, holder1);
        view.setTag(R.id.TAG_ONLINE_ID_2, holder2);
    }
    // if there are items in list get holder tag
    else {
        holder1 = (Holder) view.getTag(R.id.TAG_ONLINE_ID_1);
        holder2 = (Holder) view.getTag(R.id.TAG_ONLINE_ID_2);
    }

    //setting text to a new list items
    holder1.osoba.setText("[" + newPayment.getDate() + "]  " + newPayment.getOsoba());
    holder1.kwota.setText(newPayment.getWplata() + "zł");
    holder1.opis.setText(newPayment.getOpis());
    holder2.hourDate.setText("[" + newPayment.getHourDate() + "]");
    holder2.dayDate.setText(newPayment.getDayDate());
    holder2.osobaSelected.setText(newPayment.getOsoba());
    holder2.kwotaSelected.setText(newPayment.getWplata() + "zł");
    holder2.opisSelected.setText(newPayment.getOpis());

    //setting different color to Lukasz/Marcelina/Karolina text
    if (newPayment.getFlag() == 1) {
        holder1.osoba.setTextColor(Color.RED);
        holder2.osobaSelected.setTextColor(Color.RED);
    } else if (newPayment.getFlag() == 2) {
        holder1.osoba.setTextColor(Color.BLUE);
        holder2.osobaSelected.setTextColor(Color.BLUE);
    } else if (newPayment.getFlag() == 3) {
        holder1.osoba.setTextColor(Color.GREEN);
        holder2.osobaSelected.setTextColor(Color.GREEN);
    }

    return view;
}

}

0 个答案:

没有答案