Android学习终端研发部

高级UI之炫酷拖拽GridView—可拖拽,记录索引的GridV

2017-07-25  本文已影响179人  goodgleCoder

最近项目有要求对测量模块做一个快捷入口处理,需求要做一个可以拖拽的自定义View。
话不多说,先看 效果图吧:

自定义GridView拖拽,记录保存 多个GridView进行交互

思路:

实现方式

自定义已经显现好的ViewGroup回事一个不错的选择,而其已经为我们摆放好了一些位置如onLayout,onDraw等方法。

@Override
  public boolean onTouchEvent(MotionEvent ev) {
      // TODO Auto-generated method stub
      boolean bool = true;
      if (dragImageView != null && dragPosition != AdapterView.INVALID_POSITION) {
          // 移动时候的对应x,y位置
          bool = super.onTouchEvent(ev);
          int x = (int) ev.getX();
          int y = (int) ev.getY();
          switch (ev.getAction()) {
          case MotionEvent.ACTION_DOWN:
              downX = (int) ev.getX();
              windowX = (int) ev.getX();
              downY = (int) ev.getY();
              windowY = (int) ev.getY();
              break;
          case MotionEvent.ACTION_MOVE:
              onDrag(x, y ,(int) ev.getRawX() , (int) ev.getRawY());
              if (!isMoving){
                  OnMove(x, y);
              }
              if (pointToPosition(x, y) != AdapterView.INVALID_POSITION){
                  break;
              }
              break;
          case MotionEvent.ACTION_UP:
              stopDrag();
              onDrop(x, y);
              requestDisallowInterceptTouchEvent(false);
              break;

          default:
              break;
          }
      }
      return super.onTouchEvent(ev);
  }
 @Override
   protected void dispatchDraw(Canvas canvas) {
       super.dispatchDraw(canvas);
       if(isShowFirstGridLine){
           double count =getChildCount();
           double numCoumn =  getNumColumns();
           int rowNum = (int) Math.ceil(count / numCoumn);
           int rowHeight = getHeight() / rowNum;

           Paint paint = new Paint();
           paint.setColor(ContextCompat.getColor(getContext(),R.color.drawer_item_bg));
           paint.setAntiAlias(true);
           paint.setStrokeWidth(1);
           canvas.drawLine(0,rowHeight,DensityUtil.getScreenIntWidth(getContext()),rowHeight,paint);
       }
   }
 /**
* 类功能描述:</br>
* 数据库管理类
* 博客地址:http://blog.csdn.net/androidstarjack
* 公众号:终端研发部
* @author yuyahao
* @version 1.0 </p> 修改时间:2017-07-19</br> 修改备注:</br>
*/
public class AcheManager {
   private static  final String DEVICE_NAME_KEY = "com.yyh.mode.device.save";
   private static ACache aCache;
   private static  final String CHANNELITEM_KEY = "com.yyh.mode.channelitem.save";
   private static final AtomicReference<AcheManager> INSTANCE = new AtomicReference<AcheManager>();

   private AcheManager(){}
   //单例模式
   public static  AcheManager getInstance() {
       for (;;) {
           AcheManager current = INSTANCE.get();
           if (current != null) {
               return current;
           }
           current = new AcheManager();
           if (INSTANCE.compareAndSet(null, current)) {
               return current;
           }
       }
   }
   /**
    * 保存设备的model
    * @param context
    * @param modelList 要存放的list集合
    */
   public ArrayList<DeviceValue> saveDeviceValue(Context context, ArrayList<DeviceValue> modelList){
       if(aCache == null){
           aCache = ACache.get(context);

       }
       aCache.remove(DEVICE_NAME_KEY);
       aCache.put(DEVICE_NAME_KEY, modelList);
       return modelList;
   }
   /**
    * 获取设备的model
    * @param context
    */
   public    ArrayList<DeviceValue> getDeviceValue(Context context){
       if(aCache == null){
           aCache = ACache.get(context);
       }
       ArrayList<DeviceValue> modelList = (ArrayList<DeviceValue>) aCache.getAsObject(DEVICE_NAME_KEY);
       if(modelList == null) {
           modelList = new ArrayList<>();
       }
       return modelList;
   }
   /**
    * 保存设备的model
    * @param context
    * @param modelList 要存放的list集合
    */
   public ArrayList<ChannelItem> saveChannelItemUserValue(Context context, ArrayList<ChannelItem> modelList){
       if(aCache == null){
           aCache = ACache.get(context);

       }
       aCache.remove(CHANNELITEM_KEY);
       aCache.put(CHANNELITEM_KEY, modelList);
       return modelList;
   }
   /**
    * 获取设备的model
    * @param context
    */
   public    ArrayList<ChannelItem> getChannelItemUserValue(Context context){
       if(aCache == null){
           aCache = ACache.get(context);
       }
       ArrayList<ChannelItem> modelList = (ArrayList<ChannelItem>) aCache.getAsObject(CHANNELITEM_KEY);
       if(modelList == null) {
           modelList = new ArrayList<>();
       }
       return modelList;
   }
   ......
}

扩展

多个View进入交互

效果图下:

多个GridView进行交互

demo下载地址:

https://github.com/androidstarjack/DragMyGridViewToSP

CSDN下载链接

http://download.csdn.net/detail/androidstarjack/9907392

如果你觉得此文对您有所帮助,欢迎入群 QQ交流群 :232203809
微信公众号:终端研发部

技术+职场
上一篇下一篇

猜你喜欢

热点阅读