在<第一行代码中>是如此设置的 他在dependencies闭包中添加了如下内容
dependencies{ compile fileTree(dir: 'libs', include:['*.jar']) compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:recyclerview-v7:24.2.1' <---添加了这行 testCompile 'junit:junit:4.12' }但由于Android Studio不断的更新 已经弃用了support库和compile语句 所以要达到同样的效果 我们需要这样操作一下在Android Studio最上边点击Refactor --> Migrate to AndroidX --> 把back to project as ZIP file按钮取消 然后点击Migrate --> do factor 之后 代码会变成这样
dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.recyclerview:recyclerview:1.0.0' <--- 这行变成这样 testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }然后Sync Now同步一下就大工搞成啦
因为RecyclerView不是内置在系统SDK中的 所以需要把完整的包路径写出来. <第一行代码中> 使用的还是原先support库的引用方法 我们在之前把他修改成了AndroidX库 所以这里边的完整包路径要写成:androidx.recyclerview.widget.RecyclerView
首先准备好Fruit水果类 和 fruit_item.xml布局
一切都准备好了 我们修改MainActivity中的代码就可以使用RecyclerView啦
package com.example.recyclerviewtest; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); // 通过设置layoutManager将布局从默认的 纵向 变为 横向 layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layoutManager); FruitAdapter fruitAdapter = new FruitAdapter(fruitList); recyclerView.setAdapter(fruitAdapter); } private void initFruits() { for (int i = 0; i < 2; i++) { Fruit apple = new Fruit("Apple", R.drawable.apple_pic); fruitList.add(apple); Fruit banana = new Fruit("Banana", R.drawable.banana_pic); fruitList.add(banana); Fruit orange = new Fruit("Orange", R.drawable.orange_pic); fruitList.add(orange); Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic); fruitList.add(watermelon); Fruit pear = new Fruit("Pear", R.drawable.pear_pic); fruitList.add(pear); Fruit grape = new Fruit("Grape", R.drawable.grape_pic); fruitList.add(grape); Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic); fruitList.add(pineapple); Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic); fruitList.add(strawberry); Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic); fruitList.add(cherry); Fruit mango = new Fruit("Mango", R.drawable.mango_pic); fruitList.add(mango); } } }其实我是初学者哈 有很多问题还没有搞清楚就冒然写下这篇博客 如果程序\代码\注释 出现任何问题可以和我交流 一起进步(▽)
顺便一提 代码中的图片是<第一行代码>原书作者提供的如果大家需要可以去 https://github.com/guolindev/booksource 下载