导入依赖
compile 'com.android.support:recyclerview-v7:25.3.1'
//效果
主布局文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.administrator.dianshang.activity.ThridActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/recycle_view" > </android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout>=============mainActivity================
private RecyclerView recyclerView; private List<String> list; private MoreItemAdapter ma; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_thrid); //获取控件 recyclerView = (RecyclerView) findViewById(R.id.recycle_view); //数据源 list = new ArrayList<>(); for (int i=0;i<1000;i++){ list.add("条目"+i); } //布局管理 recyclerView.setLayoutManager(new LinearLayoutManager(ThridActivity.this)); //创建适配器 ma = new MoreItemAdapter(list,ThridActivity.this); recyclerView.setAdapter(ma); //条目的点击事件 接口回调 ma.setOnitemClickListner(new OnItemClickListner() { @Override public void onItemClick(int position) { Toast.makeText(ThridActivity.this,"点击了"+list.get(position),Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(int position) { Toast.makeText(ThridActivity.this,"长按了"+list.get(position),Toast.LENGTH_SHORT).show(); } }); }=============适配器================
List<String> list; Context context; private final int text_Only=0; private final int IMAGE_LEFT=1; private final int IMAGE_RIGHT=2; private OnItemClickListner onItemClickListner; public MoreItemAdapter(List<String> list, Context context) { this.list = list; this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder holder=null; View view; switch (viewType){ case text_Only: view= LayoutInflater.from(context).inflate(R.layout.water_item,parent,false); holder=new Text_Only(view); break; case IMAGE_LEFT: view= LayoutInflater.from(context).inflate(R.layout.image_left,parent,false); holder=new Image_left(view); break; case IMAGE_RIGHT: view= LayoutInflater.from(context).inflate(R.layout.image_right,parent,false); holder=new Image_Right(view); break; } return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { //判断方法返回值 if(getItemViewType(position)==text_Only){ ((Text_Only)holder).tv.setText(list.get(position)); //条目点击 ((Text_Only)holder).tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListner.onItemClick(position); } }); ((Text_Only)holder).tv.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemClickListner.onItemLongClick(position); return true; } }); }else if(getItemViewType(position)==IMAGE_LEFT){ ((Image_left)holder).tv.setText(list.get(position)); }else{ ((Image_Right)holder).tv.setText(list.get(position)); } } @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { if(position%3==0){ return text_Only; }else if(position%3==1){ return IMAGE_LEFT; }else{ return IMAGE_RIGHT; } } public void setOnitemClickListner(OnItemClickListner onItemClickListner) { this.onItemClickListner = onItemClickListner; //onItemClickListner.onItemClick(); }==========接口===========
public interface OnItemClickListner { public void onItemClick(int position); public void onItemLongClick(int position); }