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

Android开发实现可拖动排序的ListView功能【附源码下载】

程序员文章站 2023-08-22 19:08:19
本文实例讲述了android开发实现可拖动排序的listview功能。分享给大家供大家参考,具体如下: 一、上图 二、简述 1、需要实现的效果是长按右侧可拖动部分...

本文实例讲述了android开发实现可拖动排序的listview功能。分享给大家供大家参考,具体如下:

一、上图

Android开发实现可拖动排序的ListView功能【附源码下载】

二、简述

1、需要实现的效果是长按右侧可拖动部分布局实现列表项的拖动排序
2、当点击列表项前面的单选按钮时,在该条目右侧显示删除图标,点击该图标删除当前条目。

三、实现思路

借助github上的开源代码drag-sort-listview-master加以改造。

四、主要源码展示

1、activity代码

package com.gengducun.dslvdemo;
import java.util.arraylist;
import android.app.activity;
import android.os.bundle;
import android.widget.listview;
import com.mobeta.android.dslv.dragsortlistview;
public class mainactivity extends activity {
 protected static final string tag = "mainactivity";
 /**产品信息列表**/
 private arraylist<productinfobean> productinfolist;
 /**产品列表控件**/
 private dragsortlistview mdslvproductlist;
 /**产品信息列表*/
 private productlistadapter mproductlistadapter;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  mdslvproductlist = (dragsortlistview) findviewbyid(r.id.dslv_product_list);
  initproductlist();
  mproductlistadapter = new productlistadapter(productinfolist, this);
  mdslvproductlist.setadapter(mproductlistadapter);
  mdslvproductlist.setdroplistener(ondrop);
  mdslvproductlist.setchoicemode(listview.choice_mode_single);
 }
 private dragsortlistview.droplistener ondrop = new dragsortlistview.droplistener() {
  @override
  public void drop(int from, int to) {
   if (from != to) {
    productinfobean item = (productinfobean) mproductlistadapter.getitem(from);
    mproductlistadapter.remove(item);
    mproductlistadapter.insert(item,from, to);
    mdslvproductlist.movecheckstate(from, to);
   }
  }
 };
 private void initproductlist() {
  productinfolist = new arraylist<productinfobean>();
  productinfobean productinfo1 = new productinfobean("eurusd1");
  productinfobean productinfo2 = new productinfobean("eurusd2");
  productinfobean productinfo3 = new productinfobean("eurusd3");
  productinfobean productinfo4 = new productinfobean("eurusd4");
  productinfobean productinfo5 = new productinfobean("eurusd5");
  productinfobean productinfo6 = new productinfobean("eurusd6");
  productinfobean productinfo7 = new productinfobean("eurusd7");
  productinfolist.add(productinfo1);
  productinfolist.add(productinfo2);
  productinfolist.add(productinfo3);
  productinfolist.add(productinfo4);
  productinfolist.add(productinfo5);
  productinfolist.add(productinfo6);
  productinfolist.add(productinfo7);
 }
}

2、adapter代码

package com.gengducun.dslvdemo;
import java.util.arraylist;
import android.content.context;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.radiobutton;
import android.widget.textview;
public class productlistadapter extends baseadapter {
 private static final string tag = "productlistadapter1";
 /**产品信息列表*/
 private arraylist<productinfobean> productinfolist;
 /**上下文*/
 private context mcontext;
 /**布局加载器*/
 private layoutinflater minflater;
 /**布局缓存*/
 private viewholder mholder;
 /**本类适配器对象**/
 private productlistadapter mproductlistadapter1;
 /**选中项位置**/
 private int mselectposition = -1;
 public productlistadapter(arraylist<productinfobean> productinfolist, context context) {
  super();
  this.productinfolist = productinfolist;
  this.mcontext = context;
  this.minflater = layoutinflater.from(context);
  this.mproductlistadapter1 = this;
 }
 @override
 public int getcount() {
  return null == productinfolist ? 0 : productinfolist.size();
 }
 @override
 public object getitem(int position) {
  return productinfolist.get(position);
 }
 @override
 public long getitemid(int position) {
  return position;
 }
 public int getmselectposition() {
  return mselectposition;
 }
 public void setmselectposition(int mselectposition) {
  this.mselectposition = mselectposition;
 }
 /**
  * 删除产品
  * @param productinfobean
  */
 public void remove(productinfobean productinfobean) {
  synchronized (this) {
   productinfolist.remove(productinfobean);
  }
  notifydatasetchanged();
 }
 /**
  * 将产品移动到指定的位置
  * @param productinfobean
  * @param from 移动之前的位置
  * @param to 移动完成之后,该产品所在的位置
  */
 public void insert(productinfobean productinfobean, int from, int to) {
  synchronized (this) {
   log.i(tag, "from=" + from + "  to =" + to);
   if (mselectposition != -1) {//有选中项的前提下
    if ((from < mselectposition && to < mselectposition) || (from > mselectposition && to > mselectposition)) {
     //在选中项之前的位置进行拖拉或者在选中项之后的区域拖拉,不会影响选中项
    } else if (from < mselectposition && to > mselectposition) {
     //将选中项之前的移动到选中项之后的位置,则选中项的索引需要发生变化,应该是选中项的位置-1
     mselectposition = mselectposition - 1;
    } else if (from > mselectposition && to < mselectposition) {
     //将选中项之后的项移动到选中项之前的位置,索引也需要变化,应该是选中项的位置+1
     mselectposition = mselectposition + 1;
    } else if ((from == mselectposition && from > to) || (from == mselectposition && from < to)) {
     //如果将选中项往选中项前面的方向拖,则选中项的索引也需要变化,应该是拖动到的位置
     mselectposition = to;
    } else if ((from < mselectposition && to == mselectposition)) {
     //将选中项之前的移动到当前选中项的位置,索引需要变化,选中项的索引应该是 选中项的值-1
     mselectposition = mselectposition - 1;
    } else if (from > mselectposition && to == mselectposition) {
     //将选中项之后的移动到当前项的位置,索引需要变化,选中项的索引应该是选中项的值+1
     mselectposition = mselectposition + 1;
    }
   }
   productinfolist.add(to, productinfobean);
  }
  notifydatasetchanged();
 }
 @override
 public view getview(int position, view convertview, viewgroup parent) {
  if (null == convertview) {
   convertview = minflater.inflate(r.layout.list_item_product_list_1, null);
   mholder = new viewholder();
   mholder.rbselect = (radiobutton) convertview.findviewbyid(r.id.rb_select);
   mholder.tvpurductname = (textview) convertview.findviewbyid(r.id.tv_product_name);
   mholder.lldraglayout = (linearlayout) convertview.findviewbyid(r.id.drag_handle);
   mholder.ivdrag = (imageview) convertview.findviewbyid(r.id.iv_drag);
   mholder.ivdeleteproduct = (imageview) convertview.findviewbyid(r.id.iv_product_delete);
   convertview.settag(mholder);
  } else {
   mholder = (viewholder) convertview.gettag();
  }
  productinfobean productinfobean = productinfolist.get(position);
  if (null != productinfobean) {
   //设置单选按钮的选中状态
   if (mselectposition == position) {
    mholder.rbselect.setchecked(true);
    mholder.ivdrag.setvisibility(view.gone);
    mholder.ivdeleteproduct.setvisibility(view.visible);
    mholder.ivdeleteproduct.setonclicklistener(new removeitemclicklistener(mselectposition));
   } else {
    mholder.rbselect.setchecked(false);
    mholder.ivdrag.setvisibility(view.visible);
    mholder.ivdeleteproduct.setvisibility(view.gone);
   }
   mholder.rbselect.setonclicklistener(new productclicklistener(position));
   //设置产品名称
   if (null != productinfobean.getproductname() && !("".equals(productinfobean.getproductname()))) {
    mholder.tvpurductname.settext(productinfobean.getproductname());
   }
  }
  return convertview;
 }
 class viewholder {
  radiobutton rbselect;
  textview tvpurductname;
  linearlayout lldraglayout;
  imageview ivdrag;
  imageview ivdeleteproduct;
 }
 /**
  * 单选按钮事件监听
  * @author wilson
  */
 class productclicklistener implements onclicklistener {
  private int position;
  public productclicklistener(int position) {
   super();
   this.position = position;
  }
  @override
  public void onclick(view v) {
   if (mselectposition == position) {
    mselectposition = -1;
   } else {
    mselectposition = position;
   }
   log.i(tag, "mselectposition=" + mselectposition + " position=" + position);
   mproductlistadapter1.notifydatasetchanged();
  }
 }
 /**
  * 删除当前产品项监听事件
  */
 class removeitemclicklistener implements onclicklistener {
  private int position;
  public removeitemclicklistener(int position) {
   super();
   this.position = position;
  }
  @override
  public void onclick(view v) {
   //1、删除集合中的数据
   productinfolist.remove(position);
   //2、删除服务器端的数据
   mproductlistadapter1.notifydatasetchanged();
   //
   mselectposition = -1;
  }
 }
}

四、源码

完整实例代码点击此处本站下载

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。