MainActivity-------------
package com.example.administrator.recycleviewdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.LinearLayout; /** * recycleview的核心 * 1实例化recycleview * 2rv作为activity的内容布局 * 3.设置rv的布局 比如,new LinearLayoutManager() * 4.rv设置适配器 * 4.1getitemCount()返回item的个数 * 4.2自定义一个viewholder类(继承recycleview.viewholder),填充到onCreateview方法中,返回装填了子布局的viewholder() * 4.2.1在自定义viewholder内部,绑定一个子布局视图 * 4.2.2在 自定义viewholder中,返回一个textview给外部调用 * 5在onbindviewholder中为子布局赋值,第一个参数是强转为自定义的viewholder, 二参是item的索引值 * 5.1t通过自定义viewholder获得子布局进而设置值。 */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * 分家之后: * 1实例化 * 2setcontentview * 3setlayoutManager * 3.1布局样式线性布局 * 3.2表格布局new GridLayoutManager(context,列数(行数),方向,是否反转); * 4最后,填充适配器 */ RecyclerView rv = new RecyclerView(this); setContentView(rv); rv.setLayoutManager(new GridLayoutManager(this, 2, LinearLayout.HORIZONTAL, false)); // rv.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, true));//在主函数中可以设置布局的显示样式:方向,是否反转 rv.setAdapter(new MyAdapter(this)); } }
适配器:
package com.example.administrator.recycleviewdemo; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; /** * Created by Administrator on 2015/10/17. * 自定义recycleview的item样式的方法 * ·1在layout文件夹下新建子布局的xml文件 * 2.提取出adapter适配器继承自recycleview.adapter(); * 3.声明View控件和子布局中涉及的控件 * 4.oncreateViewHolder()方法中通过inflate加载子布局xml视图 * 5.在自定义的viewholder方法中实例化子布局控件 * 6.声明实例化数据源,在onBindViewHolder()中为控件填充数据 */ class MyAdapter extends RecyclerView.Adapter { private MainActivity mainActivity; private View root; private TextView title, content; public MyAdapter(MainActivity mainActivity) { this.mainActivity = mainActivity; } class viewholder extends RecyclerView.ViewHolder { public viewholder(View root) { super(root); title = (TextView) root.findViewById(R.id.title); content = (TextView) root.findViewById(R.id.content); } public TextView gettitle() { return title; } public TextView getContent() { return content; } } //创建viewholder,实例化viewholder中的item对象 @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { return new viewholder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listcell, null));//new viewholder 通过layoutInflater.from().inflate(布局xml,根节点视图【null】) } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { viewholder vh = (viewholder) viewHolder; CellData cd = datas[i]; //给属性赋值 vh.gettitle().setText(cd.title); vh.getContent().setText(cd.content); } @Override public int getItemCount() { return datas.length; } //声明数据源 private CellData[] datas = new CellData[]{new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa"), new CellData("jaosn", "sda"), new CellData("dsda", "fdsafdsaa")}; }
数据源:
package com.example.administrator.recycleviewdemo; /** * Created by Administrator on 2015/10/17. */ public class CellData { public String title; public String content; public CellData(String title, String content) { this.title = title; this.content = content; } }