 
1.在build.gradle中加入依赖
dependencies {
……
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fruit_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/apple"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="apple"/>
</LinearLayout>
3.java代码
package com.cwj.recyclerview;
/**
* Created by 17551_000 on 2017/9/21.
*/
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
package com.cwj.recyclerview;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
/**
* Created by 17551_000 on 2017/9/21.
*/
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
ImageView fruitImage;
TextView fruitName;
//定义一个内部类ViewHolder
public ViewHolder(View view) {
super(view);
fruitView = view;
fruitImage = view.findViewById(R.id.fruit_image);
fruitName = view.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList){
mFruitList = fruitList;
}
/**
* 用于创建ViewHolder实例
* @param parent
* @param viewType
* @return
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
//RecyclerView的点击事件
final ViewHolder holder = new ViewHolder(view);
holder.fruitView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(view.getContext(), fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
holder.fruitImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(view.getContext(), fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
return holder;
}
/**
* 用于对RecyclerView子项的数据进行赋值
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
/**
* 告诉RecyclerView一共有多少子项,并返回数据源的长度
* @return
*/
@Override
public int getItemCount() {
return mFruitList.size();
}
}
package com.cwj.recyclerview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
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 = (RecyclerView) findViewById(R.id.recycler_view);
//LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//创建一个StaggeredGridLayoutManager实例
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
//RecyclerView的横向布局
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0;i < 2; i++){
Fruit apple = new Fruit("苹果",R.drawable.apple);
fruitList.add(apple);
Fruit banana = new Fruit("香蕉",R.drawable.banana);
fruitList.add(banana);
Fruit orange = new Fruit("橘子",R.drawable.orange);
fruitList.add(orange);
Fruit Watermelon = new Fruit("西瓜",R.drawable.watermelon);
fruitList.add(Watermelon);
Fruit pear = new Fruit("梨",R.drawable.pear);
fruitList.add(pear);
Fruit Grape = new Fruit("葡萄",R.drawable.grape);
fruitList.add(Grape);
Fruit Pineapple = new Fruit("菠萝",R.drawable.pineapple);
fruitList.add(Pineapple);
Fruit strawberry = new Fruit("草莓",R.drawable.strawberry);
fruitList.add(strawberry);
Fruit cherry = new Fruit("樱桃",R.drawable.cherry);
fruitList.add(cherry);
Fruit mango = new Fruit("芒果",R.drawable.mango);
fruitList.add(mango);
}
}
}
|