当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义可编辑、删除的侧滑LisitView

Android自定义可编辑、删除的侧滑LisitView

2019年07月24日  | 移动技术网IT编程  | 我要评论

锦绣良缘 文雨,qq飞车补丁下载,暗渡陈仓的故事

最近由于项目的需要,自定义了一个具有侧滑功能的listview,侧滑后可以点击编辑、删除。好了,大家先看一下效果图,毕竟是看脸的世界。 

好了,我要先讲一下思路,一次编辑很难完善,有什么问题我后来还会补上,欢迎各位大神拍砖:
1、首先先说一下item:item用的linearlayout布局,删除、编辑分别是写死了宽度的textview,左边是一个match_parent的relativelayout,内容自己随意搞 

2、上下滑动和左右滑动的处理:当用户手指滑动时,可以进行坐标的判断,如果y轴移动距离大,则listview上下滑动;否则当x轴移动距离达到或者超过一个textview的宽度时,动态控制item的marginleft为负的两倍textview的宽度,从而实现滑动效果 

3、编辑、删除的点击:编辑、删除在adapter中进行监听,然后自己在adapter中再定义一个监听接口,方便用户在使用listview的activity中进行监听 

下面附上item的布局文件:

<?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="wrap_content"
 android:orientation="horizontal" >

 <relativelayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#ffffff"
 android:orientation="horizontal">

 <relativelayout
  android:id="@+id/rltop"
  android:layout_width="match_parent"
  android:layout_height="65dp"
  android:padding="10dp">
  <imageview
  android:id="@+id/imglamp"
  android:layout_alignparentleft="true"
  android:layout_marginleft="10dp"
  android:src="@mipmap/ic_launcher"
  android:layout_width="40dp"
  android:layout_height="40dp"/>
  <textview
  android:id="@+id/tvname"
  android:layout_width="wrap_content"
  android:layout_height="22dp"
  android:layout_torightof="@+id/imglamp"
  android:layout_marginleft="10dp"
  android:gravity="center"
  android:textsize="17sp"
  android:textcolor="#000000" />

  <textview
  android:id="@+id/tvcontent"
  android:layout_width="wrap_content"
  android:layout_height="18dp"
  android:layout_torightof="@+id/imglamp"
  android:layout_below="@+id/tvname"
  android:layout_marginleft="10dp"
  android:gravity="center"
  android:textsize="14sp"
  android:textcolor="#000000"
  android:text="content"/>
 </relativelayout>

 <view
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:layout_marginleft="20dp"
  android:layout_marginright="20dp"
  android:layout_below="@+id/rltop"
  android:background="#eeeeee"/>

 </relativelayout>

 <!--edit-->
 <textview
 android:id="@+id/tvedit"
 android:layout_width="80dp"
 android:layout_height="match_parent"
 android:background="#dddddd"
 android:gravity="center"
 android:paddingleft="20dp"
 android:layout_marginbottom="1dp"
 android:textcolor="@android:color/white"
 android:paddingright="20dp"
 android:text="编辑" />

 <!--delete-->
 <textview
 android:id="@+id/delete"
 android:layout_width="80dp"
 android:layout_height="match_parent"
 android:background="#ffff0000"
 android:gravity="center"
 android:paddingleft="20dp"
 android:layout_marginbottom="1dp"
 android:textcolor="@android:color/white"
 android:paddingright="20dp"
 android:text="删除" />

</linearlayout>

adapter代码: 

package com.lei.slidelistview;

import android.content.context;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.textview;
import java.util.list;

/**
 * created by 磊 on 2016/7/7.
 */
public class listviewslideadapter extends baseadapter{

 private list<string> bulblist;
 private context context;
 private onclicklistenereditordelete onclicklistenereditordelete;

 public listviewslideadapter(context context, list<string> bulblist){
  this.bulblist=bulblist;
  this.context=context;
 }

 @override
 public int getcount() {
  return bulblist.size();
 }

 @override
 public object getitem(int position) {
  return bulblist.get(position);
 }

 @override
 public long getitemid(int position) {
  return position;
 }

 @override
 public view getview(final int position, view convertview, viewgroup parent) {
  final string bulb=bulblist.get(position);
  view view;
  viewholder viewholder;
  if(null == convertview) {
  view = view.inflate(context, r.layout.item_slide_delete_edit, null);
  viewholder=new viewholder();
  viewholder.tvname=(textview)view.findviewbyid(r.id.tvname);
  viewholder.img=(imageview)view.findviewbyid(r.id.imglamp);
  viewholder.tvdelete=(textview)view.findviewbyid(r.id.delete);
  viewholder.tvedit=(textview)view.findviewbyid(r.id.tvedit);
  view.settag(viewholder);//store up viewholder
  }else {
  view=convertview;
  viewholder=(viewholder)view.gettag();
  }

  viewholder.img.setimageresource(r.mipmap.ic_launcher);
  viewholder.tvname.settext(bulb);
  viewholder.tvdelete.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
   if (onclicklistenereditordelete!=null){
   onclicklistenereditordelete.onclicklistenerdelete(position);
   }
  }
  });
  viewholder.tvedit.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
   if (onclicklistenereditordelete!=null){
   onclicklistenereditordelete.onclicklisteneredit(position);
   }
  }
  });
  return view;
 }

 private class viewholder{
  textview tvname,tvedit,tvdelete;
  imageview img;
 }

 public interface onclicklistenereditordelete{
 void onclicklisteneredit(int position);
 void onclicklistenerdelete(int position);
 }

 public void setonclicklistenereditordelete(onclicklistenereditordelete onclicklistenereditordelete1){
 this.onclicklistenereditordelete=onclicklistenereditordelete1;
 }

 }

  

自定义listview文件代码:

package com.lei.slidelistview;

import android.content.context;
import android.util.attributeset;
import android.util.displaymetrics;
import android.view.motionevent;
import android.view.viewgroup;
import android.view.windowmanager;
import android.widget.linearlayout;
import android.widget.listview;

public class slidelistview extends listview {
 private int mscreenwidth; // 屏幕宽度
 private int mdownx; // 按下点的x值
 private int mdowny; // 按下点的y值
 private int mdeletebtnwidth;// 删除按钮的宽度
 
 private boolean isdeleteshown; // 删除按钮是否正在显示
 
 private viewgroup mpointchild; // 当前处理的item
 private linearlayout.layoutparams mlayoutparams; // 当前处理的item的layoutparams
 
 public slidelistview(context context, attributeset attrs) {
 this(context, attrs, 0);
 }

 public slidelistview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 
 // 获取屏幕宽度
 windowmanager wm = (windowmanager) context.getsystemservice(context.window_service);
 displaymetrics dm = new displaymetrics();
 wm.getdefaultdisplay().getmetrics(dm);
 mscreenwidth = dm.widthpixels;
 }
 
 @override
 public boolean ontouchevent(motionevent ev) {
 switch (ev.getaction()) {
 case motionevent.action_down:
 performactiondown(ev);
 break;
 case motionevent.action_move:
 return performactionmove(ev);
 case motionevent.action_up:
 performactionup();
 break;
 }
 
 return super.ontouchevent(ev);
 }

 // 处理action_down事件
 private void performactiondown(motionevent ev) {
 if(isdeleteshown) {
 turntonormal();
 }
 
 mdownx = (int) ev.getx();
 mdowny = (int) ev.gety();
 // 获取当前点的item
 mpointchild = (viewgroup) getchildat(pointtoposition(mdownx, mdowny)
 - getfirstvisibleposition());
 // 获取删除按钮的宽度
 mdeletebtnwidth = mpointchild.getchildat(1).getlayoutparams().width;
 mlayoutparams = (linearlayout.layoutparams) mpointchild.getchildat(0)
 .getlayoutparams();
 mlayoutparams.width = mscreenwidth;
 mpointchild.getchildat(0).setlayoutparams(mlayoutparams);
 }
 
 // 处理action_move事件
 private boolean performactionmove(motionevent ev) {
 int nowx = (int) ev.getx();
 int nowy = (int) ev.gety();
 if(math.abs(nowx - mdownx) > math.abs(nowy - mdowny)) {
 // 如果向左滑动
 if(nowx < mdownx) {
 // 计算要偏移的距离
 int scroll = (nowx - mdownx) / 2;
 // 如果大于了删除按钮的宽度, 则最大为删除按钮的宽度
 if(-scroll >= mdeletebtnwidth) {
 scroll = -mdeletebtnwidth;
 }
 // 重新设置leftmargin
 mlayoutparams.leftmargin = scroll*2;
 mpointchild.getchildat(0).setlayoutparams(mlayoutparams);
 }
 return true;
 }
 return super.ontouchevent(ev);
 }
 
 // 处理action_up事件
 private void performactionup() {
 // 偏移量大于button的一半,则显示button
 // 否则恢复默认
 if(-mlayoutparams.leftmargin >= mdeletebtnwidth / 2) {
 mlayoutparams.leftmargin = -mdeletebtnwidth*2;
 isdeleteshown = true;
 }else {
 turntonormal();
 }
 
 mpointchild.getchildat(0).setlayoutparams(mlayoutparams);
 }
 /**
 * 变为正常状态
 */
 public void turntonormal() {
 mlayoutparams.leftmargin = 0;
 mpointchild.getchildat(0).setlayoutparams(mlayoutparams);
 isdeleteshown = false;
 }
 /**
 * 当前是否可点击
 * @return 是否可点击
 */
 public boolean canclick() {
 return !isdeleteshown;
 }


}

下面附上使用布局代码+使用后台代码(2016年7月7日亲测可用)
布局: 

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.lei.slidelistview.mainactivity">

 <textview
 android:id="@+id/tvtitle"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:textsize="20sp"
 android:text="title" />

 <com.lei.slidelistview.slidelistview
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/tvtitle">

 </com.lei.slidelistview.slidelistview>
</relativelayout>

使用后台代码,这里要注意以下adapter设置监听的位置,放错了会报错哦 

package com.lei.slidelistview;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.widget.toast;
import java.util.arraylist;
import java.util.list;

public class mainactivity extends appcompatactivity {
 private slidelistview listview;
 private list<string> list=new arraylist<string>();
 private listviewslideadapter listviewslideadapter;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 getdata();
 initview();
 }

 private void initview(){
 listview=(slidelistview)findviewbyid(r.id.list);
 listviewslideadapter=new listviewslideadapter(this,list);
 listview.setadapter(listviewslideadapter);
 listviewslideadapter.setonclicklistenereditordelete(new listviewslideadapter.onclicklistenereditordelete() {
  @override
  public void onclicklisteneredit(int position) {
  toast.maketext(mainactivity.this, "edit position: " + position, toast.length_short).show();
  }

  @override
  public void onclicklistenerdelete(int position) {
  toast.maketext(mainactivity.this, "delete position: " + position, toast.length_short).show();
  }
 });
 }

 private void getdata(){
 for (int i=0;i<20;i++){
  list.add(new string("第"+i+"个item"));
 }
 }
}

更多学习内容,可以点击《android侧滑效果汇总》学习。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网