在实际项目中删除列表中的某一项是非常常见的功能,传统的做法可以使用长按监听器等,而现在流行的做法是左滑弹出删除按钮,微信,QQ等都是这么做的,下面做一个示例,代码如下:
主页面MainActivity:代码比较简单常规
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package com.home.testslideview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import com.home.textslideview.R; public class MainActivity extends Activity { private ListView listView; private List<NewInfoBean> list = new ArrayList<NewInfoBean>(); // 适配器 private SlideAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); initView(); initData(); setAdapter(); } /** * 初始化页面控件 */ private void initView() { listView = (ListView) findViewById(R.id.main_lv); } /** * 初始化数据 */ private void initData() { list.add( new NewInfoBean( "这是测试内容1" )); list.add( new NewInfoBean( "这是测试内容2" )); list.add( new NewInfoBean( "这是测试内容3" )); list.add( new NewInfoBean( "这是测试内容4" )); list.add( new NewInfoBean( "这是测试内容5" )); list.add( new NewInfoBean( "这是测试内容6" )); list.add( new NewInfoBean( "这是测试内容7" )); list.add( new NewInfoBean( "这是测试内容8" )); list.add( new NewInfoBean( "这是测试内容9" )); list.add( new NewInfoBean( "这是测试内容10" )); } /** * 设置适配器 */ private void setAdapter() { if (adapter == null ) { adapter = new SlideAdapter( this , list); listView.setAdapter(adapter); } else { adapter.setList(list); adapter.notifyDataSetChanged(); } } } |
实体类NewInfoBean:具体项目中由自己定义:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package com.home.testslideview; public class NewInfoBean { public SlideView slideView; private String content; public SlideView getSlideView() { return slideView; } public void setSlideView(SlideView slideView) { this .slideView = slideView; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } public NewInfoBean() { super (); } public NewInfoBean(String content) { super (); this .content = content; } } |
适配器SlideAdapter:也比较简单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package com.home.testslideview; import java.util.List; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.home.testslideview.SlideView.OnSlideListener; import com.home.textslideview.R; public class SlideAdapter extends BaseAdapter implements OnSlideListener, OnClickListener { private LayoutInflater inflater; private List<NewInfoBean> list; private Context context; public SlideAdapter(Context context, List<NewInfoBean> list) { if (inflater == null ) { inflater = LayoutInflater.from(context); } this .list = list; this .context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem( int position) { return list.get(position); } @Override public long getItemId( int position) { return position; } @Override public View getView( int position, View convertView, ViewGroup arg2) { SlideView slideView = (SlideView) convertView; NewInfoBean bean = list.get(position); if (slideView == null ) { slideView = new SlideView(context); slideView.setOnSlideListener( this ); } // 设置内容 TextView contentText = getAdapterView(slideView, R.id.slideview_tv_content, position); contentText.setText(bean.getContent()); // 删除按钮 TextView delText = getAdapterView(slideView, R.id.slideview_tv_del, position); delText.setOnClickListener( this ); bean.slideView = slideView; bean.slideView.shrink(); return slideView; } @SuppressWarnings ( "unchecked" ) public <T extends View> T getAdapterView(View convertView, int id, Object tag) { SparseArray<View> viewHolder = null ; try { if (convertView.getTag(R.id.view_holder) instanceof SparseArray<?>) { viewHolder = (SparseArray<View>) convertView .getTag(R.id.view_holder); } } catch (ClassCastException e) { } if (viewHolder == null ) { viewHolder = new SparseArray<View>(); convertView.setTag(R.id.view_holder, viewHolder); convertView.setTag(R.id.order_id, tag); } View childView = viewHolder.get(id); if (childView == null ) { childView = convertView.findViewById(id); childView.setTag(tag); viewHolder.put(id, childView); } return (T) childView; } public List<NewInfoBean> getList() { return list; } public void setList(List<NewInfoBean> list) { this .list = list; } @Override public void onSlide(View view, int status) { } @Override public void onClick(View v) { switch (v.getId()) { case R.id.slideview_tv_del: int position = (Integer) v.getTag(); list.remove(position); notifyDataSetChanged(); break ; default : break ; } } } |
比较关键的两个类:
自定义的ListView:SlideListView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package com.home.testslideview; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ListView; public class SlideListView extends ListView { private SlideView itemView; public SlideListView(Context context) { super (context); } public SlideListView(Context context, AttributeSet attrs) { super (context, attrs); } public void shrinkListItem( int position) { View item = getChildAt(position); if (item != null ) { try { ((SlideView) item).shrink(); } catch (ClassCastException e) { e.printStackTrace(); } } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { int x = ( int ) event.getX(); int y = ( int ) event.getY(); int position = pointToPosition(x, y); if (position != INVALID_POSITION) { NewInfoBean data = (NewInfoBean) getItemAtPosition(position); itemView = data.slideView; } } default : break ; } if (itemView != null ) { itemView.onRequireTouchEvent(event); } return super .onTouchEvent(event); } } |
自定义的ListView中的每一行控件:SlideView(借鉴网上的一个示例):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package com.home.testslideview; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; import android.widget.Scroller; import com.home.textslideview.R; public class SlideView extends LinearLayout { private static final String TAG = SlideView. class .getSimpleName(); private Context mContext; private Scroller mScroller; private OnSlideListener mOnSlideListener; private int mHolderWidth = 80 ; private int mLastX = 0 ; private int mLastY = 0 ; private static final int TAN = 2 ; private LayoutInflater inflater; public interface OnSlideListener { public static final int SLIDE_STATUS_OFF = 0 ; public static final int SLIDE_STATUS_START_SCROLL = 1 ; public static final int SLIDE_STATUS_ON = 2 ; /** * @param view * current SlideView * @param status * SLIDE_STATUS_ON or SLIDE_STATUS_OFF */ public void onSlide(View view, int status); } public SlideView(Context context) { super (context); initView(); } public SlideView(Context context, AttributeSet attrs) { super (context, attrs); initView(); } private void initView() { mContext = getContext(); if (inflater == null ) { inflater = LayoutInflater.from(mContext); } mScroller = new Scroller(mContext); setOrientation(LinearLayout.HORIZONTAL); View.inflate(mContext, R.layout.slide_view_merge, this ); mHolderWidth = Math.round(TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources() .getDisplayMetrics())); } public void setOnSlideListener(OnSlideListener onSlideListener) { mOnSlideListener = onSlideListener; } public void shrink() { if (getScrollX() != 0 ) { this .smoothScrollTo( 0 , 0 ); } } public void onRequireTouchEvent(MotionEvent event) { int x = ( int ) event.getX(); int y = ( int ) event.getY(); int scrollX = getScrollX(); Log.d(TAG, "x=" + x + " y=" + y); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { if (!mScroller.isFinished()) { mScroller.abortAnimation(); } if (mOnSlideListener != null ) { mOnSlideListener.onSlide( this , OnSlideListener.SLIDE_STATUS_START_SCROLL); } break ; } case MotionEvent.ACTION_MOVE: { int deltaX = x - mLastX; int deltaY = y - mLastY; if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) { break ; } int newScrollX = scrollX - deltaX; if (deltaX != 0 ) { if (newScrollX < 0 ) { newScrollX = 0 ; } else if (newScrollX > mHolderWidth) { newScrollX = mHolderWidth; } this .scrollTo(newScrollX, 0 ); } break ; } case MotionEvent.ACTION_UP: { int newScrollX = 0 ; if (scrollX - mHolderWidth * 0.75 > 0 ) { newScrollX = mHolderWidth; } this .smoothScrollTo(newScrollX, 0 ); if (mOnSlideListener != null ) { mOnSlideListener.onSlide( this , newScrollX == 0 ? OnSlideListener.SLIDE_STATUS_OFF : OnSlideListener.SLIDE_STATUS_ON); } break ; } default : break ; } mLastX = x; mLastY = y; } private void smoothScrollTo( int destX, int destY) { // 缓慢滚动到指定位置 int scrollX = getScrollX(); int delta = destX - scrollX; mScroller.startScroll(scrollX, 0 , delta, 0 , Math.abs(delta) * 3 ); invalidate(); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } } |
main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < com.home.testslideview.SlideListView android:id = "@+id/main_lv" android:layout_width = "match_parent" android:layout_height = "match_parent" android:cacheColorHint = "#00000000" android:fadingEdge = "none" android:listSelector = "#00000000" android:scrollbars = "none" /> </ LinearLayout > |
slide_view_merge.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<? xml version = "1.0" encoding = "utf-8" ?> < merge xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" > < LinearLayout android:id = "@+id/slideview_layout_content" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_gravity = "center_vertical" android:gravity = "center_vertical" android:orientation = "horizontal" > < TextView android:id = "@+id/slideview_tv_content" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "内容部分" /> </ LinearLayout > < LinearLayout android:id = "@+id/view_layout_del" android:layout_width = "80dp" android:layout_height = "match_parent" android:layout_gravity = "center_vertical" android:layout_toRightOf = "@id/slideview_layout_content" android:clickable = "true" android:gravity = "center_vertical" android:orientation = "horizontal" > < TextView android:id = "@+id/slideview_tv_del" android:layout_width = "80dp" android:layout_height = "match_parent" android:layout_marginBottom = "2dp" android:gravity = "center" android:padding = "15dp" android:text = "删除" /> </ LinearLayout > </ merge > |
源码下载:高仿微信左滑删除效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010142437/article/details/46698369?utm_source=blogxgwz9