1 package com.example.recyclerviewdemo; 2 3 import android.support.v7.widget.RecyclerView; 4 import android.util.Log; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener{ 11 private String[] datas; 12 public MyAdapter(String[] datas) { 13 this.datas = datas; 14 } 15 private OnRecyclerViewItemClickListener mOnItemClickListener = null; 16 17 //define interface 18 public static interface OnRecyclerViewItemClickListener { 19 void onItemClick(View view , String data); 20 } 21 22 @Override 23 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 24 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); 25 ViewHolder vh = new ViewHolder(view); 26 //将创建的View注册点击事件 27 view.setOnClickListener(this); 28 return vh; 29 } 30 31 @Override 32 public void onBindViewHolder(ViewHolder viewHolder, int position) { 33 viewHolder.mTextView.setText(datas[position]); 34 //将数据保存在itemView的Tag中,以便点击时进行获取 35 viewHolder.itemView.setTag(datas[position]); 36 } 37 38 @Override 39 public void onClick(View v) { 40 if (mOnItemClickListener != null) { 41 //注意这里使用getTag方法获取数据 42 mOnItemClickListener.onItemClick(v,(String)v.getTag()); 43 } 44 } 45 46 public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { 47 this.mOnItemClickListener = listener; 48 } 49 50 51 //获取数据的数量 52 @Override 53 public int getItemCount() { 54 return datas.length; 55 } 56 //自定义的ViewHolder,持有每个Item的的所有界面元素 57 public static class ViewHolder extends RecyclerView.ViewHolder { 58 public TextView mTextView; 59 public ViewHolder(View view){ 60 super(view); 61 mTextView = (TextView) view.findViewById(R.id.text); 62 } 63 } 64 }
adapter
1 import com.example.recyclerviewdemo.MyAdapter.OnRecyclerViewItemClickListener; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.support.v7.widget.LinearLayoutManager; 5 import android.support.v7.widget.RecyclerView; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View; 10 import android.widget.Toast; 11 12 public class MainActivity extends ActionBarActivity { 13 private RecyclerView mRecyclerView; 14 private LinearLayoutManager mLayoutManager; 15 private MyAdapter mAdapter; 16 private String[] data= new String[] {"aa","bb", "aa","bb", "aa","bb", "aa","bb", "aa","bb","aa","bb", "aa","bb", "aa","bb", "aa","bb", "aa","bb" }; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view); 23 //创建默认的线性LayoutManager 24 mLayoutManager = new LinearLayoutManager(this); 25 mRecyclerView.setLayoutManager(mLayoutManager); 26 //如果可以确定每个item的高度是固定的,设置这个选项可以提高性能 27 mRecyclerView.setHasFixedSize(true); 28 //创建并设置Adapter 29 mAdapter = new MyAdapter(data); 30 mRecyclerView.setAdapter(mAdapter); 31 mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener(){ 32 @Override 33 public void onItemClick(View view , String data){ 34 Toast.makeText(MainActivity.this, data, 600).show(); 35 } 36 }); 37 } 38 39 @Override 40 public boolean onCreateOptionsMenu(Menu menu) { 41 // Inflate the menu; this adds items to the action bar if it is present. 42 getMenuInflater().inflate(R.menu.main, menu); 43 return true; 44 } 45 46 @Override 47 public boolean onOptionsItemSelected(MenuItem item) { 48 // Handle action bar item clicks here. The action bar will 49 // automatically handle clicks on the Home/Up button, so long 50 // as you specify a parent activity in AndroidManifest.xml. 51 int id = item.getItemId(); 52 if (id == R.id.action_settings) { 53 return true; 54 } 55 return super.onOptionsItemSelected(item); 56 } 57 58 59 }
activity
时间: 2024-10-10 21:41:08