首先Xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.longfei.recyclerviewdemo.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/RecyclerView" /> </LinearLayout>
MainActivity主代码package com.example.longfei.recyclerviewdemo; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView RecyclerView1; private List<Bean> data = new ArrayList<Bean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //首先添加数据 for (int i = 0; i < 10; i++) { Bean b = new Bean(R.mipmap.a2,"我是姓名"+i,"我是年龄"+i); data.add(b); } System.out.println(data.size()); initView(); } private void initView() { //找控件 RecyclerView1 = (RecyclerView) findViewById(R.id.RecyclerView); //选择是线性还是网格 //RecyclerView1.setLayoutManager(new LinearLayoutManager(this)); RecyclerView1.setLayoutManager(new GridLayoutManager(this,3)); //适配器 MyAdapter myAdapter = new MyAdapter(this, data); RecyclerView1.setAdapter(myAdapter); //RecyclerView的点击事件 RecyclerView1.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, RecyclerView1, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this,"我是点击事件",Toast.LENGTH_LONG).show(); } @Override public void onItemLongClick(View view, int position) { Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } })); } }MyAdapter适配器package com.example.longfei.recyclerviewdemo; 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 java.util.List; /** * Created by longfei on 2016/10/11. */ class MyAdapter extends RecyclerView.Adapter { //传来的值 private MainActivity mMainActivity; private List<Bean> data; //有参构造 public MyAdapter(MainActivity mainActivity, List<Bean> data) { mMainActivity = mainActivity; this.data = data; } //自定义一个ViewHodler 在这里面找控件 class ViewHodler extends RecyclerView.ViewHolder { private ImageView mImageView; private TextView mName; private TextView mAge; public ViewHodler(View itemView) { super(itemView); mImageView = (ImageView) itemView.findViewById(R.id.image); mName = (TextView) itemView.findViewById(R.id.name); mAge = (TextView) itemView.findViewById(R.id.age); } } //让布局与我们的ViewHolder绑定在一起 @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHodler(LayoutInflater.from(mMainActivity).inflate(R.layout.item,null)); } //更新数据 @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { //首先得到holdler ViewHodler h = (ViewHodler) holder; Bean bean = data.get(position); h.mName.setText(bean.getName()); h.mAge.setText(bean.getAge()); h.mImageView.setImageResource(bean.getImages()); // // h.mImageView.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // Toast.makeText(mMainActivity,"的三大大厦大厦的",Toast.LENGTH_LONG).show(); // } // }); } @Override public int getItemCount() { return data.size(); } }
自定义点击事件package com.example.longfei.recyclerviewdemo; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { public interface OnItemClickListener { void onItemClick(View view, int position); void onItemLongClick(View view, int position); } private OnItemClickListener mListener; private GestureDetector mGestureDetector; public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { mListener = listener; mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } @Override public void onLongPress(MotionEvent e) { View childView = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null) { mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView)); } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { View childView = view.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { mListener.onItemClick(childView, view.getChildAdapterPosition(childView)); } return false; } @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } }
Bean包package com.example.longfei.recyclerviewdemo; /** * Created by longfei on 2016/10/11. */ public class Bean { private int images; private String name; private String age; public int getImages() { return images; } public void setImages(int images) { this.images = images; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Bean(int a2, String s) { } public Bean(int images, String name, String age) { this.images = images; this.name = name; this.age = age; } }