步骤:1.在adapter中新建一个接口,接口在adapter中有一个实例,并且提供set方法
2.在createViewHolder的时候给itemview设置点击事件this(Adapter实现了View.OnClickListener 接口).
3.在onBindViewHolder的时候给itemview设置tag,目的是让itemview带着自己的位置,被复用后会设置新的。
4.重写Adapter的OnClickListener方法。
if (ItemclickListener != null) {
ItemclickListener.onitemclick(Integer.parseInt(view.getTag().toString()));
}
5.在Activity中去使用。给adapter.setItemclickListener(匿名内部类的方式)。
也可以Activity实现OnItemClick接口,重写onitemclick方法。
RecycleViewAdapter.java
package com.example.adapter;
import android.content.Context;
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 com.example.entity.CarCompany;
import com.example.jump.R;
import java.util.List;
/**
* Created by Administrator on 2017/2/20.
*/
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyHolder> implements View.OnClickListener{
private Context mContext;
private List<CarCompany> list;
private LayoutInflater inflater;
private OnItemClick ItemclickListener=null;//给一个实例并且提供下边set方法
public void setItemClickListener(OnItemClick itemclicklistener){
ItemclickListener=itemclicklistener;
}
//有参的构造函数,并实例化布局加载器
public RecycleViewAdapter(Context mContext, List<CarCompany> list) {
this.mContext = mContext;
this.list = list;
inflater=LayoutInflater.from(mContext);
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//第二个参数是容器 第三个参数:true使用父布局的布局参数,false使用自己的布局参数
View view=inflater.inflate(R.layout.activity_item_list,parent,false);
MyHolder holder=new MyHolder(view);
holder.itemView.setOnClickListener(this);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
//获取对应位置的数据, 不能再此重新实例化CarCompany
CarCompany company=list.get(position);
holder.tv_name.setText(company.getCompany());
holder.iv_img.setImageResource(company.getImg());
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return list.size();
}
/**
*
* @param v
*/
@Override
public void onClick(View v) {
if(ItemclickListener!=null){
ItemclickListener.onitemclick(Integer.parseInt(v.getTag().toString()));
}
}
//创建内部类继承于RecycleView.ViewHolder实例化自定义布局中的属性
class MyHolder extends RecyclerView.ViewHolder{//内部类要写在主函数里边
private ImageView iv_img;
private TextView tv_name;
public MyHolder(View itemView) {
super(itemView);
iv_img= (ImageView) itemView.findViewById(R.id.iv_img);
tv_name= (TextView) itemView.findViewById(R.id.name);
}
}
//有方法 我们需要知道点击的是第几个item.
// 自定义接口,同时传入position参数设置item布局点击的是哪一个item
public interface OnItemClick{
void onitemclick(int position);
}
}
RecycleViewAcvitity.java
package com.example.jump;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.adapter.RecycleViewAdapter;
import com.example.entity.CarCompany;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewActivity extends AppCompatActivity {
private RecyclerView rv;
private String[] names = new String[]{"长安", "沃尔沃", "大众", "宾利", "奔驰"};
private int[] ids = new int[]{R.drawable.changan, R.drawable.vol, R.drawable.das, R.drawable.bl, R.drawable.benz};
private List<CarCompany> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_view);
rv= (RecyclerView) findViewById(R.id.rv_view);//一定要实例化对应layout中设置的RecycleView
intiRV();
}
private void intiRV(){
list=new ArrayList<>();
for (int i = 0; i <names.length ; i++) {
CarCompany company=new CarCompany();
company.setCompany(names[i]);
company.setImg(ids[i]);
list.add(company);
}
RecycleViewAdapter adapter=new RecycleViewAdapter(this,list);
//设置布局管理器
rv.setLayoutManager(new LinearLayoutManager(this));//像ListView一样展示
rv.setAdapter(adapter);
}
}
recycleview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jump.RecycleViewActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>