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

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

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:

  1. //想要创建自己的自定义ArrayAdapter。将扩展基类ArrayAdapter并保存我们的WordFolder对象
  2. public class WordAdapter extends ArrayAdapter<WordFolder> {
  3. //构造函数 - 它接受上下文和单词列表
  4. WordAdapter(Context context, ArrayList<WordFolder> word){
  5. super(context, 0, word);
  6. }
  7. @Override
  8. public View getView(int position, View convertView, ViewGroup parent){
  9. View listItemView = convertView;
  10. if(listItemView == null){
  11. listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
  12. }
  13. //获取当前单词
  14. WordFolder currentWord = getItem(position);
  15. //将3个文本视图设置为与我们的word_folder.xml相匹配
  16. TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
  17. TextView title = (TextView) listItemView.findViewById(R.id.title);
  18. TextView desc = (TextView) listItemView.findViewById(R.id.desc);
  19. //使用setText获取文本并将其设置到TextView中
  20. date_created.setText(currentWord.getDateCreated());
  21. title.setText(currentWord.getTitle());
  22. desc.setText(currentWord.getTitleDesc());
  23. return listItemView;
  24. }
  25. }```
  26. <details>
  27. <summary>英文:</summary>
  28. 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!
  29. 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);
}

  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent){
  3. View listItemView = convertView;
  4. if(listItemView == null){
  5. listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
  6. }
  7. //Getting the current word
  8. WordFolder currentWord = getItem(position);
  9. //making the 3 text view to match our word_folder.xml
  10. TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
  11. TextView title = (TextView) listItemView.findViewById(R.id.title);
  12. TextView desc = (TextView) listItemView.findViewById(R.id.desc);
  13. //using the setText to get the text and set it in the textView
  14. date_created.setText(currentWord.getDateCreated());
  15. title.setText(currentWord.getTitle());
  16. desc.setText(currentWord.getTitleDesc());
  17. return listItemView;
  18. }

}```

答案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:

确定