android中RecyclerView的简单使用(一)

  • RecyclerView.Adapter

  • LayoutManager

  • ItemAnimator

RecyclerView.Adapter

确切的说,Adapter扮演着两个角色。一是,根据不同ViewType创建与之相应的的Item-Layout,二是,访问数据集合并将数据绑定到正确的View上。这就需要我们重写以下两个函数:

*public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 创建Item视图,并返回相应的ViewHolder

*public void onBindViewHolder(VH holder, int position) 绑定数据到正确的Item视图上。

另外我们还需要重写另一个方法,像ListView-Adapter那样,同样地告诉RecyclerView-Adapter列表Items的总数:

public int getItemCount() 返回该Adapter所持有的Itme数量

ViewHolder的基本用法是用来存放View对象。Android团队很早之前就推荐使用“ViewHolder设计模式”,但实际上他们并没有把这种概念强加给开发者,而且也没有要求开发者在Adapter中必须使用ViewHolder pattern。那么现在对于这种新型的RecyclerView.Adapter,我们必须实现并使用它。

RecyclerView.LayoutManager

LayoutManager的职责是摆放Item的位置,并且负责决定何时回收和重用Item。必须为RecyclerView指定LayoutManager,否则会出现异常

目前SDK中提供了三种自带的LayoutManager:

LinearLayoutManager 水平或者垂直的Item视图。

GridLayoutManager 网格Item视图。

StaggeredGridLayoutManager 交错的网格Item视图。瀑布流

RecyclerView.ItemDecoration

通过设置recyclerView.addItemDecoration(new DividerDecoration(this));来改变Item之间的偏移量或者对Item进行装饰。

RecyclerView.ItemDecoration是一个抽象类,可以通过重写以下三个方法,来实现Item之间的偏移量或者装饰效果:

public void onDraw(Canvas c, RecyclerView parent) 装饰的绘制在Item条目绘制之前调用,所以这有可能被Item的内容所遮挡

public void onDrawOver(Canvas c, RecyclerView parent) 装饰的绘制在Item条目绘制之后调用,因此装饰将浮于Item之上

public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) 与padding或margin类似,LayoutManager在测量阶段会调用该方法,计算出每一个Item的正确尺寸并设置偏移量。

RecyclerView.ItemAnimator

ItemAnimator能够帮助Item实现独立的动画。

ItemAnimator作触发于以下三种事件:

某条数据被插入到数据集合中

从数据集合中移除某条数据

更改数据集合中的某条数据

幸运的是,在Android中默认实现了一个DefaultItemAnimator,我们可以通过以下代码为Item增加动画效果:

recyclerView.setItemAnimator(new DefaultItemAnimator());

在之前的版本中,当时据集合发生改变时,我们通过调用.notifyDataSetChanged(),来刷新列表,因为这样做会触发列表的重绘,所以并不会出现任何动画效果,因此需要调用一些以notifyItem*()作为前缀的特殊方法,比如:

public final void notifyItemInserted(int position) 向指定位置插入Item

public final void notifyItemRemoved(int position) 移除指定位置Item

public final void notifyItemChanged(int position) 更新指定位置Item

这些就是RecyclerView的一些基础知识。下面贴一些代码,方便理解。

public class MainActivity extends Activity implements CallBack {

private LinearLayout mLin;

private RecyclerView recycle;

private List list=new ArrayList();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initList();

initView();

}

private void initView() {

mLin=(LinearLayout) findViewById(R.id.mLin);

recycle=new RecyclerView(this);

recycle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

mLin.addView(recycle);

LinearLayoutManager manager=new LinearLayoutManager(this);//线性布局管理器,支持横向和纵向

manager.setOrientation(LinearLayoutManager.HORIZONTAL);

recycle.setLayoutManager(manager);

recycle.setHasFixedSize(true);//固定宽高

recycle.setItemAnimator(new DefaultItemAnimator());//默认的条目添加动画

recycle.setAdapter(new MyAdapter(this, list, this));

}

private void initList() {

for(int i=0;i<30;i++){

list.add(“第”+(i+1)+“条数据”);

}

}

@Override

public void call(int index) {

Toast.makeText(this, list.get(index), 0).show();

}

}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH>{

private Context ctx;

private List list;

private CallBack call;

public MyAdapter(Context ctx,List list,CallBack call) {

this.ctx=ctx;

this.list=list;

this.call=call;

}

//内部类–》找ID

class VH extends RecyclerView.ViewHolder{

private TextView tv;

private ImageView img;

private View view;

public VH(View arg0) {

super(arg0);

tv=(TextView) arg0.findViewById(R.id.tv);

img=(ImageView) arg0.findViewById(R.id.img);

view=arg0.findViewById(R.id.view);

}

}

@Override

public int getItemCount() {

return list.size();

}

//arg0==holder arg1:position

@Override

public void onBindViewHolder(VH arg0, final int arg1) {

arg0.img.setImageResource(R.drawable.ic_launcher);

arg0.tv.setText(list.get(arg1));

arg0.view.setBackgroundColor(Color.BLACK);

//自己写的条目点击事件

arg0.tv.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

call.call(arg1);

}

});

}

@Override

public VH onCreateViewHolder(ViewGroup arg0, int arg1) {

View v=View.inflate(ctx, R.layout.item, null);

return new VH(v);

}

public interface CallBack{

public void call(int index);//index:是你要点击的那个条目对应的在list中的位置:position

}

}

xml中的代码比较简单,就不贴了。

二、实验要求: 1、 内网为个企业的内部网络。其中,PC1是总经理机器,PC2是业务部机器, PC3为财务部机器,PC2和PC3分属两个VLAN,PC1可以同时访问财务部和业务部。 2、 二层交换机1和三层交换机1之间实现聚合;二层交换机2和三层交换机1之间实现RSTP。 3、 PC4为FTP服务器,只允许管理人员总经理访问,其他工作人员不允许访问。 4.该内网只有个合法的上网地址,pc5为外网上的web服务器,作路由使得内外网之间可以通信,并且在Router1上作NAPT,使得只有pc1所在的网段和pc4可以登录外网,且pc4向外网提供ftp服务。 三、实验步骤 二层交换机1的配置: S2126G-1>enable 14 Password: S2126G-1#conf Enter configuration commands, one per line. End with CNTL/Z. S2126G-1(config)# S2126G-1(config)#hostname % Incomplete command. S2126G-1(config)#hostname wde 2008-06-26 17:09:29 @5-CONFIG:Configured from outband wde(config)#vlan 10 2008-06-26 17:09:34 @5-CONFIG:Configured from outband wde(config-vlan)#end 2008-06-26 17:09:38 @5-CONFIG:Configured from outband wde#conf Enter configuration commands, one per line. End with CNTL/Z. wde(config)#interface range fastethernet 0/3-6 2008-06-26 17:10:26 @5-CONFIG:Configured from outband wde(config-if-range)# wde(config-if-range)#switchport access vlan 20 2008-06-26 17:11:02 @5-CONFIG:Configured from outband %Warning : Access VLAN does not exist. Creating vlan 20 wde(config-if-range)#exit 2008-06-26 17:11:08 @5-CONFIG:Configured from outband wde(config)#interface aggregateport 1 2008-06-26 17:15:43 @5-CONFIG:Configured from outband wde(config-if)#switchport mode trunk 2008-06-26 17:16:04 @5-CONFIG:Configured from outband wde(config-if)#exit 2008-06-26 17:16:08 @5-CONFIG:Configured from outband wde(config)#interface range fastethernet 0/1-2 2008-06-26 17:16:48 @5-CONFIG:Configured from outband wde(config-if-range)#port-group 1 2008-06-26 17:17:12 @5-CONFIG:Configured from outband wde(config-if-range)#exit 2008-06-26 17:17:16 @5-CONFIG:Configured from outband wde(config)# Press RETURN to get started!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值