RecyclerView虽然很强大,但是没有点击事件,美中不足!所以需要自己写点击事件啦。
Activity界面
public class MainActivity extends AppCompatActivity { private RecyclerView rv; private SwipeRefreshLayout sr; private List<String> data; private MyRecyclerViewAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView();//初始化view initData();//填充数据 //条目点击事件,调用适配器写的条目点击的方法 adapter.setOnItemClickListener(new OnItemClickListener() { @Override public void setOnItemClickListener(View view, int position) { Toast.makeText(MainActivity.this,"这是条目"+position,Toast.LENGTH_SHORT).show(); } }); //条目长按事件,调用适配器写的条目长按的方法 adapter.setOnLongClickListener(new OnLongClickListener() { @Override public void setOnLongClickListener(View view, int position) { //长按删除 data.remove(position); //刷新适配器 adapter.notifyDataSetChanged(); } }); sr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { initData(); sr.setRefreshing(false); } }); } private void initData() { data = new ArrayList<>(); for (int i = 0; i <20; i++) { data.add("这是条目"+i); } adapter = new MyRecyclerViewAdapter(MainActivity.this, data); rv.setAdapter(adapter);//设置适配器 } private void initView() { rv = (RecyclerView) findViewById(R.id.rv); sr = (SwipeRefreshLayout) findViewById(R.id.sr); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);//线性样式显示,和ListView显示的布局格式一致 GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);//九宫格样式显示,和GridView显示的布局格式一致 StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);//瀑布流样式显示 rv.setLayoutManager(linearLayoutManager);//LinearLayoutManager、gridLayoutManager、staggeredGridLayoutManager三者都可使用 } } activity_main布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/sr"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>适配器布局文件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:padding="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv"/> </LinearLayout>
适配器代码:public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> { private List<String> data=new ArrayList<>(); private Context context; //定义两个变量,接收条目点击和长按的对象 private OnItemClickListener mOnItemClickListener; private OnLongClickListener mOnLongClickListener; public MyRecyclerViewAdapter(Context context,List<String> data){ this.context=context; this.data=data; } //创建条目点击的方法,用变量接收一下接口对象 public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.mOnItemClickListener=onItemClickListener; } //创建条目长按的方法,用变量接收一下接口对象 public void setOnLongClickListener(OnLongClickListener onLongClickListener){ this.mOnLongClickListener=onLongClickListener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //创建自己的ViewHolder View view = LayoutInflater.from(context).inflate(R.layout.recycletview, null); MyViewHolder myViewHolder=new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { holder.tv.setText(data.get(position)); //holder的视图点击监听 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mOnItemClickListener.setOnItemClickListener(holder.tv,position);//控件和条目下标 } }); //holder的条目视图长按监听 holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { mOnLongClickListener.setOnLongClickListener(holder.tv,position); return false; } }); } @Override public int getItemCount() { return data!=null?data.size():0; } //创建ViewHolder class MyViewHolder extends RecyclerView.ViewHolder{ private TextView tv; public MyViewHolder(View itemView) { super(itemView); //使用itemView找到控件 tv = (TextView) itemView.findViewById(R.id.tv); } } }两个接口:public interface OnItemClickListener { void setOnItemClickListener(View view,int position); }public interface OnLongClickListener { void setOnLongClickListener(View view, int position); }代码就这么多,很好理解!