如何在长按ListView项后为所有项添加复选框? Android Java

huangapple go评论79阅读模式
英文:

How to add checkbox on all the listView items after having a long item click? Android Java

问题

我想在我的项目中添加一个功能,即如果用户点击ListView中的某个项目,那么将会出现一个复选框,允许用户一次性删除一个或多个项目。类似于三星笔记(Samsung Notes),当您想要删除笔记、文件夹等内容时。然而,这个概念对我来说完全陌生,目前我不知道从何开始,也不知道应该查找什么主题/资源/示例代码。此外,我有一个自定义的Array Adapter类,我用它来使它与我的ListView配合工作,但我了解到您只需要一个Array Adapter类就可以使其工作,这让我很困惑,因为我不知道从何处开始进一步操作。任何帮助都将是非常棒的!

以下是我目前拥有的Array Adapter:

//想要创建自己的自定义ArrayAdapter。将扩展基类ArrayAdapter并保存我们的WordFolder对象
public class WordAdapter extends ArrayAdapter<WordFolder> {
    //构造函数 - 它接受上下文和单词列表
    WordAdapter(Context context, ArrayList<WordFolder> word){
        super(context, 0, word);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View listItemView = convertView;
        if(listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
        }

        //获取当前单词
        WordFolder currentWord = getItem(position);
        //将3个文本视图设置为与我们的word_folder.xml相匹配
        TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);

        TextView title = (TextView) listItemView.findViewById(R.id.title);

        TextView desc = (TextView) listItemView.findViewById(R.id.desc);

        //使用setText获取文本并将其设置到TextView中
        date_created.setText(currentWord.getDateCreated());

        title.setText(currentWord.getTitle());

        desc.setText(currentWord.getTitleDesc());

        return listItemView;

    }

}```


<details>
<summary>英文:</summary>

I want to add a feature on my project where if the user clicks one of the items on the listView then a checkbox would appear allowing the user to delete 1 or many items at once. Similar to Samsung Notes when you want to delete notes and or folders, etc. However, this concept is completely foreign to me and currently, I don&#39;t know where to begin to start this or what topic/resource/sample code I should look for. Also, I have a custom Array Adapter class that I used to order to work with my ListView but it came to my knowledge that you only need 1 array adapter class to make this work which made me confused since I don&#39;t know where to begin to manipulate it even further. Any help would be amazing!

Here is my Array Adapter that I have at the moment

//want to create our own custom ArrayAdapter. Going to extends the base class ArrayAdapter and hold our
//Word object
public class WordAdapter extends ArrayAdapter<WordFolder> {
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
return listItemView;
}

}```

答案1

得分: 0

在R.layout.folder_view中添加一个元素,并将其设置为不可见或隐藏。在长按事件(OnLongClick)时使它们可见。

英文:

In R.layout.folder_view add one and make it invisible or gone. OnLongClick make them Visible.

huangapple
  • 本文由 发表于 2020年8月28日 20:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定