Tag Archives: boiler

Inconsistency between adapter data and UI data after dragging recyclerview (data disorder)

Firstly, the function of dragging and sliding is added on the basis of recyclerview. It is written directly according to the official document as follows

 ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
            ): Int {
                //item dragging direction
                var dragflag =
                    ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
                return makeMovementFlags(dragflag, 0)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                myAdapter.notifyItemMoved(viewHolder.layoutPosition, target.layoutPosition)
                return true
            }


            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}

            override fun canDropOver(
                recyclerView: RecyclerView,
                current: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                //The current ViewHolder can be placed on the target ViewHolder
                return true
            }

            override fun isLongPressDragEnabled(): Boolean {
                //Turn on long press drag
                return true
            }

        }).attachToRecyclerView(binding.myRV)//add to RecyclerView

After running, it is found that the UI is normal, but when obtaining adapter.items, it is found that the data has not changed, so perform data exchange and sorting in onmove:

ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
            ): Int {
                var dragflag =
                    ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
                return makeMovementFlags(dragflag, 0)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                if (viewHolder.layoutPosition < target.layoutPosition) {
                    for (i in viewHolder.layoutPosition until target.layoutPosition) {
                        Collections.swap(myAdapter.items, i, i + 1) 
                    }
                } else {
                    for (i in viewHolder.layoutPosition downTo target.layoutPosition + 1) {
                        Collections.swap(myAdapter.items, i, i - 1) 
                    }

                }
                myAdapter.notifyItemMoved(viewHolder.layoutPosition, target.layoutPosition)
                myAdapter.notifyItemChanged(target.layoutPosition)
                return true
            }


            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}

            override fun canDropOver(
                recyclerView: RecyclerView,
                current: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                return true
            }

            override fun isLongPressDragEnabled(): Boolean {
                return true
            }

        }).attachToRecyclerView(binding.myRV)

If you want to listen to the end of drag, you can override the Clearview method to listen

[Solved] Android Studio Error: The binary version of its metadata is 1.5.1, expected version is 1.1.15.

Question:

Android studio reports an error: module was compiled with an incompatible version of kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
Solution 1:

Add ext.kotlin in build.gradle of the project_ Version = "1.3.72" (lower version) changed to ext.kotlin_ Version = "1.5.21" (not necessarily 1.5.21, but also the latest version). Just sync and run again

Solution 2:

Open build.gradle under your module (my module name here is MyLibrary) directory, delete some lines in dependencies, and only the corresponding lines in the second figure are left. Just run it again

these lines remain:

[Solved] Ktolin Android Project Compile Error: w: Runtime JAR files in the classpath should have the same version. These file

I haven’t opened the Android project for a while; Open compilation and report an error
w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
e: D:/software/Gradle_ workplace/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.5.10/6b84d926e28493be69daf673e40076f89492ef7/kotlin-stdlib-common-1.5.10.jar!/ META-INF/kotlin-stdlib-common.kotlin_ Module: module was compiled with an incompatible version of kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.13.
insert a picture description here

the final solution is:

add this dependency; Implementation “androidx. Core: core KTX: +”
remove it and you can compile it;

Feeling is conflict; Moreover, this feeling at the beginning of Android x is prone to conflict sometimes. After all, the dependence on the new package name feels unstable. If there is a problem in the future, just lean in this direction;

The boss who knows the problem can also give advice;

Environment:
kotlin version is 1.3.11
JDK is 1.8
gradle is 4.6
Android studio is 4.1.3

ktor org.jetbrains:kotlin-css-jvm Dependency Failure [Solved]

1, upgrade the version number of css-jvm
//    implementation "org.jetbrains:kotlin-css-jvm:1.0.0-pre.31-kotlin-1.2.41"
    implementation "org.jetbrains:kotlin-css-jvm:1.0.0-pre.122-kotlin-1.4.10"

2. Gradle uses a higher version. When I started to compile it, I used 5.6.4-all. There were many problems that I couldn’t download it. Later, I changed it to 6.7.1-all

3. You can also start without relying on this library