android 實現圖片選擇拖拽控制項

來源:互聯網
上載者:User

標籤:android   圖片   拖拽   

1.使用RecyclerView

使用RecyclerView可以輕鬆實現圖片切換時的動畫過程,這點要好於GridView。

2. 拖拽的實現

  • Dragmanager

繼承View.OnDragListener,對拖拽過程中進行操作,

Action_drag_started 擷取到操作的Item

Action_Drag_location 根據每個停留的位置判斷是否交換item的位置。

Action_Drag_ended 跟新位置

  • DragSortAdapter

抽象類別,為recyclerView 增加onItemTouchListener和onScrollListener, 記錄onTouch的item,並在拖拽過程中判斷recyclerview是否可以滾動,從而在拖拽的item快到邊界時滾動recyclerView,使可以與本來在螢幕上不可見的item進行交換位置。

  • ViewHolder

ViewHolder 實現startDrag方法


2.itemDecoration


為recyclerview item增加divider,可以有兩種方式,覆蓋onDraw方法繪製itemDivider,或者覆蓋getItemOffsets方法,使item之間可以分隔開。

3.GridlayoutManager

當recyclerview嵌入到scrollview中時,需要複寫LayoutManager,在這裡複寫其中的onMeasure方法,需要計算每個item的高度或者寬度進行疊加,當recyclerview中item很多時,不要採用這種方式,回導致view不能複用,其中在計算item高度時,需要加上每個item的itemOffsets,查看recyclerview的源碼發現,無法直接擷取到item的offsets,最終採用反射的方式擷取到其值:

try {    Method method = recyclerView.getClass().getDeclaredMethod("getItemDecorInsetsForChild",View.class);    method.setAccessible(true);    final Rect insets = (Rect)method.invoke(recyclerView, child);    itemDecorationHeight = heightUsed + insets.height();    itemDecorationWidth = widthUsed + insets.width();} catch (NoSuchMethodException e){    Log.d("FullGridLayoutManager","no method found");}catch(IllegalAccessException e){    Log.d("FullGridLayoutManager","IllegalAccessException");}catch (InvocationTargetException e){    Log.d("FullGridLayoutManager","InvocationTargetException");}



android 實現圖片選擇拖拽控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.