今天,我是第一次在掘金寫作,寫一篇技術文章,是我自己在Android開發中總結提煉出的一個自定義控件EditListView,下面主要對其功能及其使用做簡單介紹。
首先,我們來看一下效果演示圖:
接下來,我們講解一下控件功能及其使用:
1.功能
封裝列表控件,使其條目具有可編輯選擇、刪除等功能。
2.Android Studio使用方法
dependencies{compile
'com.wkp:EditListView:1.0.2'//Android Studio3.0+可用以下方式//implementation
'com.wkp:EditListView:1.0.2'
}
復制代碼3.使用詳解
<!--是否開啟測量高度--><attr name=
"wkp_measureHeight" format=
"boolean"/><!--未選中狀態時圖標--><attr name=
"wkp_uncheckedImg" format=
"reference"/><!--選中狀態時圖標--><attr name=
"wkp_checkedImg" format=
"reference"/><!--編輯/退出編輯動畫時長(默認200)--><attr name=
"wkp_editAnimDuration" format=
"integer"/>
復制代碼<!--activity_main.xml-->
<?xml version=
"1.0" encoding=
"utf-8"?>
<ScrollView xmlns:android=
"http://schemas.android.com/apk/res/android"xmlns:app=
"http://schemas.android.com/apk/res-auto"xmlns:tools=
"http://schemas.android.com/tools"android:id=
"@+id/sv"android:layout_width=
"match_parent"android:layout_height=
"wrap_content"><LinearLayoutandroid:layout_width=
"match_parent"android:layout_height=
"match_parent"android:orientation=
"vertical"><LinearLayoutandroid:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:orientation=
"horizontal"><TextViewandroid:id=
"@+id/delete"android:onClick=
"delete"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center"android:padding=
"@dimen/len_5dp"android:text=
"刪除"/><TextViewandroid:id=
"@+id/edit"android:onClick=
"edit"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center"android:padding=
"@dimen/len_5dp"android:text=
"編輯"/><TextViewandroid:id=
"@+id/exit_edit"android:onClick=
"exitEdit"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center"android:padding=
"@dimen/len_5dp"android:text=
"退出編輯"/><TextViewandroid:id=
"@+id/select_all"android:onClick=
"selectAll"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center"android:padding=
"@dimen/len_5dp"android:text=
"全選"/><TextViewandroid:id=
"@+id/select_none"android:onClick=
"selectNone"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center"android:padding=
"@dimen/len_5dp"android:text=
"全不選"/></LinearLayout><com.wkp.editlistview_library.view.EditListViewandroid:id=
"@+id/lv"app:wkp_checkedImg=
"@drawable/ic_checked"app:wkp_uncheckedImg=
"@drawable/ic_uncheck"app:wkp_measureHeight=
"true"android:layout_width=
"match_parent"android:layout_height=
"wrap_content"></com.wkp.editlistview_library.view.EditListView></LinearLayout>
</ScrollView><!--item_lv.xml-->
<?xml version=
"1.0" encoding=
"utf-8"?>
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"android:layout_width=
"match_parent"android:layout_height=
"match_parent"><TextViewandroid:id=
"@+id/item_tv"android:padding=
"10dp"android:layout_width=
"wrap_content"android:layout_height=
"wrap_content"/><TextViewandroid:padding=
"10dp"android:layout_alignParentRight=
"true"android:text=
"新"android:textColor=
"@android:color/white"android:background=
"@android:color/holo_red_light"android:layout_width=
"wrap_content"android:layout_height=
"wrap_content"/></RelativeLayout>
復制代碼/*** kotlin用法*/@RequiresApi(Build.VERSION_CODES.KITKAT)
class MainActivity :
AppCompatActivity() {private val data = arrayListOf<String>(
"托兒索",
"兒童劫",
"小學僧",
"橡皮妮",
"喜之螂",
"提款姬",
"魚尾雯",
"雞毛信",
"娃娃魚",
"過家嘉",
"尿不獅",
"沙琪馬",
"阿童木",
"大嘴猴",
"香港皎")private var mListView: EditListView? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)mListView = findViewById<EditListView>(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<String> data = new ArrayList<>();private EditListView mListView;{data.addAll(Arrays.asList(mStrings));}@Overrideprotected 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<String>(this,R.layout.item_lv,R.id.item_tv,data));//條目長按監聽mListView.setOnItemLongClickListener(new AdapterView.
OnItemLongClickListener() {@Overridepublic 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() {@Overridepublic 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());}
}
復制代碼結語
控件支持直接代碼創建,還有更多API請觀看EditListView.java內的注釋說明。
歡迎大家使用Github地址,感覺好用請給個Star鼓勵一下,謝謝!
大家如果有更好的意見或建議以及好的靈感,請郵箱作者,謝謝!
QQ郵箱: 1535514884@qq.com
163郵箱: 15889686524@163.com
Gmail郵箱: wkp15889686524@gmail.com
轉載于:https://juejin.im/post/5a4ed1e8f265da3e5b329859
總結
以上是生活随笔為你收集整理的可编辑选择、删除条目的ListView的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。