【问题标题】:Recyclerview not being displayed when using API 28使用 API 28 时不显示 Recyclerview
【发布时间】:2019-08-12 23:26:04
【问题描述】:

我有一个 android 模块(我自己的库),我用它从 web 获取数据并使用 recyclerview 将其显示为列表。

数据包含我通过 Picasso lib 加载的图像的路径。 recyclerview 列表中的每一项都是一个 ImageView。

我正在使用 androidx 依赖项。应用程序安装在 API 24 模拟器中时可按预期工作。但是,当应用程序安装在 API 28 模拟器中时,数据不会显示。

我已经针对 API 28 进行了调试,并且按预期获取了数据。

这里是列表作为“it”从网络返回的地方。调试时,列表中符合预期的 20 项。

viewModel.trendingsLiveData.observe(this, Observer {
        Log.d("trendingtitle", "trendingsLiveData.observe() called")
        allTrendingAdapter!!.loadItems(it)
        allTrendingAdapter!!.notifyDataSetChanged()

        shimmerViewContainer?.stopShimmer()//stopShimmerAnimation()
        shimmerViewContainer?.setVisibility(View.INVISIBLE)
    })

用于在适配器中加载图像的行(也使用断点检查 - 按预期工作)

Picasso.get().load("http://image.tmdb.org/t/p/w342${results!!.get(position).poster_path}").into(holder.poster);

Build.gradle(我的 Android 模块)

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"


defaultConfig {
    minSdkVersion 21
    targetSdkVersion 28
    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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


def moshiVersion="1.8.0"
def retrofit2_version = "2.5.0"
def okhttp3_version = "3.12.0"
def kotlinCoroutineVersion = "1.0.1"
def picassoVersion = "2.71828"

//ViewModel Scope
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01'

//recyclerview
implementation 'androidx.recyclerview:recyclerview:1.0.0'

implementation 'com.google.android.material:material:1.0.0'

//Moshi
implementation "com.squareup.moshi:moshi-kotlin:$moshiVersion"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"

//Retrofit2
implementation "com.squareup.retrofit2:retrofit:$retrofit2_version"
implementation "com.squareup.retrofit2:converter-moshi:$retrofit2_version"
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2"
//Because of an error with JAR version
implementation "org.jetbrains.kotlin:kotlin-reflect:'1.3.31'"

//Okhttp3
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'

//Picasso
implementation ("com.squareup.picasso:picasso:$picassoVersion"){
    exclude group: "com.android.support"
}

implementation 'com.facebook.shimmer:shimmer:0.4.0'
}

Build.gradle(模块应用)

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 21
        targetSdkVersion 28
        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"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation project(path: ':posterlistlib')
}

Build.gradle(项目)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.41'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

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

为什么在 API 24 中显示图像列表,但在 API 28 中却没有?

【问题讨论】:

    标签: android android-studio android-recyclerview


    【解决方案1】:

    对于简单的修复,请在AndroidManifest.xml中使用以下代码

     <application
            android:usesCleartextTraffic="true"
       ...>
    

    从 API 28 开始,默认情况下带有 HTTP 的 URL 将不起作用,有关此主题的更多详细信息,请查看以下链接

    https://developer.android.com/about/versions/pie/android-9.0-changes-28

    【讨论】:

    • 请注意,Play Protect 等各种应用扫描程序可能会将您的应用标记为有害/不安全​​。如需更好的答案,请参阅stackoverflow.com/a/57493322/3999808
    • 我刚刚更改了正确答案,因为它更安全。但两者都允许应用显示所需的数据。
    【解决方案2】:

    要修复无法加载的图像,请将http://image.tmdb.org 替换为https://image.tmdb.org

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      相关资源
      最近更新 更多