1. 程式人生 > >android listview拖拽,拖動item 改變位置

android listview拖拽,拖動item 改變位置

在packages/apps/Music/src/touchIncepter.java中

該類提供了listview的拖動效果,並提供介面,在程式介面中實現資料的交換即可。

  1. package com.and.DragListview;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import android.app.ListActivity;  
  5. import android.content.Context;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.ImageView;  
  12. import android.widget.TextView;  
  13. publicclass DragListview extends ListActivity {     
  14.     MyAdapter adapter;  
  15.     TouchInterceptor list;  
  16.     List<String> arrayText;  
  17.     @Override
  18.     publicvoid onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         list = (TouchInterceptor) getListView();//(TouchInterceptor)findViewById(android.R.id.list);
  22.         getText();  
  23.         adapter = new
     MyAdapter(this);  
  24.         setListAdapter(adapter);  
  25.         list.setDropListener(mDropListener);  
  26.      //   list.setRemoveListener(mRemoveListener);      
  27.     }  
  28.     publicvoid getText(){  
  29.         arrayText = new ArrayList<String>();  
  30.         arrayText.add("傳奇");  
  31.         arrayText.add("紅豆");  
  32.         arrayText.add("流年");  
  33.         arrayText.add("棋子");  
  34.     }  
  35.     //交換listview的資料
  36.     private TouchInterceptor.DropListener mDropListener =  
  37.         new TouchInterceptor.DropListener() {  
  38.         publicvoid drop(int from, int to) {  
  39.             String item = arrayText.get(from);  
  40.             arrayText.remove(item);//.remove(from);
  41.             arrayText.add(to, item);  
  42.             adapter.notifyDataSetChanged();  
  43.         }  
  44.     };  
  45.     private TouchInterceptor.RemoveListener mRemoveListener =  
  46.         new TouchInterceptor.RemoveListener() {  
  47.         publicvoid remove(int which) {            
  48.         }  
  49.     };  
  50.     class MyAdapter extends BaseAdapter{  
  51.         private LayoutInflater mInflater;  
  52.         Context mContext;  
  53.         public MyAdapter(Context c){  
  54.             mInflater = LayoutInflater.from(c);  
  55.         }  
  56.         publicint getCount() {           
  57.             return arrayText.size();  
  58.         }  
  59.         public Object getItem(int arg0) {  
  60.             return arrayText.get(arg0);  
  61.         }  
  62.         publiclong getItemId(int arg0) {  
  63.             return arg0;  
  64.         }  
  65.         public View getView(int arg0, View contentView, ViewGroup arg2) {  
  66.             ImageView img;  
  67.             TextView text;  
  68.             if(contentView==null){  
  69.                 contentView = mInflater.inflate(R.layout.list_layout, null);   
  70.                 //contentView = mInflater.inflate(R.layout.list_layout,null);
  71.             }  
  72.             img = (ImageView)contentView.findViewById(R.id.img);  
  73.             img.setBackgroundResource(R.drawable.icon);  
  74.             text = (TextView)contentView.findViewById(R.id.text);  
  75.             text.setText(arrayText.get(arg0).toString());  
  76.             return contentView;  
  77.         }  
  78.     }  
  79. }  

原始碼中的類:
  1. /* 
  2.  * Copyright (C) 2008 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */
  16. package com.and.DragListview;  
  17. import android.content.Context;  
  18. import android.content.SharedPreferences;  
  19. import android.content.res.Resources;  
  20. import android.graphics.Bitmap;  
  21. import android.graphics.PixelFormat;  
  22. import android.graphics.Rect;  
  23. import android.util.AttributeSet;  
  24. import android.view.GestureDetector;  
  25. import android.view.Gravity;  
  26. import android.view.MotionEvent;  
  27. import android.view.View;  
  28. import android.view.ViewConfiguration;  
  29. import android.view.ViewGroup;  
  30. import android.view.WindowManager;  
  31. import android.view.GestureDetector.SimpleOnGestureListener;  
  32. import android.widget.AdapterView;  
  33. import android.widget.ImageView;  
  34. 相關推薦

    android listviewitem 改變位置

    在packages/apps/Music/src/touchIncepter.java中 該類提供了listview的拖動效果,並提供介面,在程式介面中實現資料的交換即可。 package com.and.DragListvie

    如何在Listview其中的子Item移動其位置

    最近開發遇到一個需求,需要顯示的物件支援拖拽,一般的物件直接使用dargarea即可。但我用的是Listview來顯示,而且是自己定義的listmodel,可以說是相當的煩。 1、一種情況,如果是使用qml中的listmodel則直接呼叫model的move方法進行移動:核心程式碼為 &nb

    Android 使用變形矩陣實現可以縮放旋轉的影象

    上篇博文介紹了變形矩陣的一些用法,所以這篇博文就結合變形矩陣來實現一個可以拖拽、縮放、旋轉的影象吧。 首先,我們就繼承ImageView來實現我們的自定義View。 程式碼如下: public class MyMatrixImg extends Ima

    模擬點選滑鼠移動按鍵下拉框的處理

    1.模擬點選 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from time import sleep driver = webdriver.Chrom

    安卓實現任意控制元件view可並監聽和點選事件可自動拉回螢幕邊緣

    因為專案中有需要實現控制元件可任意拖拽的需求,所以簡單寫了個自定義OnTouchListener,以作拋磚引玉,歡迎大家提議反饋。 完整實現類如下,程式碼中有詳細註釋: 使用者可以決定是否開啟自動拖拽邊緣功能,可以監聽控制元件的拖拽和點選事件 public cl

    RecyclerView實現條目左滑、右滑移除效果

    對於android開發者來說RecyclerView應該已經很熟悉了,專案中基本都是使用RecyclerView來實現列表效果,這裡要實現的是RecyclerView的條目拖拽、左滑、右滑移除效果,這些效果都是在RecyclerView列表基礎上來實現的,所以還是先簡單的實現RecyclerVi

    QML實現的無邊框視窗的拉伸基本解決閃爍嚴重問題

    使用qt製作的無邊框視窗,只需在其flag中加入FramelessWindowHint。然而,無邊框視窗意味著,無法使用原有的邊框拉伸,拖拽功能。在qwidget中,有很多實現的方法,比如重寫    + mouseMoveEvent(QMouseEvent *event)  

    一個可移動自由組合子控制元件的檢視控制元件讓開發更簡單

    今天給大家推薦一個自由拖拽,自由組合的控制元件,這個控制元件是我自定義寫的。通過它,我們可以自由拖拽,自由組合實現一個介面,滿足一個使用者自由組合介面的需求。這裡不是通過自由拖拽控制元件,來快速開發一個介面,而且更人性化的讓使用者去自由組合一個介面。 前言

    使用UGUI實現揹包物體的交換等操作

    本例項通過UGUI實現揹包的簡單功能,物品的拖拽,交換,拖放位置處理,還有針對揹包多頁面的滑動翻頁的處理。 具體的程式碼如下。 首先是UIItem類,這個揹包內各種物體的類: using UnityEngine; using System.Collections; usi

    html5要在ondragover阻止這個預設事件,和解決火狐上的相容問題

    在火狐上不相容這一套: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <styl

    如何解決div裡面的h4標題並且實現div裡面的文字能夠選中和不能選中的問題

    <!doctype html> <html> <head> <meta charset="utf-8"> <title>無標題文件</title> <style> *{ margin:0;

    android 輸入法彈出鍵盤把listview頂上去保留頂部標題欄位置

    文章出處:http://blog.csdn.net/xingCome/article/details/51424724 只為記錄。 一:給listview 設定屬性 android:transcriptMode="normal"(必須) android:fastSc

    支援多檔案上傳預覽基於bootstrap的上傳外掛fileinput的ajax非同步上傳

    首先需要匯入一些js和css檔案 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"> <link type="text/css" rel="stylesheet" href="__

    自定義事件時div可以img不了的解決方法

    原因:未阻止事件流和預設事件 解決方法: function pauseEvent(e){ //已做相容性處理 if(e.stopPropagation) e.stopPropagation(); if(e.preventDefault) e.pre

    適合pc端的移動分享一下。

    h5新加的特性拖拽事件,但是隻適合PC端哦。不多說了上程式碼   <!DOCTYPE html>

    Android ListView動態刷新某項Item

    ack static auto ids avi idt ica horizon extc 使用ViewHolder來刷新某項數據,而不用每次都全部刷新數據。 繼承BaseAdapter,新建ViewHolder類。 public class TestListAdap

    詳細講解html5放技術的區別及優勢

    提到拖拽,我們都很熟悉,那麼拖放呢?一字之差,代表的意義是不一樣的,拖拽就是拉著走,拖放就是有拖,有放,我們都知道原生 JS 拖拽效果的缺點:1. 程式碼相對複雜與冗餘2. 僅限於在瀏覽器內的元素間拖放3、不能實現跨頁面的拖放 所以H5就出現了拖放技術,與 JS 原生相比 HTML5 拖放的優勢

    HTML5 drag & drop

    執行 javascrip eve eight 觸發 元素 padding 軟件 測試 關鍵詞: 1. draggable:規定元素是否可拖動的,draggable=true可拖動 2. dataTransfer:拖拽對象用來傳遞的媒介,使用方式:event.dataTran

    Android ListView功能擴充套件實現高效能的瀑布流佈局

    經過前面兩篇文章的學習,我們已經對ListView進行了非常深層次的剖析,不僅瞭解了ListView的原始碼和它的工作原理,同時也將ListView中常見的一些問題進行了歸納和總結。那麼本篇文章是我們ListView系列三部曲的最後一篇,在這篇文章當中我們將對ListView

    Android ListView效能優化非同步載入圖片

    <span style="font-size:14px;"><span style="font-size:14px;">public static void initImageLoader(Context context) { personOptions = new D