我没有导入Recycler View AndroidX库,但是,我正在使用它。为什么以及如何运作?

时间:2019-01-22 05:14:48

标签: android android-recyclerview androidx

build.gradle(app)

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.todolistapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.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.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // Room
    def room_version = "2.0.0"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // ViewModel and LiveData
    def lifecycle_version = "2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    // For Floating Action Button
    implementation 'com.google.android.material:material:1.0.0'
}

从我的依赖关系中可以看到,我没有导入回收视图Androidx库。

  

androidx.recyclerview:recyclerview:1.0.0

但是如下所示,我可以在布局(activity_main.xml)和MainActivity代码中轻松使用它。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewTasks"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="80dp"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:tint="@android:color/white"
        app:srcCompat="@android:drawable/ic_input_add"/>
</FrameLayout>

MainActivity.kt

import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), TaskAdapter.TaskViewCliskListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerViewTasks.layoutManager = LinearLayoutManager(this)
}

什么是这种行为的原因?

6 个答案:

答案 0 :(得分:3)

按照dependencies of the com.google.android.material:material:1.0.0

  

androidx.recyclerview:recyclerview:1.0.0

这意味着Material库已经具有对RecyclerView的传递性依赖,因此您无需自己手动添加它。

答案 1 :(得分:2)

  

我没有导入Recycler View AndroidX库,但我正在使用。为什么以及如何运作?

因为您已经添加了window.onload = function(){ let uList = document.querySelector('[name =tTable]'); fetchCall('https://jsonplaceholder.typicode.com/users', getUsers); sButton.addEventListener('click', fetchCall('https://jsonplaceholder.typicode.com/users', sBar), false); }; function sBar(getObject) { let sUser = getObject; let inputBar = document.getElementById("searchInput"); let text = inputBar.textContent; let textView = text.toUpperCase(); for (let i = 0; i < getObject.length; i++) { let uObject = sUser[i]; if (textView == uObject.name || textView == uObject.email) { let new_tTable = document.createElement('tbody'); uList.parentNode.replaceChild(new_tTable, uList) let row = uList.insertRow(); let idInput = document.createElement('td'); let nameInput = document.createElement('td'); let usernameInput = document.createElement('td'); let emailInput = document.createElement('td'); let cityInput = document.createElement('td'); let phoneInput = document.createElement('td'); let websiteInput = document.createElement('td'); let companyInput = document.createElement('td'); idInput.textContent = uObject.id; nameInput.textContent = uObject.name; usernameInput.textContent = uObject.username; emailInput.textContent = uObject.email; cityInput.textContent = uObject.address.city; phoneInput.textContent = uObject.phone; websiteInput.textContent = uObject.website; companyInput.textContent = uObject.company.name; row.appendChild(idInput); row.appendChild(nameInput); row.appendChild(usernameInput); row.appendChild(emailInput); row.appendChild(cityInput); row.appendChild(phoneInput); row.appendChild(websiteInput); row.appendChild(companyInput); } else{ alert("User not found"); } } } function fetchCall(url, fn){ fetch(url) .then(function(response){ return response.json(); }) .then(function(endPoint){ fn(endPoint); }) .catch(function(error){ console.error(error); }) } function getUsers(getObject) { let user = getObject; for (let i = 0; i < getObject.length; i++) { let userObject = user[i]; let row = uList.insertRow(); let idInput = document.createElement('td'); let nameInput = document.createElement('td'); let usernameInput = document.createElement('td'); let emailInput = document.createElement('td'); let cityInput = document.createElement('td'); let phoneInput = document.createElement('td'); let websiteInput = document.createElement('td'); let companyInput = document.createElement('td'); idInput.textContent = userObject.id; nameInput.textContent = userObject.name; usernameInput.textContent = userObject.username; emailInput.textContent = userObject.email; cityInput.textContent = userObject.address.city; phoneInput.textContent = userObject.phone; websiteInput.textContent = userObject.website; companyInput.textContent = userObject.company.name; row.appendChild(idInput); row.appendChild(nameInput); row.appendChild(usernameInput); row.appendChild(emailInput); row.appendChild(cityInput); row.appendChild(phoneInput); row.appendChild(websiteInput); row.appendChild(companyInput); } } 的依赖项

如果您已经添加了com.google.android.material:material:1.0.0,则无需添加androidx.recyclerview:recyclerview:1.0.0

答案 2 :(得分:2)

如果您的问题是要使用与material依赖项中的版本不同的版本。

每次尝试手动添加recyclerview体面时,我都尝试使用androidx.recyclerview:recyclerview的Alpha版本,因此代码在构建时运行良好。 但是该应用程序一直崩溃,因为它无法在运行时从recyclerview的Alpha版本中找到我使用的新类。

我尝试使用material的最新Alpha版本,但是它也依赖于recyclerview的最新稳定版本..因此它无济于事。

为解决这个问题,我从recyclerview中排除了material的体面,并这样添加了recyclerview的体面。

implementation("com.google.android.material:material:1.1.0") {
    exclude(group = "androidx.recyclerview", module = "recyclerview")
}
implementation("androidx.recyclerview:recyclerview:1.2.0-alpha04")

答案 3 :(得分:0)

我已经尝试和测试。com.google.android.material:material:1.0.0也包括RecyclerView。可能是为了使材料设计看起来像MaterialButton

答案 4 :(得分:0)

com.google.android.material:material:1.0.0包括 RecyclerView 组件,因此,如果您将com.google.android.material:material:1.0.0添加到应用程序依赖项中,则无需在外部添加它,而无需添加{{ 1}}到您的依赖项

答案 5 :(得分:-1)

dependencies{

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

}

如果您已经添加

com.google.android.material:material:1.0.0

在您的依赖项中,则不需要recyclerview依赖项。它已经添加了材料