• Guest
HabraHabr
  • Main
  • Users

  • Development
    • Programming
    • Information Security
    • Website development
    • JavaScript
    • Game development
    • Open source
    • Developed for Android
    • Machine learning
    • Abnormal programming
    • Java
    • Python
    • Development of mobile applications
    • Analysis and design of systems
    • .NET
    • Mathematics
    • Algorithms
    • C#
    • System Programming
    • C++
    • C
    • Go
    • PHP
    • Reverse engineering
    • Assembler
    • Development under Linux
    • Big Data
    • Rust
    • Cryptography
    • Entertaining problems
    • Testing of IT systems
    • Testing Web Services
    • HTML
    • Programming microcontrollers
    • API
    • High performance
    • Developed for iOS
    • CSS
    • Industrial Programming
    • Development under Windows
    • Image processing
    • Compilers
    • FPGA
    • Professional literature
    • OpenStreetMap
    • Google Chrome
    • Data Mining
    • PostgreSQL
    • Development of robotics
    • Visualization of data
    • Angular
    • ReactJS
    • Search technologies
    • Debugging
    • Test mobile applications
    • Browsers
    • Designing and refactoring
    • IT Standards
    • Solidity
    • Node.JS
    • Git
    • LaTeX
    • SQL
    • Haskell
    • Unreal Engine
    • Unity3D
    • Development for the Internet of things
    • Functional Programming
    • Amazon Web Services
    • Google Cloud Platform
    • Development under AR and VR
    • Assembly systems
    • Version control systems
    • Kotlin
    • R
    • CAD/CAM
    • Customer Optimization
    • Development of communication systems
    • Microsoft Azure
    • Perfect code
    • Atlassian
    • Visual Studio
    • NoSQL
    • Yii
    • Mono и Moonlight
    • Parallel Programming
    • Asterisk
    • Yandex API
    • WordPress
    • Sports programming
    • Lua
    • Microsoft SQL Server
    • Payment systems
    • TypeScript
    • Scala
    • Google API
    • Development of data transmission systems
    • XML
    • Regular expressions
    • Development under Tizen
    • Swift
    • MySQL
    • Geoinformation services
    • Global Positioning Systems
    • Qt
    • Dart
    • Django
    • Development for Office 365
    • Erlang/OTP
    • GPGPU
    • Eclipse
    • Maps API
    • Testing games
    • Browser Extensions
    • 1C-Bitrix
    • Development under e-commerce
    • Xamarin
    • Xcode
    • Development under Windows Phone
    • Semantics
    • CMS
    • VueJS
    • GitHub
    • Open data
    • Sphinx
    • Ruby on Rails
    • Ruby
    • Symfony
    • Drupal
    • Messaging Systems
    • CTF
    • SaaS / S+S
    • SharePoint
    • jQuery
    • Puppet
    • Firefox
    • Elm
    • MODX
    • Billing systems
    • Graphical shells
    • Kodobred
    • MongoDB
    • SCADA
    • Hadoop
    • Gradle
    • Clojure
    • F#
    • CoffeeScript
    • Matlab
    • Phalcon
    • Development under Sailfish OS
    • Magento
    • Elixir/Phoenix
    • Microsoft Edge
    • Layout of letters
    • Development for OS X
    • Forth
    • Smalltalk
    • Julia
    • Laravel
    • WebGL
    • Meteor.JS
    • Firebird/Interbase
    • SQLite
    • D
    • Mesh-networks
    • I2P
    • Derby.js
    • Emacs
    • Development under Bada
    • Mercurial
    • UML Design
    • Objective C
    • Fortran
    • Cocoa
    • Cobol
    • Apache Flex
    • Action Script
    • Joomla
    • IIS
    • Twitter API
    • Vkontakte API
    • Facebook API
    • Microsoft Access
    • PDF
    • Prolog
    • GTK+
    • LabVIEW
    • Brainfuck
    • Cubrid
    • Canvas
    • Doctrine ORM
    • Google App Engine
    • Twisted
    • XSLT
    • TDD
    • Small Basic
    • Kohana
    • Development for Java ME
    • LiveStreet
    • MooTools
    • Adobe Flash
    • GreaseMonkey
    • INFOLUST
    • Groovy & Grails
    • Lisp
    • Delphi
    • Zend Framework
    • ExtJS / Sencha Library
    • Internet Explorer
    • CodeIgniter
    • Silverlight
    • Google Web Toolkit
    • CakePHP
    • Safari
    • Opera
    • Microformats
    • Ajax
    • VIM
  • Administration
    • System administration
    • IT Infrastructure
    • *nix
    • Network technologies
    • DevOps
    • Server Administration
    • Cloud computing
    • Configuring Linux
    • Wireless technologies
    • Virtualization
    • Hosting
    • Data storage
    • Decentralized networks
    • Database Administration
    • Data Warehousing
    • Communication standards
    • PowerShell
    • Backup
    • Cisco
    • Nginx
    • Antivirus protection
    • DNS
    • Server Optimization
    • Data recovery
    • Apache
    • Spam and antispam
    • Data Compression
    • SAN
    • IPv6
    • Fidonet
    • IPTV
    • Shells
    • Administering domain names
  • Design
    • Interfaces
    • Web design
    • Working with sound
    • Usability
    • Graphic design
    • Design Games
    • Mobile App Design
    • Working with 3D-graphics
    • Typography
    • Working with video
    • Work with vector graphics
    • Accessibility
    • Prototyping
    • CGI (graphics)
    • Computer Animation
    • Working with icons
  • Control
    • Careers in the IT industry
    • Project management
    • Development Management
    • Personnel Management
    • Product Management
    • Start-up development
    • Managing the community
    • Service Desk
    • GTD
    • IT Terminology
    • Agile
    • Business Models
    • Legislation and IT-business
    • Sales management
    • CRM-systems
    • Product localization
    • ECM / EDS
    • Freelance
    • Venture investments
    • ERP-systems
    • Help Desk Software
    • Media management
    • Patenting
    • E-commerce management
    • Creative Commons
  • Marketing
    • Conferences
    • Promotion of games
    • Internet Marketing
    • Search Engine Optimization
    • Web Analytics
    • Monetize Web services
    • Content marketing
    • Monetization of IT systems
    • Monetize mobile apps
    • Mobile App Analytics
    • Growth Hacking
    • Branding
    • Monetize Games
    • Display ads
    • Contextual advertising
    • Increase Conversion Rate
  • Sundry
    • Reading room
    • Educational process in IT
    • Research and forecasts in IT
    • Finance in IT
    • Hakatonas
    • IT emigration
    • Education abroad
    • Lumber room
    • I'm on my way

Drag and Swipe in RecyclerView. Part 1: ItemTouchHelper

Drag and Swipe in RecyclerView. Part 1: ItemTouchHelper 3r3444.
 3r33450.
There are many tutorials, libraries, and examples of the implementation of 3r33428. drag & drop
and 3r33428. swipe-to-dismiss
in Android using RecyclerView. Most of them still use the outdated 3r314. View.ondragListener
and approach 3r316. SwipeToDismiss
designed by Roman Nurik. Although new and more effective methods are already available. Very few use the latest API, often relying on 3r33430. GestureDetectors and 3r33430. onInterceptTouchEvent or on other more complex implementations. In fact, there is a very simple way to add these functions to RecyclerView . This requires only one class, which is also part of the Android Support Library. 3r3444. RecyclerView.ItemDecoration , making it easy to add to almost any existing 3r33030. LayoutManager and adapter. It also works with animation elements and provides the ability to drag items of one type to another place in the list and much more. In this article, I will demonstrate a simple implementation of ItemTouchHelper . Later, in this series of articles, we will expand the scope and consider the remaining possibilities. 3r3444.
 3r33450.

3r33434. Note. 3r33434. Want to see the result right away? Check out Github: Android-ItemTouchHelper-Demo . 3r3442. The first commit 3r3443. refers to this article. Demo 3r33430. .apk file can be downloaded here is . 3r3444.
 3r33450.

3r366. 3r3444.
 3r33450.

Setup


 3r33450.

First we need to configure RecyclerView . If you have not done so yet, add the dependency RecyclerView in your file 3r33030. build.gradle . 3r3444.
 3r33450. 3r3013. 3r33430. compile 'com.android.support:recyclerview-v7:???' 3r3407.
 3r33450.

3r33430. ItemTouchHelper will work with virtually any RecyclerView.Adapter and 3r33430. LayoutManager , but this article is based on examples using r3r397. These files are . 3r3444.
 3r33450. 3r3102. Use ItemTouchHelper and ItemTouchHelper.Callback
 3r33450.

To use ItemTouchHelper , you need to create ItemTouchHelper.Callback . This is an interface that allows you to track the movement (3r33232. Eng. 3r33232. Move) and swipe (3r33232. Eng. 3r33233. Swipe). In addition, here you can monitor the status of the selected view -component and override the default animation. There is a helper class that you can use if you want to use the basic implementation, 3r3117. SimpleCallback . But in order to understand how this works in practice, we will do everything on our own. 3r3444.
 3r33450.

The main functions of the interface that we must override to enable the basic functionality of 3r33428. drag & drop and 3r33428. swipe-to-dismiss :


 3r33450. 3r3013. 3r3402. getMovementFlags (RecyclerView, ViewHolder)
3r33450. onMove (RecyclerView, ViewHolder, ViewHolder)
3r33450. onSwiped (ViewHolder, int) 3r3407.
 3r33450.

We will also use several auxiliary methods: 3r3444.
 3r33450. 3r3013. 3r3402. isLongPressDragEnabled ()
3r33450. isItemViewSwipeEnabled () 3r3407.
 3r33450.

Consider them one by one. 3r3444.
 3r33450. 3r3013. 3r3402. @Override
public int getMovementFlags (RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 3r33450. int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; 3r33450. return makeMovementFlags (dragFlags, swipeFlags); 3r33450.} 3r3407.
 3r33450.

3r33430. ItemTouchHelper makes it easy to determine the direction of the event. You need to override the method getMovementFlags () to indicate which directions to drag and drop will be supported. To create returned flags, use an auxiliary method ItemTouchHelper.makeMovementFlags (int, int) . In this example, we allow dragging and brushing in both directions. 3r3444.
 3r33450. 3r3013. 3r3402. @Override
public boolean isLongPressDragEnabled () {
return true; 3r33450.} 3r3407.
 3r33450.

3r33430. ItemTouchHelper can only be used for dragging without the swipe function (or vice versa), so you must specify which features should be supported. Method isLongPressDragEnabled () should return the value true so that dragging is supported after a long press on element RecyclerView . Alternatively, you can call method ItemTouchHelper.startDrag (RecyclerView.ViewHolder) to start dragging manually. Consider this option later. 3r3444.
 3r33450. 3r3013. 3r3402. @Override
public boolean isItemViewSwipeEnabled () {
return true; 3r33450.} 3r3407.
 3r33450.

To enable swipe after touching anywhere within 3-333430. view -component, just return the value true from method 3r33030. isItemViewSwipeEnabled () . Alternatively, you can call method ItemTouchHelper.startSwipe (RecyclerView.ViewHolder) to start swiping manually. 3r3444.
 3r33450.

The following two methods, onMove () and 3r33430. onSwiped () , are required in order to notify about updating data. So, first we will create an interface that allows you to pass these events along a chain of calls. 3r3444.
 3r33450.

3r33232. ItemTouchHelperAdapter.java 3r3444.
 3r33450. 3r3013. 3r3402. public interface ItemTouchHelperAdapter {
3r33450. void onItemMove (int fromPosition, int toPosition); 3r33450. 3r33450. void onItemDismiss (int position); 3r33450.} 3r3407.
 3r33450.

The easiest way to do this is to make RecyclerListAdapter implemented the listener. 3r3444.
 3r33450. 3r3013. 3r3402. public class RecyclerListAdapter extends
RecyclerView.Adapter 3r33450. implements ItemTouchHelperAdapter {
3r33450. //code from[примера](https://gist.github.com/iPaulPro/2216ea5e14818056cfcc#file-recyclerlistadapter-java)
3r33450. @Override
public void onItemDismiss (int position) {
mItems.remove (position); 3r33450. notifyItemRemoved (position); 3r33450.} 3r33450. 3r33450. @Override
public boolean onItemMove (int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap (mItems, i, i + 1);
}
} else {
for (int i = fromPosition; i> toPosition; i -) {
Collections.swap (mItems, i, i - 1);
}
}
NotifyItemMoved (fromPosition, toPosition); 3rrr5050.  3r33450.

It is very important to call methods notifyItemRemoved () and 3r33430. notifyItemMoved () So that the adapter sees the changes. It should also be noted that we change the position of the element every time view -component is shifted to a new index, and 3r3343428. not at the very end of the move (event “drop”) 3r33434. . 3r3444.
 3r33450.

Now we can go back to creating SimpleItemTouchHelperCallback since we still need to override the methods onMove () and 3r33430. onSwiped () . First add the constructor and field for the adapter:


 3r33450. 3r3013. 3r3402. private final ItemTouchHelperAdapter mAdapter; 3r33450. 3r33450. public SimpleItemTouchHelperCallback (
ItemTouchHelperAdapter adapter) {
mAdapter = adapter; 3r33450.} 3r3407.
 3r33450.

Then redefine the remaining events and report this to the adapter:


 3r33450. 3r3013. 3r3402. @Override
public boolean onMove (RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
mAdapter.onItemMove (viewHolder.getAdapterPosition (),
target.getAdapterPosition ()); 3r33450. return true; 3r33450.} 3r33450. @Override
public void onSwiped (RecyclerView.ViewHolder viewHolder,
int direction) {
mAdapter.onItemDismiss (viewHolder.getAdapterPosition ()); 3r33450.} 3r3407.
 3r33450.

As a result, the class Callback should look something like this:


 3r33450. 3r3013. 3r3402. public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
3r33450. private final ItemTouchHelperAdapter mAdapter; 3r33450. 3r33450. public SimpleItemTouchHelperCallback (ItemTouchHelperAdapter adapter) {
mAdapter = adapter; 3r33450.} 3r33450. 3r33450. @Override
public boolean isLongPressDragEnabled () {
return true; 3r33450.} 3r33450. 3r33450. @Override
public boolean isItemViewSwipeEnabled () {
return true; 3r33450.} 3r33450. 3r33450. @Override
public int getMovementFlags (RecyclerView recyclerView, ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 3r33450. int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; 3r33450. return makeMovementFlags (dragFlags, swipeFlags); 3r33450.} 3r33450. 3r33450. @Override
public boolean onMove (RecyclerView recyclerView, ViewHolder viewHolder,
ViewHolder target) {
mAdapter.onItemMove (viewHolder.getAdapterPosition (), target.getAdapterPosition ()); 3r33450. return true; 3r33450.} 3r33450. 3r33450. @Override
public void onSwiped (ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss (viewHolder.getAdapterPosition ()); 3r33450.} 3r33450. 3r33450.} 3r3407.
 3r33450.

When the callback is ready, we can create ItemTouchHelper and call the method. attachToRecyclerView (RecyclerView) (for example, in MainFragment.java ):


 3r33450. 3r3013. 3r3402. ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback (adapter); 3r33450. ItemTouchHelper touchHelper = new ItemTouchHelper (callback); 3r33450. touchHelper.attachToRecyclerView (recyclerView); 3r3407.
 3r33450.

After the launch should get something like this:


 3r33450.

3r3155. 3r3444.
 3r33450. 3r33434. Conclusion 3r3r436.
 3r33450.

This is the most simplified implementation of ItemTouchHelper . However, you may notice that you do not need to use third-party library for implementing standard actions drag & drop and 3r33428. swipe-to-dismiss in 3r33430. RecyclerView . In the next part, we will pay more attention to the appearance of the elements at the time of dragging or swiping. 3r3444.
 3r33450. 3r33434. Source code
 3r33450.
I created a project on GitHub to show what is described in this series of articles: 3r34040. Android-ItemTouchHelper-Demo
. 3r3442. The first commit 3r3443. mainly refers to this part and a little to the second. 3r3444.
3r33450. 3r33450. 3r33448. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d! ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();
3r33450.

It may be interesting

  • Comments
  • About article
  • Similar news
This publication has no comments.

weber

Author

2-11-2018, 08:28

Publication Date

Development / Programming

Category
  • Comments: 0
  • Views: 396
RecyclerView tree list (without
Drag and Swipe in RecyclerView. Part 2:
JsonWriterSax is a library for creating
From Dribbble to Android Motion
Animated numbers on Android
Use the debugger Android Studio to a
Write a comment
Name:*
E-Mail:


Comments
Quickly this site  could indisputably generally always be dominant relating to every one of web  site buyers, as a consequence of fastidious stories plus exams https://www.pizzahutcouponcode.com/pizza-hut-coupons-code/


Helpful information. Fortunate me I discovered your web site accidentally,
and I am stunned why this accident did not happen earlier! I bookmarked it. Thanks, I've recently been looking for information about this topic for [hide]a[https://www.pizzahutcouponcode.com/pizza-hut-coupons-code/
] long time and yours is the greatest I've discovered so far. But, what concerning the conclusion? Are you positive about the source?


Today, 09:15

Alytani

this is really nice to read..informative post is very good to read..thanks a lot! How is the cost of house cleaning calculated?
Yesterday, 17:14

Legend SEO

It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.

entegrasyon programları
Yesterday, 17:09

taxiseo2

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.

entegrasyon programları
Yesterday, 17:02

taxiseo2

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...먹튀

Yesterday, 16:50

raymond weber

Adv
Website for web developers. New scripts, best ideas, programming tips. How to write a script for you here, we have a lot of information about various programming languages. You are a webmaster or a beginner programmer, it does not matter, useful articles will help to make your favorite business faster.

Login

Registration Forgot password