Android Проблема импорта Studio RecyclerView - PullRequest
0 голосов
/ 10 февраля 2020

В настоящее время я получаю сообщение об ошибке при попытке импортировать представление переработчика для моего проекта.

import android.support.v7.widget.RecyclerView;

Вот мой build.gradle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.zooapp"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
            'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.android.support:recyclerview-v7:29.0.0'
}

И вот начало моего кода:

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;


public class AnimalsHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    private final ImageView animalIcon;
    private final TextView animalName;

Когда это так, и я пытаюсь нажать кнопку, чтобы открыть новое действие (где должен быть вид рециркулятора), приложение просто падает и говорит:

android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.v7.widget.RecyclerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)

Тогда, когда я удаляю androidx и добавляю импорт, который должен быть там с V7.

import android.support.v7.widget.RecyclerView;

v7 становится красным, как и расширяет RecyclerView.ViewHolder. Если я попытаюсь нажать ALT + ENTER, чтобы импортировать его снова, появится androidx.

Так как это из практики в университете, он должен быть с android .support.v7.

Ответы [ 4 ]

1 голос
/ 10 февраля 2020

Если вы хотите использовать AndroidX в своем проекте

  1. Открыть Android Studio
  2. Нажмите Refactor
  3. Нажмите Переход на AndroidX
  4. Сначала будет предложено сделать zip . Нажмите ОК

Если вы хотите использовать AppCompat в своем проекте

  1. Открыть Android Studio
  2. Нажмите Refactor
  3. Нажмите Перейдите в AppCompat
  4. Сначала будет предложено сделать zip . Нажмите ОК
1 голос
/ 10 февраля 2020

ссылка https://developer.android.com/jetpack/androidx/releases/recyclerview#1 .1.0

 //Add this
  dependencies
  {
   implementation "androidx.recyclerview:recyclerview:1.1.0"
   // For control over item selection of both touch and mouse driven selection
   implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
  }

// in xml

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_defulterList"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
1 голос
/ 10 февраля 2020

Измените свой блок recyclerView на приведенный ниже код, если вы используете зависимость androidx для RecyclerView

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>
0 голосов
/ 22 апреля 2020

Я могу решить добавить эту зависимость в файл gradle приложения:

 implementation 'androidx.recyclerview:recyclerview:1.1.0'
...