




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中实现一个左滑删除列表功能
怎么在Android中实现一个左滑删除列表功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1、布局文件view_left_slide_remove.xml包含一个RelativeLayout和TextView,RelativeLayout是实际界面,TextView是删除按钮。<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp">
<RelativeLayout
android:id="@+id/view_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/tv_remove"
android:layout_width="@dimen/left_slide_remove_width"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="删除"
android:textSize="16sp"
android:textColor="@color/white"
android:background="#ffff4b30"
android:visibility="gone"/>
</RelativeLayout>2、自定义AdapterLeftSlideRemoveAdapter类实现了BaseAdapter。在getView方法中引用了view_left_slide_remove布局,提供getSubView来导入真正的布局。OnItemRemoveListener是删除监听器。public
static
abstract
class
LeftSlideRemoveAdapter
extends
BaseAdapter
{
protected
Context
mContext;
private
OnItemRemoveListener
mListener;
public
LeftSlideRemoveAdapter(Context
context)
{
this.mContext
=
context;
}
@Override
public
final
View
getView(final
int
position,
View
convertView,
ViewGroup
parent)
{
ViewHolder
holder;
if
(convertView
==
null)
{
LayoutInflater
inflater
=
LayoutInflater.from(mContext);
convertView
=
inflater.inflate(R.layout.view_left_slide_remove,
parent,
false);
holder
=
new
ViewHolder();
holder.viewContent
=
(RelativeLayout)
convertView.findViewById(R.id.view_content);
holder.tvRmove
=
(TextView)
convertView.findViewById(R.id.tv_remove);
convertView.setTag(holder);
//
viewChild是实际的界面
holder.viewChild
=
getSubView(position,
null,
parent);
holder.viewContent.addView(holder.viewChild);
}
else
{
holder
=
(ViewHolder)
convertView.getTag();
getSubView(position,
holder.viewChild,
parent);
}
holder.tvRmove.setOnClickListener(new
View.OnClickListener(){
@Override
public
void
onClick(View
v)
{
if
(mListener
!=
null)
{
mListener.onItemRemove(position);
notifyDataSetChanged();
}
}
});
return
convertView;
}
public
abstract
View
getSubView(int
position,
View
convertView,
ViewGroup
parent);
}
private
static
class
ViewHolder
{
RelativeLayout
viewContent;
View
viewChild;
View
tvRmove;
}
public
static
interface
OnItemRemoveListener
{
public
void
onItemRemove(int
position);
}3、自定义左滑删除列表在dispatchTouchEvent方法里面捕捉MotionEvent事件。在onTouchEvent方法里面滑动界面。VelocityTracker类记录手势。Scroller类进行滑动操作。public
class
LeftSlideRemoveListView
extends
ListView
{
private
final
static
int
SNAP_VELOCITY
=
600;
private
Scroller
mScroller;
private
VelocityTracker
mVelocityTracker;
private
int
mTouchSlop;
private
boolean
mIsSlide
=
false;
private
int
mDelta
=
0;
private
int
mDownX;
private
int
mDownY;
private
int
mMaxDistence;
private
int
mSlidePosition
=
INVALID_POSITION;
private
OnItemRemoveListener
adapterListener;
private
OnItemRemoveListener
mRemoveListener
=
new
OnItemRemoveListener()
{
@Override
public
void
onItemRemove(int
position)
{
if
(adapterListener
!=
null)
{
adapterListener.onItemRemove(position);
}
clear();
mSlidePosition
=
INVALID_POSITION;
}
};
private
LeftSlideRemoveAdapter
mRemoveAdapter;
private
View
mCurrentContentView,
mCurrentRemoveView;
public
LeftSlideRemoveListView(Context
context)
{
this(context,
null);
}
public
LeftSlideRemoveListView(Context
context,
AttributeSet
attrs)
{
super(context,
attrs);
mScroller
=
new
Scroller(context);
mTouchSlop
=
ViewConfiguration.get(getContext()).getScaledTouchSlop();
mMaxDistence
=
context.getResources().getDimensionPixelSize(
R.dimen.left_slide_remove_width);
}
@Override
public
boolean
dispatchTouchEvent(MotionEvent
ev)
{
switch
(ev.getAction())
{
case
MotionEvent.ACTION_DOWN:
addVelocityTracker(ev);
if
(!mScroller.isFinished())
{
return
super.dispatchTouchEvent(ev);
}
//
起始位置,当前position
mDownX
=
(int)
ev.getX();
mDownY
=
(int)
ev.getY();
int
position
=
pointToPosition(mDownX,
mDownY);
if
(position
==
mSlidePosition)
break;
mSlidePosition
=
position;
if
(mSlidePosition
==
INVALID_POSITION
)
{
return
super.dispatchTouchEvent(ev);
}
//
恢复状态
clear();
//
获取当前界面
View
childView
=
getChildAt(mSlidePosition
-
getFirstVisiblePosition());
mCurrentContentView
=
childView.findViewById(R.id.view_content);
mCurrentRemoveView
=
childView.findViewById(R.id.tv_remove);
break;
case
MotionEvent.ACTION_MOVE:
if
(mCurrentContentView
==
null)
break;
if
(Math.abs(getScrollVelocity())
>
SNAP_VELOCITY
||
(Math.abs(ev.getX()
-
mDownX)
>
mTouchSlop
&&
Math.abs(ev.getY()
-
mDownY)
<
mTouchSlop))
{
//
开始滑动
mIsSlide
=
true;
}
break;
case
MotionEvent.ACTION_UP:
if
(mCurrentContentView
==
null
&&
mIsSlide)
break;
//
如果左滑小于4/5,按钮不显示
if
(mDelta
<
mMaxDistence
*
4
/
5)
{
mCurrentRemoveView.setVisibility(View.GONE);
scrollRight();
}
else
if
(mDelta
<
mMaxDistence)
{
scrollLeft();
}
recycleVelocityTracker();
mIsSlide
=
false;
break;
}
return
super.dispatchTouchEvent(ev);
}
@Override
public
boolean
onTouchEvent(MotionEvent
ev)
{
if
(mIsSlide
&&
mSlidePosition
!=
INVALID_POSITION)
{
final
int
action
=
ev.getAction();
int
x
=
(int)
ev.getX();
switch
(action)
{
case
MotionEvent.ACTION_MOVE:
addVelocityTracker(ev);
int
deltaX
=
mDownX
-
x;
mDownX
=
x;
mDelta
+=
deltaX;
if
(mDelta
<
0)
{
mCurrentContentView.scrollTo(0,
0);
mDelta
=
0;
mCurrentRemoveView.setVisibility(View.GONE);
}
else
if
(mDelta
>=
mMaxDistence)
{
mDelta
=
mMaxDistence;
mCurrentContentView.scrollTo(mMaxDistence,
0);
mCurrentRemoveView.setVisibility(View.VISIBLE);
mCurrentRemoveView.setTranslationX(0);
}
else
{
mCurrentContentView.scrollBy(deltaX,
0);
mCurrentRemoveView.setVisibility(View.VISIBLE);
mCurrentRemoveView.setTranslationX(mMaxDistence
-
mDelta);
}
break;
}
return
true;
}
return
super.onTouchEvent(ev);
}
//
右滑
private
void
scrollRight()
{
final
int
delta
=
mDelta;
mScroller.startScroll(delta,
0,
-delta,
0,
Math.abs(delta));
mDelta
=
0;
postInvalidate();
}
//
左滑
private
void
scrollLeft()
{
final
int
delta
=
mMaxDistence
-
mDelta;
mScroller.startScroll(mDelta,
0,
delta,
0,
Math.abs(delta));
mDelta
=
mMaxDistence;
postInvalidate();
}
@Override
public
void
computeScroll()
{
if
(mSputeScrollOffset())
{
mCurrentContentView.scrollTo(mScroller.getCurrX(),
mScroller.getCurrY());
mCurrentRemoveView.setTranslationX(mMaxDistence
-
mScroller.getCurrX());
postInvalidate();
if
(mScroller.isFinished())
{
mCurrentContentView.scrollTo(mDelta,
0);
mCurrentRemoveView.setTranslationX(0);
}
}
}
private
void
addVelocityTracker(MotionEvent
event)
{
if
(mVelocityTracker
==
null)
{
mVelocityTracker
=
VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
private
int
getScrollVelocity()
{
mVelocityTputeCurrentVelocity(1000);
int
velocity
=
(int)
mVelocityTracker.getXVelocity();
return
velocity;
}
private
void
recycleVelocityTracker()
{
if
(mVelocityTracker
!=
null)
{
mVelocityTracker.recycle();
mVelocityTracker
=
null;
}
}
private
void
clear()
{
if
(mCurrentContentView
!=
null)
{
mDelta
=
0;
mCurrentContentView.scrollTo(0,
0);
mCurrentContentView
=
null;
mCurrentRemoveView.setVisibility(View.GONE);
mCurrentRemoveView
=
null;
}
}
@Override
public
void
setAdapter(ListAdapter
adapter)
{
if
(adapter
instanceof
LeftSlideRemoveAdapter)
{
super.setAdapter(adapter);
mRemoveAdapter
=
(LeftSlideRemoveAdapter)
adapter;
mRemoveAdapter.mListener
=
mRemoveListener;
}
else
{
throw
new
IllegalArgumentException("Must
be
LeftSlideRemoveAdapter");
}
}
public
void
setOnItemRemoveListener(OnItemRemoveListener
listener)
{
adapterListener
=
listener;
}
}4、测试例子ContractAdapter继承LeftSlideRemoveAdapter类。LeftSlideRemoveActivity中使用LeftSlideRemoveListView类。public
class
LeftSlideRemoveActivity
extends
Activity
{
private
List<Map<String,
String>>
mContentList
=
new
ArrayList<Map<String,
String>>();
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_slide_remove);
Map<String,
String>
item
=
new
HashMap<String,
String>();
item.put("name",
"Peter");
item.put("address",
"ShangHai");
mContentList.add(item);
item
=
new
HashMap<String,
String>();
item.put("name",
"Lily");
item.put("address",
"BeiJing");
mContentList.add(item);
item
=
new
HashMap<String,
String>();
item.put("name",
"Jack");
item.put("address",
"GuangZhou");
mContentList.add(item);
item
=
new
HashMap<String,
String>();
item.put("name",
"Mike");
item.put("address",
"ShengZhen");
mContentList.add(item);
LeftSlideRemoveListView
lv
=
(LeftSlideRemoveListView)
findViewById(R.id.id_listview);
lv.setAdapter(new
ContractAdapter(this));
lv.setOnItemRemoveListener(new
LeftSlideRemoveListView.OnItemRemoveListener()
{
@Override
public
void
onItemRemove(int
position)
{
mContentList.remove(position);
}
});
}
private
class
ContractAdapter
extends
LeftSlideRemoveListView.LeftSlideRemoveAdapter
{
public
ContractAdapter(Context
context)
{
super(context);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度沙发厂厂长聘用合同范本
- 2025版公路运输合同服务质量保障协议
- 2025版外汇市场交易执行顾问服务合同专业
- 2025年度房地产抵押权转让合同模板
- 2025照明灯具行业合作研发合同范本
- 2025版全新协议离婚财产放弃及共同子女财产租赁合同
- 2025年仓储服务与仓储设施租赁及仓储管理合同
- 2025民法典宣传周·旅游合同法律风险评估合同
- 2025年度新能源产业第三方担保服务合同
- 2025年大学生实习安全协议汇编及法律风险提示
- 能源费用托管服务方案投标文件(技术方案)
- 6.3.1 两条直线平行 课件-2025-2026学年高一数学高教版2023修订版基础模块 下册
- Unit 4 Plants around us单元试卷(含答案含听力原文)
- 五防系统培训
- 消除母婴传播培训
- 视神经炎的护理
- 2025年急性肺栓塞诊断和治疗指南解读课件
- 物联网技术应用专业教学标准(中等职业教育)2025修订
- T/CIE 169-2023企业级固态硬盘测试规范第5部分:稳定性测试
- 科研项目经费管理
- 焊接生产与管理
评论
0/150
提交评论