RecyclerView可以说是一个增强版的ListView
使用他之前我们需要在项目的build.gradle中添加相应的依赖库才可以
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.android.support:recyclerview-v7:24.2.1' }
然后我们也需要准备一个实体类,和一个行布局,这里以一个水果类举例
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 void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/fruit_image"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/fruit_name"
android:text="苹果"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"/>
</LinearLayout>
然后我们需要准备一个适配器
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
public FruitAdapter(List<Fruit> FruitList){
mFruitList=FruitList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
final ViewHolder holder=new ViewHolder(view);
holder.fruitView.setOnClickListener(new View.OnClickListener() {//点击事件
@Override
public void onClick(View v) {
int position=holder.getAdapterPosition();//获取用户点击的position
Fruit fruit=mFruitList.get(position);
Toast.makeText(v.getContext(),fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
holder.fruitImage.setOnClickListener(new View.OnClickListener() {//点击事件
@Override
public void onClick(View v) {
int position=holder.getAdapterPosition();//获取用户点击的position
Fruit fruit=mFruitList.get(position);
Toast.makeText(v.getContext(),fruit.getImageId(),Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit=mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount() {
return mFruitList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;//fruitView变量来保存子项最外层布局的实例
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View itemView) {
super(itemView);
fruitView=itemView;
fruitImage= (ImageView) itemView.findViewById(R.id.fruit_image);
fruitName= (TextView) itemView.findViewById(R.id.fruit_name);
}
}
}
然后在mainActivity中运用
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);//指定RecyclerView的布局方式
// layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);//设置布局的排列方向
/*
第一个参数:指定布局的列数,传入3,则表示会把布局分为3列
第二个参数:用于指定布局的排列方向
*/
StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
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.xiaoniao);
fruitList.add(apple);
Fruit banana=new Fruit("香蕉",R.drawable.xiaoniao);
fruitList.add(banana);
Fruit orange=new Fruit("橘子",R.drawable.xiaoniao);
fruitList.add(orange);
Fruit watermelon=new Fruit("西瓜",R.drawable.xiaoniao);
fruitList.add(watermelon);
Fruit pear=new Fruit("梨",R.drawable.xiaoniao);
fruitList.add(pear);
Fruit strawberry=new Fruit("草莓",R.drawable.xiaoniao);
fruitList.add(strawberry);
}
}
}