[Android新手学习笔记25]-RecylerView简单应用

来源:互联网 发布:网络直播软件下载 编辑:程序博客网 时间:2024/06/06 01:41

使用前需要在app/build.gradle文件中添加如下内容:

compile 'com.android.support:recyclerview-v7:25.1.1'

注意25.1.1为版本,有要求,和上面版本填一样。

  1. dependencies {
  2.    compile fileTree(dir: 'libs', include: ['*.jar'])
  3.    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  4.        exclude group: 'com.android.support', module: 'support-annotations'
  5.    })
  6.    compile 'com.android.support:appcompat-v7:25.1.1'
  7.    compile 'com.android.support:recyclerview-v7:25.1.1'
  8.    testCompile 'junit:junit:4.12'
  9. }

修改app/build.gradle文件时,会提示:

Gradle files have changed since last project sync. A project sync may be necessary for the IDE to work properly. Sync Now

点击Sync Now即可。

创建布局文件:fruit_item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="wrap_content">
  7.    <ImageView
  8.        android:id="@+id/fruit_image"
  9.        android:layout_width="60dp"
  10.        android:layout_height="60dp" />
  11.    <TextView
  12.        android:id="@+id/fruit_name"
  13.        android:textSize="30sp"
  14.        android:gravity="center_vertical"
  15.        android:layout_width="0dp"
  16.        android:layout_weight="1"
  17.        android:layout_marginLeft="10dp"
  18.        android:layout_height="60dp" />
  19. </LinearLayout>

创建Fruit类:

  1. public class Fruit {
  2.    private String name;
  3.    private int imageId;
  4.    public Fruit(String name, int imageId) {
  5.        this.name = name;
  6.        this.imageId = imageId;
  7.    }
  8.    public String getName() {
  9.        return this.name;
  10.    }
  11.    public int getImageId() {
  12.        return this.imageId;
  13.    }
  14. }

创建FruitAdapter类,继承RecyclerView.Adapter<FruitAdapter.ViewHolder>:

  1. public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
  2.    private List<Fruit> mFruitList;
  3.    static class ViewHolder extends RecyclerView.ViewHolder {
  4.        ImageView fruitImage;
  5.        TextView fruitName;
  6.        public ViewHolder(View view) {
  7.            super(view);
  8.            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
  9.            fruitName = (TextView) view.findViewById(R.id.fruit_name);
  10.        }
  11.    }
  12.    public FruitAdapter(List<Fruit> fruitList) {
  13.        mFruitList = fruitList;
  14.    }
  15.    @Override
  16.    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  17.        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
  18.        ViewHolder holder =  new ViewHolder(view);
  19.        return holder;
  20.    }
  21.    @Override
  22.    public void onBindViewHolder(ViewHolder holder, int position) {
  23.        Fruit fruit = mFruitList.get(position);
  24.        holder.fruitImage.setImageResource(fruit.getImageId());
  25.        holder.fruitName.setText(fruit.getName());
  26.    }
  27.    @Override
  28.    public int getItemCount() {
  29.        return mFruitList.size();
  30.    }
  31. }

在MainActivity布局中引用fruit_item:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <android.support.v7.widget.RecyclerView
  8.        android:id="@+id/recycler_view"
  9.        android:layout_width="match_parent"
  10.        android:layout_height="match_parent" />
  11. </LinearLayout>

在MainActivity类中使用:

  1. public class MainActivity extends AppCompatActivity {
  2.    private List<Fruit> fruitList = new ArrayList<>();
  3.    @Override
  4.    protected void onCreate(Bundle savedInstanceState) {
  5.        super.onCreate(savedInstanceState);
  6.        setContentView(R.layout.activity_main);
  7.        initFruits();
  8.        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  9.        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  10.        recyclerView.setLayoutManager(layoutManager);
  11.        FruitAdapter adapter = new FruitAdapter(fruitList);
  12.        recyclerView.setAdapter(adapter);
  13.    }
  14.    private void initFruits() {
  15.        Fruit strawberry = new Fruit("草莓", R.drawable.strawberry);
  16.        fruitList.add(strawberry);
  17.        Fruit pear = new Fruit("梨", R.drawable.pear);
  18.        fruitList.add(pear);
  19.        Fruit mango = new Fruit("芒果", R.drawable.mango);
  20.        fruitList.add(mango);
  21.        Fruit apple = new Fruit("苹果", R.drawable.apple);
  22.        fruitList.add(apple);
  23.        Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon);
  24.        fruitList.add(watermelon);
  25.        Fruit banana = new Fruit("香蕉", R.drawable.banana);
  26.        fruitList.add(banana);
  27.        Fruit cherry = new Fruit("樱桃", R.drawable.cherry);
  28.        fruitList.add(cherry);
  29.    }
  30. }


创建横向滚动:

改变fruit_itme:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="60dp"
  6.    android:layout_height="wrap_content">
  7.    <ImageView
  8.        android:id="@+id/fruit_image"
  9.        android:layout_gravity="center_horizontal"
  10.        android:layout_width="60dp"
  11.        android:layout_height="60dp" />
  12.    <TextView
  13.        android:id="@+id/fruit_name"
  14.        android:textSize="20sp"
  15.        android:layout_gravity="center_horizontal"
  16.        android:gravity="center"
  17.        android:layout_width="60dp"
  18.        android:layout_marginTop="10dp"
  19.        android:layout_height="60dp" />
  20. </LinearLayout>

修改LinearLayoutManager:

  1. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  2. layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);


创建瀑布流:

这里创建的横向垂直瀑布流,高度不一致效果明显。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_margin="5dp"
  6.    android:layout_width="match_parent"
  7.    android:layout_height="wrap_content">
  8.    <ImageView
  9.        android:id="@+id/fruit_image"
  10.        android:layout_gravity="center_horizontal"
  11.        android:layout_width="wrap_content"
  12.        android:layout_height="wrap_content" />
  13.    <TextView
  14.        android:id="@+id/fruit_name"
  15.        android:textSize="20sp"
  16.        android:layout_gravity="left"
  17.        android:gravity="center"
  18.        android:layout_width="wrap_content"
  19.        android:layout_marginTop="10dp"
  20.        android:layout_height="wrap_content" />
  21. </LinearLayout>

  1. public class MainActivity extends AppCompatActivity {
  2.    private List<Fruit> fruitList = new ArrayList<>();
  3.    @Override
  4.    protected void onCreate(Bundle savedInstanceState) {
  5.        super.onCreate(savedInstanceState);
  6.        setContentView(R.layout.activity_main);
  7.        initFruits();
  8.        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  9.        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
  10.        recyclerView.setLayoutManager(layoutManager);
  11.        FruitAdapter adapter = new FruitAdapter(fruitList);
  12.        recyclerView.setAdapter(adapter);
  13.    }
  14.    private void initFruits() {
  15.        Fruit strawberry = new Fruit(getRandomLengthName("草莓"), R.drawable.strawberry);
  16.        fruitList.add(strawberry);
  17.        Fruit pear = new Fruit(getRandomLengthName("梨"), R.drawable.pear);
  18.        fruitList.add(pear);
  19.        Fruit mango = new Fruit(getRandomLengthName("芒果"), R.drawable.mango);
  20.        fruitList.add(mango);
  21.        Fruit apple = new Fruit(getRandomLengthName("苹果"), R.drawable.apple);
  22.        fruitList.add(apple);
  23.        Fruit watermelon = new Fruit(getRandomLengthName("西瓜"), R.drawable.watermelon);
  24.        fruitList.add(watermelon);
  25.        Fruit banana = new Fruit(getRandomLengthName("香蕉"), R.drawable.banana);
  26.        fruitList.add(banana);
  27.        Fruit cherry = new Fruit(getRandomLengthName("樱桃"), R.drawable.cherry);
  28.        fruitList.add(cherry);
  29.    }
  30.    private String getRandomLengthName(String name) {
  31.        Random random = new Random();
  32.        int length = random.nextInt(100) + 1;
  33.        StringBuilder builder = new StringBuilder();
  34.        for (int i = 0; i < length; ++i) {
  35.            builder.append(name);
  36.        }
  37.        return builder.toString();
  38.    }
  39. }

点击事件:

  1. public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
  2.    private List<Fruit> mFruitList;
  3.    static class ViewHolder extends RecyclerView.ViewHolder {
  4.        View fruitView;
  5.        ImageView fruitImage;
  6.        TextView fruitName;
  7.        public ViewHolder(View view) {
  8.            super(view);
  9.            fruitView = view;
  10.            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
  11.            fruitName = (TextView) view.findViewById(R.id.fruit_name);
  12.        }
  13.    }
  14.    public FruitAdapter(List<Fruit> fruitList) {
  15.        mFruitList = fruitList;
  16.    }
  17.    @Override
  18.    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  19.        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
  20.        final ViewHolder holder = new ViewHolder(view);
  21.        holder.fruitView.setOnClickListener(new View.OnClickListener() {
  22.            @Override
  23.            public void onClick(View v) {
  24.                int position = holder.getAdapterPosition();
  25.                Fruit fruit = mFruitList.get(position);
  26.                Toast.makeText(v.getContext(), "你点击了" + fruit.getName() +"的名字", Toast.LENGTH_SHORT).show();
  27.            }
  28.        });
  29.        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
  30.            @Override
  31.            public void onClick(View v) {
  32.                int position = holder.getAdapterPosition();
  33.                Fruit fruit = mFruitList.get(position);
  34.                Toast.makeText(v.getContext(), "你点击了" + fruit.getName() + "的图片", Toast.LENGTH_SHORT).show();
  35.            }
  36.        });
  37.        return holder;
  38.    }
  39.    @Override
  40.    public void onBindViewHolder(ViewHolder holder, int position) {
  41.        Fruit fruit = mFruitList.get(position);
  42.        holder.fruitImage.setImageResource(fruit.getImageId());
  43.        holder.fruitName.setText(fruit.getName());
  44.    }
  45.    @Override
  46.    public int getItemCount() {
  47.        return mFruitList.size();
  48.    }
  49. }

0 0
原创粉丝点击