主方法 Activity
package chenchneg20171009.bwie.com.lianxi10day13_1; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { RecyclerView rv; List<ItemBrean> list = new ArrayList<ItemBrean>(); /** * 网格 */ private Button mBt; /** * 网格 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //初始化数据 rv= (RecyclerView) findViewById(R.id.rv); show(true); } private void initView() { mBt = (Button) findViewById(R.id.bt); mBt.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt: String str = mBt.getText().toString().trim(); if ("网格".equals(str)){ show(false); mBt.setText("垂直"); }else{ show(true); mBt.setText("网格"); } break; } } private void show(boolean flag){ //设置布局管理器 //垂直列表 // rv.setLayoutManager(new LinearLayoutManager(this)); //网格模式 // rv.setLayoutManager(new GridLayoutManager(this, 2)); //添加垂直分割线 rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); //水平分割线 RecyclerView.LayoutManager layoutManager=null; if (flag){ layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); }else{ layoutManager=new GridLayoutManager(this,2); } rv.setLayoutManager(layoutManager); rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL)); //横向列表false是从左往右,ture是从右往左 // rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)); for (int i = 0; i < 100; i++) { ItemBrean itemBrean = new ItemBrean("name" + i, "age" + i); list.add(itemBrean); } //创建适配器 MyAdapter myAdapter = new MyAdapter(this, list); rv.setAdapter(myAdapter); myAdapter.setonItemClick(new MyAdapter.OnItemListener() { @Override public void onItemClick(int psotion) { Toast.makeText(MainActivity.this,"点击"+psotion,Toast.LENGTH_SHORT).show(); } @Override public void onItemlongClick(int postion) { Toast.makeText(MainActivity.this,"长点击"+postion,Toast.LENGTH_SHORT).show(); } }); } }适配器 Adapter:
package chenchneg20171009.bwie.com.lianxi10day13_1; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; /** * Created by dell on 2017/10/13. */ public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ItemBrean> list; private OnItemListener onItemListener; //接口回调 public interface OnItemListener{ public void onItemClick(int psotion); public void onItemlongClick(int postion); } public void setonItemClick(OnItemListener onItemListener){ this.onItemListener=onItemListener; } public MyAdapter(Context context, List<ItemBrean> list){ this.context=context; this.list=list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(context).inflate(R.layout.item1,parent,false); Type1ViewHolder type1ViewHolder = new Type1ViewHolder(view); return type1ViewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { ItemBrean itemBrean = list.get(position); //强转 Type1ViewHolder type= (Type1ViewHolder) holder; type.tvname.setText(itemBrean.getName()); type.tvage.setText(itemBrean.getAge()); //条目监听 type.ll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (onItemListener!=null){ onItemListener.onItemClick(position); } } }); type.ll.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (onItemListener!=null){ onItemListener.onItemlongClick(position); } return true; } }); } @Override public int getItemCount() { return list.size(); } private class Type1ViewHolder extends RecyclerView.ViewHolder{ TextView tvname; TextView tvage; LinearLayout ll; public Type1ViewHolder(View itemView) { super(itemView); tvname = itemView.findViewById(R.id.tv_name); tvage = itemView.findViewById(R.id.tv_age); ll = itemView.findViewById(R.id.ll); } } }