@先看看效果是不是自己想要的吧@
&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>item及item内部控件点击事件不懂的可以先点击查看<&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
使用步骤&#xff1a;
第一步&#xff1a;添加RecyclerView适配器依赖
compile &#39;com.android.support:recyclerview-v7:27.1.1&#39;
第二步&#xff1a;布局文件里使用RecyclerView
第三步&#xff1a;创建两个item子布局填充item内容
itemone.xml
itemtwo.xml
第四步&#xff1a;定义一个实体类Data&#xff0c;作为Recyclerview的适配类型
package com.ied.recyclerview.entity;public class Data {public static final int TYPE_ONE &#61; 1;//类型1public static final int TYPE_TWO &#61; 2;//类型2public int type;//item内容 类型public int icon;public String username;public String message;public Data(int type, int icon, String username, String message) {this.type &#61; type;this.icon &#61; icon;this.username &#61; username;this.message &#61; message;}public static int getTypeOne() {return TYPE_ONE;}public static int getTypeTwo() {return TYPE_TWO;}public int getType() {return type;}public void setType(int type) {this.type &#61; type;}public int getIcon() {return icon;}public void setIcon(int icon) {this.icon &#61; icon;}public String getUsername() {return username;}public void setUsername(String username) {this.username &#61; username;}public String getMessage() {return message;}public void setMessage(String message) {this.message &#61; message;}
}
第五步&#xff1a;创建自定义MyRecyclerViewAdapter适配器继承RecyclerView.Adapter
package com.ied.recyclerview.adapter;import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.ied.recyclerview.R;
import com.ied.recyclerview.entity.Data;
import java.util.List;public class MyRecyclerViewAdapter extends RecyclerView.Adapter{private List list;//数据源private Context context;//上下文public MyRecyclerViewAdapter(List list, Context context) {this.list &#61; list;this.context &#61; context;}&#64;NonNull&#64;Overridepublic RecyclerView.ViewHolder onCreateViewHolder(&#64;NonNull ViewGroup parent, int viewType) {//选择类型switch (viewType){case Data.TYPE_ONE:View view &#61; LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);return new OneViewHolder(view);case Data.TYPE_TWO:view &#61; LayoutInflater.from(parent.getContext()).inflate(R.layout.itemtwo,parent,false);return new TwoViewHolder(view);}return null;}//绑定&#64;Overridepublic void onBindViewHolder(&#64;NonNull RecyclerView.ViewHolder holder, int position) {}//有多少个item&#xff1f;&#64;Overridepublic int getItemCount() {return list.size();}//item类型&#64;Overridepublic int getItemViewType(int position) {return list.get(position).type;}//&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;以下为item中的button控件点击事件处理&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;//第一步&#xff1a;自定义一个回调接口来实现Click和LongClick事件public interface OnItemClickListener {void onItemClick(View v, int position);void onItemLongClick(View v);}public OnItemClickListener mOnItemClickListener;//第二步&#xff1a;声明自定义的接口//第三步&#xff1a;定义方法并暴露给外面的调用者public void setOnItemClickListener(OnItemClickListener listener) {this.mOnItemClickListener &#61; listener;}
//&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;&#43;//第一个item类型class OneViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {private ImageView ivIcon;private TextView tvUsername, tvMessage;private Button btnAgree, btnRefuse;//private List list;public OneViewHolder(View itemView) {super(itemView);ivIcon &#61; itemView.findViewById(R.id.iv_icon);tvUsername &#61; itemView.findViewById(R.id.tv_username);tvMessage &#61; itemView.findViewById(R.id.tv_message);btnAgree &#61; itemView.findViewById(R.id.btn_agree);btnRefuse &#61; itemView.findViewById(R.id.btn_refuse);// 为item及item内部控件添加点击事件itemView.setOnClickListener(this);btnAgree.setOnClickListener(this);btnRefuse.setOnClickListener(this);}&#64;Overridepublic void onClick(View v) {if (mOnItemClickListener !&#61; null) {mOnItemClickListener.onItemClick(v, getAdapterPosition());}}}//第二个item类型class TwoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {private ImageView ivIcon;private TextView tvUsername;public TwoViewHolder(View itemView) {super(itemView);ivIcon &#61; itemView.findViewById(R.id.iv_icon);tvUsername &#61; itemView.findViewById(R.id.tv_username);// 为item添加点击事件itemView.setOnClickListener(this);}&#64;Overridepublic void onClick(View v) {if (mOnItemClickListener !&#61; null) {mOnItemClickListener.onItemClick(v, getAdapterPosition());}}}}
第六步&#xff1a;在MainActivity中编写&#xff0c;初始化数据&#xff0c;设置适配器
package com.ied.recyclerview;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.ied.recyclerview.adapter.MyRecyclerViewAdapter;
import com.ied.recyclerview.entity.Data;
import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private RecyclerView rvRecyclerView;private MyRecyclerViewAdapter adapter;private List list;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();rvRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));//控制布局为LinearLayout或者是GridView或者是瀑布流布局adapter &#61; new MyRecyclerViewAdapter(list,this);rvRecyclerView.setAdapter(adapter);// 设置item及item中控件的点击事件adapter.setOnItemClickListener(MyItemClickListener);}private void initView() {rvRecyclerView &#61; (RecyclerView) findViewById(R.id.rv_recyclerView);}private void initData() {list &#61; new ArrayList<>();list.add(new Data(1,R.mipmap.ic_launcher, "username", "让我们成为好友吧&#xff01;"));list.add(new Data(2,R.mipmap.ic_launcher, "username", "让我们成为好友吧&#xff01;"));list.add(new Data(1,R.mipmap.ic_launcher, "username", "让我们成为好友吧&#xff01;"));list.add(new Data(2,R.mipmap.ic_launcher, "username", "让我们成为好友吧&#xff01;"));list.add(new Data(1,R.mipmap.ic_launcher, "username", "让我们成为好友吧&#xff01;"));}/*** item&#xff0b;item里的控件点击监听事件*/private MyRecyclerViewAdapter.OnItemClickListener MyItemClickListener &#61; new MyRecyclerViewAdapter.OnItemClickListener() {&#64;Overridepublic void onItemClick(View v, int position) {switch (v.getId()){case R.id.btn_agree:Toast.makeText(MainActivity.this,"你点击了同意按钮"&#43;(position&#43;1),Toast.LENGTH_SHORT).show();break;case R.id.btn_refuse:Toast.makeText(MainActivity.this,"你点击了拒绝按钮"&#43;(position&#43;1),Toast.LENGTH_SHORT).show();break;default:Toast.makeText(MainActivity.this,"你点击了item按钮"&#43;(position&#43;1),Toast.LENGTH_SHORT).show();break;}}&#64;Overridepublic void onItemLongClick(View v) {}};}
最后成功实现多布局item及item内部控件的点击事件&#xff01;&#xff01;