欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿QQ首页ListView左滑置顶、删除功能

程序员文章站 2022-07-01 16:17:59
 android 仿qq首页listview左滑置顶、删除等实现源码,具体内容如下 效果图 实现源码:package com.duguang.basean...

 android 仿qq首页listview左滑置顶、删除等实现源码,具体内容如下

效果图

Android仿QQ首页ListView左滑置顶、删除功能

实现源码:package com.duguang.baseanimation.ui.listivew.deletelistview;  
 

import android.content.context; 
import android.util.attributeset; 
import android.util.log; 
import android.util.typedvalue; 
import android.view.motionevent; 
import android.view.view; 
import android.widget.linearlayout; 
import android.widget.relativelayout; 
import android.widget.scroller; 
import android.widget.textview; 
 
import com.kayak.deletelistview.r; 
 
public class slideview extends linearlayout { 
 
 private static final string tag = "slideview"; 
 
 private context mcontext; 
 private linearlayout mviewcontent; 
 private relativelayout mholder; 
 private scroller mscroller; 
 private onslidelistener monslidelistener; 
 
 private int mholderwidth = 120; 
 
 private int mlastx = 0; 
 private int mlasty = 0; 
 private static final int tan = 2; 
 
 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(); 
  mscroller = new scroller(mcontext); 
 
  setorientation(linearlayout.horizontal); 
  view.inflate(mcontext, r.layout.activity_listview_delete_slide_view_merge, this); 
  mviewcontent = (linearlayout) findviewbyid(r.id.view_content); 
  mholderwidth = math.round(typedvalue.applydimension( 
    typedvalue.complex_unit_dip, mholderwidth, getresources() 
      .getdisplaymetrics())); 
 } 
 
 public void setbuttontext(charsequence text) { 
  ((textview)findviewbyid(r.id.delete)).settext(text); 
 } 
 
 public void setcontentview(view view) { 
  mviewcontent.addview(view); 
 } 
 
 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(); 
  } 
 } 
 
} 

package com.duguang.baseanimation.ui.listivew.deletelistview; 
 
import android.content.context; 
import android.util.attributeset; 
import android.util.log; 
import android.view.motionevent; 
import android.view.view; 
import android.widget.listview; 
 
import com.duguang.baseanimation.ui.listivew.deletelistview.deletelistviewmainactivity.messageitem; 
 
public class listviewcompat extends listview { 
 
 private static final string tag = "listviewcompat"; 
 
 private slideview mfocuseditemview; 
 
 public listviewcompat(context context) { 
  super(context); 
 } 
 
 public listviewcompat(context context, attributeset attrs) { 
  super(context, attrs); 
 } 
 
 public listviewcompat(context context, attributeset attrs, int defstyle) { 
  super(context, attrs, defstyle); 
 } 
 
 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); 
   log.e(tag, "postion=" + position); 
   if (position != invalid_position) { 
    messageitem data = (messageitem) getitematposition(position); 
    mfocuseditemview = data.slideview; 
    log.e(tag, "focuseditemview=" + mfocuseditemview); 
   } 
  } 
  default: 
   break; 
  } 
 
  if (mfocuseditemview != null) { 
   mfocuseditemview.onrequiretouchevent(event); 
  } 
 
  return super.ontouchevent(event); 
 } 
 
} 

package com.duguang.baseanimation.ui.listivew.deletelistview; 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.viewgroup; 
import android.widget.adapterview; 
import android.widget.adapterview.onitemclicklistener; 
import android.widget.baseadapter; 
import android.widget.imageview; 
import android.widget.textview; 
import android.widget.toast; 
 
import com.duguang.baseanimation.ui.base.baseactivity; 
import com.duguang.baseanimation.ui.listivew.deletelistview.slideview.onslidelistener; 
import com.kayak.deletelistview.r; 
 
public class deletelistviewmainactivity extends baseactivity implements onitemclicklistener, onclicklistener, 
  onslidelistener { 
 
 private static final string tag = "mainactivity"; 
 
 private listviewcompat mlistview; 
 
 private list<messageitem> mmessageitems = new arraylist<deletelistviewmainactivity.messageitem>(); 
 
 private slideview mlastslideviewwithstatuson; 
  
 private slideadapter adapter; 
  
 @override 
 public void setview() { 
   setcontentview(r.layout.activity_listview_delete_main); 
   
 } 
 
 @override 
 public void initview() { 
   mlistview = (listviewcompat) findviewbyid(r.id.list); 
 
   for (int i = 0; i < 20; i++) { 
    messageitem item = new messageitem(); 
    if (i % 3 == 0) { 
     item.iconres = r.drawable.delete_default_qq_avatar; 
     item.title = "腾讯新闻"; 
     item.msg = "青岛爆炸满月:大量鱼虾死亡"; 
     item.time = "晚上18:18"; 
    } else { 
     item.iconres = r.drawable.delete_wechat_icon; 
     item.title = "微信团队"; 
     item.msg = "欢迎你使用微信"; 
     item.time = "12月18日"; 
    } 
    mmessageitems.add(item); 
   } 
    
   adapter = new slideadapter(); 
   mlistview.setadapter(adapter); 
   mlistview.setonitemclicklistener(this); 
   
 } 
 
 @override 
 public void setlistener() { 
  // todo auto-generated method stub 
   
 } 
 
 private class slideadapter extends baseadapter { 
 
  private layoutinflater minflater; 
  slideadapter() { 
   super(); 
   minflater = getlayoutinflater(); 
  } 
 
  @override 
  public int getcount() { 
   return mmessageitems.size(); 
  } 
 
  @override 
  public object getitem(int position) { 
   return mmessageitems.get(position); 
  } 
 
  @override 
  public long getitemid(int position) { 
   return position; 
  } 
 
  @override 
  public view getview(final int position, view convertview, viewgroup parent) { 
   viewholder holder; 
   slideview slideview = (slideview) convertview; 
   if (slideview == null) { 
    view itemview = minflater.inflate(r.layout.item_listview_delete, null); 
 
    slideview = new slideview(deletelistviewmainactivity.this); 
    slideview.setcontentview(itemview); 
 
    holder = new viewholder(slideview); 
    slideview.setonslidelistener(deletelistviewmainactivity.this); 
    slideview.settag(holder); 
   } else { 
    holder = (viewholder) slideview.gettag(); 
   } 
   messageitem item = mmessageitems.get(position); 
   item.slideview = slideview; 
   item.slideview.shrink(); 
 
   holder.icon.setimageresource(item.iconres); 
   holder.title.settext(item.title); 
   holder.msg.settext(item.msg); 
   holder.time.settext(item.time); 
   holder.deleteholder.setonclicklistener(new onclicklistener() { 
     
    @override 
    public void onclick(view v) { 
     mmessageitems.remove(position); 
     adapter.notifydatasetchanged(); 
     toast.maketext(deletelistviewmainactivity.this, "删除第" + position+"个条目", 0).show(); 
    } 
   }); 
    
   return slideview; 
  } 
 
 } 
 
 public class messageitem { 
  public int iconres; 
  public string title; 
  public string msg; 
  public string time; 
  public slideview slideview; 
 } 
 
 private static class viewholder { 
  public imageview icon; 
  public textview title; 
  public textview msg; 
  public textview time; 
  public viewgroup deleteholder; 
 
  viewholder(view view) { 
   icon = (imageview) view.findviewbyid(r.id.icon); 
   title = (textview) view.findviewbyid(r.id.title); 
   msg = (textview) view.findviewbyid(r.id.msg); 
   time = (textview) view.findviewbyid(r.id.time); 
   deleteholder = (viewgroup)view.findviewbyid(r.id.holder); 
  } 
 } 
 
 @override 
 public void onitemclick(adapterview<?> parent, view view, int position, 
   long id) { 
  toast.maketext(this, "onitemclick position=" + position, 0).show(); 
   
 } 
 
 @override 
 public void onslide(view view, int status) { 
  if (mlastslideviewwithstatuson != null && mlastslideviewwithstatuson != view) { 
   mlastslideviewwithstatuson.shrink(); 
  } 
 
  if (status == slide_status_on) { 
   mlastslideviewwithstatuson = (slideview) view; 
  } 
 } 
 
 @override 
 public void onclick(view v) { 
  switch (v.getid()) { 
 case r.id.holder: 
   
  break; 
 
 default: 
  break; 
 } 
 } 
 
} 

源码下载地址:android仿qq左滑置顶删除功能

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