简体   繁体   English

单击Android按钮时,在ListView的行内显示删除按钮

[英]Show delete button inside row of a ListView when clicking a button Android

I need to implement a kind of Slide to Delete inside my ListView but what I want is not to slide the row but when the user clicks a button inside the row the delete button should show up. 我需要在ListView内实现一种“要删除的幻灯片”,但是我想要的不是滑动行,而是当用户单击行内的按钮时,应该显示删除按钮。 Is it possible? 可能吗? I've already implemented the Button and I know when the user press it but I pretty much have no idea on how to implement the delete button slide in effect from the right of the row. 我已经实现了Button并且我知道用户何时按下按钮,但是我几乎不知道如何实现该行右侧的Delete Button幻灯片。 Can someone give me an Idea on how to start? 有人可以给我一个入门的想法吗?

If you don't want Swipe-to-delete, then the implementation is pretty simple. 如果您不希望“刷卡删除”,那么实现非常简单。

Basically you are gonna show/hide that button on button click. 基本上,您将在单击按钮时显示/隐藏该按钮。

Since Android ListView reuses the Views, if you show/hide the button of a View of ListItem in onItemClick(), then when the list is scrolled the state will be lost and will result in incorrect behavior. 由于Android ListView重用了Views,因此,如果在onItemClick()中显示/隐藏ListItem的View的按钮,则滚动列表时,状态将丢失并且将导致错误的行为。

So we need to maintain this selection state in the Adapter's modal class. 因此,我们需要在适配器的模式类中保持此选择状态。 Just add a field like selected in the modal class. 只需添加一个模态类中选定的字段即可。 For instance, if you are showing list of Contact objects, then your modal class will be all like, 例如,如果您要显示Contact对象的列表,则您的模态类将完全一样,

class Contact {

  private String name;

  private String number;

  private boolean selected;

  ..........

} 

from what I've understand; 据我了解; you need to implement lets say an edit button, then when user clicks on it, it will disappear and another button called delete will popup instead of it on the same position. 您需要实现一个编辑按钮,然后当用户单击它时,该按钮将消失,并且将弹出另一个名为Delete的按钮,而不是在同一位置。

here is a possible approach to achieve that: 这是实现该目标的一种可能方法:

in your list_view_adapter.xml : 在您的list_view_adapter.xml

1-create a frame layout contain both of the buttons on top of each other. 1-创建一个框架布局,其中两个按钮彼此重叠。

2-default state of delete button is GONE -> android:visibility="gone" 删除按钮的2个默认状态为GONE-> android:visibility =“ gone”

3-when edit is pressed delete button will be visible and you will programmatically set Edit visibility to GONE 3-按下编辑键时,将显示删除按钮,并且您将以编程方式将“ Edit可见性”设置为“ GONE

4-set your delete button: 4-设置您的删除按钮:

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

    View v = convertView;

    // Some other things...

    Button delete = (Button) v.findViewById(R.id.delete);

    delete.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

       // After you delete the object from Parse database here,
       notifyDataSetChanged();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM