Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ListView editable selection and deletion of entries

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要讲解了"ListView可编辑选择与删除条目怎么实现",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"ListView可编辑选择与删除条目怎么实现"吧!

首先,我们来看一下效果演示图:

接下来,我们讲解一下控件功能及其使用:1.功能

封装列表控件,使其条目具有可编辑选择、删除等功能。

2.Android Studio使用方法dependencies{ compile 'com.wkp:EditListView:1.0.2' //Android Studio3.0+可用以下方式 //implementation 'com.wkp:EditListView:1.0.2'}3.使用详解

属性讲解

布局示例

代码示例

/** * kotlin用法 */ @RequiresApi(Build.VERSION_CODES.KITKAT)class MainActivity : AppCompatActivity() { privateval data = arrayListOf("托儿索", "儿童劫", "小学僧", "橡皮妮", "喜之螂", "提款姬", "鱼尾雯", "鸡毛信", "娃娃鱼", "过家嘉", "尿不狮", "沙琪马", "阿童木", "大嘴猴", "香港皎") private var mListView: EditListView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) mListView = findViewById(R.id.lv) val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, data) //设置适配器 mListView!!.adapter = adapter //设置是否测量高度(解决ScrollView冲突)// mListView!!.setMeasureHeight(true) //长按监听 mListView!!.setOnItemLongClickListener({ parent, view, position, id -> //开启编辑状态 mListView!!.isEditState = true //设置长按条目选中状态 mListView!!.setItemChecked(position, true) //返回false会导致OnItemClickListener调用,使以上的选中状态消失 true }) //设置所有条目选中/未选中监听(每次条目状态改变都会回调) mListView!!.setOnAllItemCheckedListener { checked -> Log.d("MainActivity", "checked:" + checked) } } //删除按钮 fun delete(view: View) { //删除所有已选中条目(adapter的源数据为数组时不支持转换) mListView!!.deleteAllCheckedItem(data) }// 编辑按钮 fun edit(view: View) {// 开启编辑状态 mListView!!.isEditState = true }// 退出编辑按钮 fun exitEdit(view: View) {// 关闭编辑状态 mListView!!.isEditState = false }// 全选按钮 fun selectAll(view: View) {// 全选 mListView!!.setAllItemChecked() //是否全选 Log.d("MainActivity","isAllItemChecked:" + mListView!!.isAllItemChecked) }// 全不选按钮 fun selectNone(view: View) {// 全不选 mListView!!.setAllItemUnchecked() //是否全不选 Log.d("MainActivity","isAllItemUnchecked:" + mListView!!.isAllItemUnchecked) }}/** * Created by user on 2017/11/6. * java用法 */@RequiresApi(api = Build.VERSION_CODES.KITKAT)public class TestActivity extends AppCompatActivity { private String[] mStrings = {"托儿索", "儿童劫", "小学僧", "橡皮妮", "喜之螂", "提款姬", "鱼尾雯", "鸡毛信", "娃娃鱼", "过家嘉", "尿不狮", "沙琪马", "阿童木", "大嘴猴", "香港皎","脑残片","卖卖卖","333","干干干"}; private List data = new ArrayList(); private EditListView mListView; { data.addAll(Arrays.asList(mStrings)); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = findViewById(R.id.lv); //设置编辑/退出编辑动画时长 mListView.setAnimDuration(400); //设置适配器 mListView.setAdapter(new ArrayAdapter(this,R.layout.item_lv,R.id.item_tv,data)); //条目长按监听 mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { //开启编辑状态 mListView.setEditState(true); //设置长按条目选中状态 mListView.setItemChecked(position,true); //返回false会导致OnItemClickListener调用,使以上的选中状态消失 return true; } }); //设置所有条目选中/未选中监听(每次条目状态改变都会回调) mListView.setOnAllItemCheckedListener(new EditListView.OnAllItemCheckedListener() { @Override public void onAllItemChecked(boolean checked) { Log.d("TestActivity", "checked:" + checked); } }); } //删除按钮 public void delete(View view) { //删除所有已选中条目(adapter的源数据为数组时不支持转换) mListView.deleteAllCheckedItem(data); } //编辑按钮 public void edit(View view) { //开启编辑状态 mListView.setEditState(true); } //退出编辑按钮 public void exitEdit(View view) { //关闭编辑状态 mListView.setEditState(false); } //全选按钮 public void selectAll(View view) { //全选 mListView.setAllItemChecked(); //是否全选 Log.d("MainActivity", "isAllItemChecked:" + mListView.isAllItemChecked()); } //全不选按钮 public void selectNone(View view) { //全不选 mListView.setAllItemUnchecked(); //是否全不选 Log.d("MainActivity", "isAllItemUnchecked:" + mListView.isAllItemUnchecked()); }}感谢各位的阅读,以上就是"ListView可编辑选择与删除条目怎么实现"的内容了,经过本文的学习后,相信大家对ListView可编辑选择与删除条目怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report