【问题标题】:Why is my RecyclerVIewSwipeDecorator library not getting imported?为什么我的 RecyclerVIewSwipeDecorator 库没有被导入?
【发布时间】:2021-06-20 12:31:18
【问题描述】:

我正在尝试在我的应用程序上实现以下库: https://github.com/xabaras/RecyclerViewSwipeDecorator

我已经在 gradle 中同步了,但是无法导入使用。

这是我的毕业论文:


plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.mylist"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    //For view binding
    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    //CardView
    implementation "androidx.cardview:cardview:1.0.0"

    //Recycler View Swipe Decorator
    implementation 'it.xabaras.android:recyclerview-swipedecorator:1.2.3'

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-firestore:23.0.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

其他等级:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath 'com.google.gms:google-services:4.3.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        //jcenter() // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我尝试使用它的地方:

package com.example.mylist;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mylist.Adapters.TaskAdapter;

//this shows error on xabaras
import it.xabaras.android.recyclerview.swipedecorator.RecyclerViewSwipeDecorator;



public class TouchHelper extends ItemTouchHelper.SimpleCallback {
    private TaskAdapter taskAdapter;

    public TouchHelper(TaskAdapter taskAdapter) {
        super(0 , ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT); //for dragging and swiping
        this.taskAdapter = taskAdapter;
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        final int position = viewHolder.getAdapterPosition();
        if (direction == ItemTouchHelper.RIGHT){
            AlertDialog.Builder builder = new AlertDialog.Builder(taskAdapter.getContext());
            builder.setMessage("Are you sure you want to delete this task?")
                    .setTitle("Delete Task")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            taskAdapter.deleteTask(position);
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    taskAdapter.notifyItemChanged(position);
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();
        }else{
            taskAdapter.editTask(position);
        }
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

//can't use these
        new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
                .addSwipeRightActionIcon(R.drawable.ic_baseline_delete_24)
                .addSwipeRightBackgroundColor(Color.RED)
                .addSwipeLeftActionIcon(R.drawable.ic_baseline_edit_24)
                .addSwipeLeftBackgroundColor(ContextCompat.getColor(taskAdapter.getContext() , R.color.edit_color))
                .create()
                .decorate();
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

我曾尝试在 gradle 中单独实现 RecyclerView,但没有奏效。此外,我的所有活动都使用 ViewBinding,所以这可能是一个影响因素。

请帮帮我。

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    他们正在使用来自基本构建 gradle 文件的 jcenter() 库。因此,请确保您已从基础构建 gradle 添加了存储库。

    buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
            classpath 'com.android.tools.build:gradle:3.4.1'
            ...
        }
    }
    
    allprojects {
        repositories {
            jcenter()
            google()
        }
    }
    

    jcenter() 已被弃用并与mavenCentral() 核实

    【讨论】:

      【解决方案2】:

      要使用最新版本 (1.3) 的 RecyclerViewSwipeDecorator,首先将 ma​​ven 添加到您的项目 Gradle 的存储库中:

      allprojects {
          repositories {
              google()
              mavenCentral()
              maven() { url 'https://jitpack.io' }  <-- this one
          }
      }
      

      接下来,在您的 Module Gradle 中添加此依赖项:

      implementation 'com.github.xabaras:RecyclerViewSwipeDecorator:1.3'
      

      【讨论】:

        猜你喜欢
        • 2023-02-07
        • 2011-08-08
        • 2013-08-10
        • 2020-10-30
        • 2018-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多