版权声明:本文为HaiyuKing原创文章,转载请注明出处!

前言

记录使用ItemTouchHelper对Recyclerview进行拖拽排序功能的实现。

效果图

RecyclerViewItemTouchHelperDemo【使用ItemTouchHelper进行拖拽排序功能】

代码分析

ItemTouchHelper是一个工具类,可实现侧滑删除和拖拽移动,使用这个工具类需要RecyclerView和Callback。同时根据需要重写onMove和onSwiped方法。

使用步骤

一、项目组织结构图

RecyclerViewItemTouchHelperDemo【使用ItemTouchHelper进行拖拽排序功能】

RecyclerViewItemTouchHelperDemo【使用ItemTouchHelper进行拖拽排序功能】

注意事项:

1、  导入类文件后需要change包名以及重新import R文件路径

2、  Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖

二、导入步骤

(1)在build.gradle中引用recyclerview【版本号和appcompat保持一致】

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.why.project.recyclerviewitemtouchhelperdemo"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //RecyclerView
    compile "com.android.support:recyclerview-v7:27.1.1"
}

(2)在AndroidManifest.xml中声明震动权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.why.project.recyclerviewitemtouchhelperdemo">

    <!-- ======================RecyclerView拖拽震动效果====================== -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

(3)在项目中实现Recyclerview基本数据展现

1、创建Bean类

package com.why.project.recyclerviewitemtouchhelperdemo.bean;

/**
 * Created by HaiyuKing
 * Used 列表项的bean类
 */

public class ChannelBean {
    private String channelId;//频道id值
    private String channelName;//频道名称

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }
}
ChannelBean.java

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-02-05
  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
猜你喜欢
  • 2021-11-02
  • 2022-12-23
  • 2021-12-02
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案