当前位置: 首页 > news >正文

android仿qq侧滑删除,Android仿QQ微信侧滑删除效果

仿QQ侧滑删除效果图

e3381ca07d24466fcbb8740e03af6430.png

1.自定义listview

public class DragDelListView extends ListView {

private boolean moveable=false;

private boolean closed=true;

private float mDownX,mDownY;

private int mTouchPosition,oldPosition=-1;

private DragDelItem mTouchView,oldView;

private Context context;

public DragDelListView(Context context) {

super(context);

init(context);

}

public DragDelListView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context);

}

public DragDelListView(Context context, AttributeSet attrs) {

super(context, attrs);

init(context);

}

private void init(Context context) {

this.context=context;

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN:

mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());

mTouchView=(DragDelItem)getChildAt(mTouchPosition - getFirstVisiblePosition());

mDownX = ev.getX();

mDownY=ev.getY();

if(oldPosition==mTouchPosition ||closed)

{

moveable=true;

mTouchView.mDownX =(int)mDownX;

}else

{

moveable=false;

if(oldView!=null)

{

oldView.smoothCloseMenu();

}

}

oldPosition=mTouchPosition;

oldView=mTouchView;

break;

case MotionEvent.ACTION_MOVE:

if (Math.abs(mDownX-ev.getX()) < Math.abs(mDownY-ev.getY()) * dp2px(2)) {

break;

}

if (moveable)

{

int dis = (int) (mTouchView.mDownX -ev.getX());

if(mTouchView.state==mTouchView.STATE_OPEN)

dis+=mTouchView.mMenuView.getWidth();

mTouchView.swipe(dis);

ev.setAction(MotionEvent.ACTION_CANCEL);

}

break;

case MotionEvent.ACTION_UP:

if (moveable)

{

if ((mTouchView.mDownX -ev.getX()) > (mTouchView.mMenuView.getWidth()/2)) {

// open

mTouchView.smoothOpenMenu();

closed=false;

} else {

// close

mTouchView.smoothCloseMenu();

closed=true;

}

ev.setAction(MotionEvent.ACTION_CANCEL);

}

break;

}

return super.onTouchEvent(ev);

}

private int dp2px(int dp) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,

getContext().getResources().getDisplayMetrics());

}

}

2.自定义滑动条目

public class DragDelItem extends LinearLayout {

public static final int STATE_CLOSE = 0;

public static final int STATE_OPEN = 1;

private View mContentView;

public View mMenuView;

public int mDownX;

public int state = STATE_CLOSE;

public boolean isFling;

private int mBaseX;

private Scroller scroll;

public DragDelItem(View contentView, View menuView) {

super(contentView.getContext());

scroll=new Scroller(getContext());

mContentView = contentView;

mMenuView = menuView;

init();

}

private DragDelItem(Context context, AttributeSet attrs) {

super(context, attrs);

}

private DragDelItem(Context context) {

super(context);

}

private void init() {

setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,

LayoutParams.WRAP_CONTENT));

LayoutParams contentParams = new LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

mContentView.setLayoutParams(contentParams);

mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT));

addView(mContentView);

addView(mMenuView);

}

public void swipe(int dis) {

if (dis > mMenuView.getWidth()) {

dis = mMenuView.getWidth();

}

if (dis < 0) {

dis = 0;

}

mContentView.layout(-dis, mContentView.getTop(),

mContentView.getWidth() - dis, getMeasuredHeight());

mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),

mContentView.getWidth() + mMenuView.getWidth() - dis,

mMenuView.getBottom());

}

@Override

public void computeScroll() {

if (state == STATE_OPEN) {

if (scroll.computeScrollOffset()) {

swipe(scroll.getCurrX());

postInvalidate();

}

} else {

if (scroll.computeScrollOffset()) {

swipe(mBaseX - scroll.getCurrX());

postInvalidate();

}

}

}

public void smoothCloseMenu() {

state = STATE_CLOSE;

mBaseX = -mContentView.getLeft();

scroll.startScroll(0, 0, mBaseX, 0, 350);

postInvalidate();

}

public void smoothOpenMenu() {

state = STATE_OPEN;

scroll.startScroll(-mContentView.getLeft(), 0,

mMenuView.getWidth()/2, 0, 350);

postInvalidate();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mMenuView.measure(MeasureSpec.makeMeasureSpec(0,

MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(

getMeasuredHeight(), MeasureSpec.EXACTLY));

mContentView.measure(MeasureSpec.makeMeasureSpec(0,

MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(

getMeasuredHeight(), MeasureSpec.EXACTLY));

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

mContentView.layout(0, 0, getMeasuredWidth(),

mContentView.getMeasuredHeight());

mMenuView.layout(getMeasuredWidth(), 0,

getMeasuredWidth() + mMenuView.getMeasuredWidth(),

mContentView.getMeasuredHeight());

}

}

3.所用到的布局文件

—swipecontent.xml代码

android:id="@+id/rl_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#999999"

android:padding="8dp" >

android:id="@+id/iv_icon"

android:layout_width="50dp"

android:layout_height="50dp"

android:src="@drawable/ic_launcher" />

android:id="@+id/tv_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="10dp"

android:layout_toRightOf="@+id/iv_icon"

android:text="name"

android:textColor="@android:color/black"

android:textSize="18sp" />

—swipemenu.xml代码

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:orientation="horizontal"

>

android:id="@+id/tv_open"

android:layout_width="90dp"

android:layout_height="match_parent"

android:gravity="center"

android:background="#C2C2C2"

android:text="置顶"

android:textColor="@android:color/white"

android:textSize="18sp" />

android:id="@+id/tv_del"

android:layout_width="90dp"

android:layout_height="match_parent"

android:gravity="center"

android:background="#FF0000"

android:text="删除"

android:textColor="@android:color/white"

android:textSize="18sp" />

4.主界面代码

public class MainActivity extends Activity {

private List mAppList;

private DragDelListView mListView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_list);

mAppList = getPackageManager().getInstalledApplications(0);

mListView = (DragDelListView) findViewById(R.id.listView);

mListView.setAdapter(new AppAdapter(mAppList));

}

class AppAdapter extends BaseAdapter {

private List mAppList;

public AppAdapter(List appList)

{

mAppList=appList;

}

@Override

public int getCount() {

return mAppList.size();

}

@Override

public ApplicationInfo getItem(int position) {

return mAppList.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder holder=null;

View menuView=null;

if (convertView == null) {

convertView = View.inflate(getApplicationContext(),

R.layout.swipecontent, null);

menuView = View.inflate(getApplicationContext(),

R.layout.swipemenu, null);

convertView = new DragDelItem(convertView,menuView);

holder=new ViewHolder(convertView);

} else {

holder = (ViewHolder) convertView.getTag();

}

ApplicationInfo item = getItem(position);

holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));

holder.tv_name.setText(item.loadLabel(getPackageManager()));

holder.tv_open.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Toast.makeText(MainActivity.this, "置顶:"+position, Toast.LENGTH_SHORT).show();

}

});

holder.tv_del.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Toast.makeText(MainActivity.this, "删除:"+position, Toast.LENGTH_SHORT).show();

}

});

return convertView;

}

class ViewHolder {

ImageView iv_icon;

TextView tv_name;

TextView tv_open,tv_del;

RelativeLayout relativeLayout;

public ViewHolder(View view) {

iv_icon = (ImageView) view.findViewById(R.id.iv_icon);

tv_name = (TextView) view.findViewById(R.id.tv_name);

tv_open=(TextView)view.findViewById(R.id.tv_open);

tv_del=(TextView)view.findViewById(R.id.tv_del);

relativeLayout = (RelativeLayout) view.findViewById(R.id.rl_layout);

//改变relativeLayout宽度

WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

int width = wm.getDefaultDisplay().getWidth();

relativeLayout.setMinimumWidth(width-60);

view.setTag(this);

}

}

}

}

主界面布局代码

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章:

  • html 重置样式表,CSS 浏览器样式重置终结版
  • 压缩html源文件,js代码压缩还原详解
  • html5随机选取文本框,HTML5交互式电子邮件(带输入框和选择框)
  • html格子切换效果图,(HTML)关于格子流布局方案可以如此设计
  • android密码设成星号,将EditText密码掩码字符更改为星号(*)
  • 江西赣州信丰2021高考成绩查询,2021上半年江西信丰县教资面试成绩查询入口
  • 计算机应用bsp什么意思,bsp是什么
  • 指利用计算机技术实现对文本篇章的理解,【人工智能课|人工智能自然语言处理技术是什么】- 环球网校...
  • 微型计算机结构认识,认识《微机原理》
  • 土木工程计算机仿真学科未来前景,土木工程学院土木工程计算机仿真2010级学历教育硕士--培养方案...
  • 本科会计大二转学美国学计算机,国内本科生可以转学去美国:最佳时机在大二!...
  • 分级列表html,CSS分级属性 二
  • html计算机之间的距离,【百思不得其解~求助】html网页编程:求输入的两个数之间的所有质...
  • 计算机游戏7步变28,亲子游戏100种
  • 我的未来规划作文计算机,我的未来规划500字作文
  • 5、React组件事件详解
  • 78. Subsets
  • Apache Spark Streaming 使用实例
  • create-react-app项目添加less配置
  • Fastjson的基本使用方法大全
  • go append函数以及写入
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • jdbc就是这么简单
  • JS 面试题总结
  • laravel with 查询列表限制条数
  • log4j2输出到kafka
  • overflow: hidden IE7无效
  • Spring Boot MyBatis配置多种数据库
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 第2章 网络文档
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 如何设计一个微型分布式架构?
  • 突破自己的技术思维
  • 想写好前端,先练好内功
  • 应用生命周期终极 DevOps 工具包
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​人工智能书单(数学基础篇)
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #FPGA(基础知识)
  • #QT项目实战(天气预报)
  • #每日一题合集#牛客JZ23-JZ33
  • #图像处理
  • (145)光线追踪距离场柔和阴影
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (第一天)包装对象、作用域、创建对象
  • (汇总)os模块以及shutil模块对文件的操作
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (循环依赖问题)学习spring的第九天
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .net 7 上传文件踩坑