之前已经用过了ListVIEw控件,虽然可以实现许多放入功能,但是其扩展性还是有一定缺陷的,比如所无法实现横向布局,为此,AndroID中提供了一个更强大的滚动控件——RecyclerVIEw,它可以实现ListVIEw的功能,同时还在ListVIEw的基础上进行了优化。
RecyclerVIEw基本使用
想要使用RecyclerVIEw,首先我们应该在app的build.gradle中添加相关依赖:
dependencIEs { 、、、 implementation 'androIDx.recyclervIEw:recyclervIEw:1.1.0' 、、、}
之后就可以直接使用RecyclerVIEw控件了:
<!--由于RecyclerVIEw并不是内置在SDK中,所以必须把完整的包路径写下来--><androIDx.recyclervIEw.Widget.RecyclerVIEw androID:ID="@+ID/recyclerVIEw" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"/>
使用RecyclerVIEw实现ListVIEw相同的效果:
创建主活动CardRecyclerVIEwActivity,设置布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <androIDx.recyclervIEw.Widget.RecyclerVIEw androID:ID="@+ID/recycler_vIEw_card" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /></linearLayout>
编写子列表的布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <ImageVIEw androID:ID="@+ID/card_image" androID:layout_wIDth="150dp" androID:layout_height="150dp" androID:scaleType="fitCenter" androID:src="@mipmap/ic_launcher" /> <linearLayout androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="6" androID:layout_margintop="3dp" androID:layout_marginleft="5dp" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="5dp" androID:text="name" androID:ID="@+ID/card_name"/> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="5dp" androID:text="Msg" androID:ID="@+ID/card_msg" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="5dp" androID:text="Info" androID:ID="@+ID/card_info"/> </linearLayout></linearLayout>
子列表样式:
创建一个Card类对应我们的列表项,用来封装数据:
public class Card { private String name; private int imageID; private String msg; private String info; public Card(String name, int imageID,String msg,String info) { this.name = name; this.imageID = imageID; this.msg=msg; this.info=info; } public String getname() { return name; } public int getimageID() { return imageID; } public String getMsg() { return msg; } public String getInfo() { return info; }}
使用RecyclerVIEw实现一个适配器,新建CardAdapter类,让这个类继承RecyclerVIEw.Adapter,并将泛型置定位CardAdapter.VIEwHolder。其中VIEwHolder我们定义为内部类。
继承RecyclerVIEw.Adapter需要实现的三个方法:
onCreateVIEwHolder(VIEwGroup parent, int vIEwType):创建VIEwHolder实例,在这个方法中可以将列表布局加载进来,之后创建一个VIEwHolder实例,并把加载出来的布局传入到构造函数中,最后将VIEwHolder实例返回。onBindVIEwHolder(VIEwHolder holder, int position):用于对RecyclerVIEw子项进行赋值,会在每个子项被滚动到屏幕内时执行,我们可以通过position获取当前列表项对应的实例,然偶进行 *** 作。getItemCount():表示RecyclerVIEw中有多少个子项,返回数据源的长度public class CardAdapter extends RecyclerVIEw.Adapter<CardAdapter.VIEwHolder>{ private Context context; private List<Card> mCardList; public CardAdapter(Context context,List<Card> cardList) { this.context=context; mCardList = cardList; } @OverrIDe public CardAdapter.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { VIEw vIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false); final VIEwHolder holder = new VIEwHolder(vIEw); holder.cardVIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { int position = holder.getAdapterposition(); Card card = mCardList.get(position); Toast.makeText(v.getContext(), card.getname()+": 奥里给", Toast.LENGTH_SHORT).show(); } }); holder.cardImage.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { int position = holder.getAdapterposition(); final Card card = mCardList.get(position); AlertDialog.Builder builder=new AlertDialog.Builder(context); builder.setIcon(card.getimageID()); builder.setTitle(card.getname()); builder.setMessage("要拉我上船吗?"); builder.setPositivebutton("确定", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { Toast.makeText(context,card.getname()+"加入了海贼船", Toast.LENGTH_SHORT).show(); } }); builder.setNegativebutton("取消", null); builder.create(); builder.show(); } }); return holder; } @OverrIDe public voID onBindVIEwHolder(VIEwHolder holder, int position) { Card card = mCardList.get(position); holder.cardImage.setimageResource(card.getimageID()); holder.cardname.setText(card.getname()); holder.cardMsg.setText(card.getMsg()); holder.cardInfo.setText(card.getInfo()); } @OverrIDe public int getItemCount() { return mCardList.size(); } static class VIEwHolder extends RecyclerVIEw.VIEwHolder { VIEw cardVIEw; ImageVIEw cardImage; TextVIEw cardname; TextVIEw cardMsg; TextVIEw cardInfo; public VIEwHolder(VIEw vIEw) { super(vIEw); cardVIEw = vIEw; cardImage = (ImageVIEw) vIEw.findVIEwByID(R.ID.card_image); cardname = (TextVIEw) vIEw.findVIEwByID(R.ID.card_name); cardMsg=(TextVIEw) vIEw.findVIEwByID(R.ID.card_msg); cardInfo=(TextVIEw) vIEw.findVIEwByID(R.ID.card_info); } }}
适配器准备好就可以编写主界面了:
public class CardRecyclerVIEwActivity extends AppCompatActivity { private List<Card> cardList = new ArrayList<Card>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_card_recycler_vIEw); initCards(); RecyclerVIEw recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recycler_vIEw_card); linearlayoutmanager layoutManager=new linearlayoutmanager(this); //设置布局管理器 recyclerVIEw.setLayoutManager(layoutManager); // StaggeredGrIDLayoutManager layoutManager = new// StaggeredGrIDLayoutManager(3, StaggeredGrIDLayoutManager.VERTICAL);// recyclerVIEw.setLayoutManager(layoutManager); //添加分割线 recyclerVIEw.addItemdecoration(new divIDerItemdecoration( this, divIDerItemdecoration.HORIZONTAL)); CardAdapter adapter = new CardAdapter(CardRecyclerVIEwActivity.this,cardList); //设置adapter recyclerVIEw.setAdapter(adapter); } private voID initCards() { List<Card> List = new ArrayList<Card>(); List.add(new Card("路飞", R.mipmap.lufei,"【船长】蒙奇·D·路飞(蒙奇·D·路飞 )","1500000000¥")); List.add(new Card("索隆", R.mipmap.suolong,"【剑士】罗罗诺亚·索隆","320000000¥")); List.add(new Card("乌索普", R.mipmap.wusuopu,"【狙击手】乌索普","200000000¥")); List.add(new Card("娜美", R.mipmap.namei,"【航海士】娜美","66000000¥")); List.add(new Card("山治", R.mipmap.shanzhi,"【厨师】香吉士","330000000¥")); List.add(new Card("乔巴", R.mipmap.qiaoba,"【船医】托尼托尼·乔巴","100¥")); List.add(new Card("罗宾", R.mipmap.luobin,"【考古学家】妮可·罗宾 ","130000000¥")); List.add(new Card("弗兰奇", R.mipmap.fulanqi,"【船匠】弗兰奇","94000000¥")); List.add(new Card("布鲁克", R.mipmap.buluke,"【音乐家】布鲁克","83000000¥")); List.add(new Card("香克斯", R.mipmap.xiangkesi,"【四皇】香克斯","4000000000¥")); for(int i=0;i<60;i++){ cardList.add(List.get((int)(Math.random()*10))); } }}
运行结果:
修改子列表项的布局:card_item.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="150dp" androID:layout_height="150dp"> <ImageVIEw androID:ID="@+ID/card_image" androID:layout_wIDth="150dp" androID:layout_height="150dp" androID:scaleType="fitCenter" androID:src="@mipmap/ic_launcher" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="name" androID:ID="@+ID/card_name"/> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:text="Info" androID:ID="@+ID/card_info"/> </relativeLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="3dp" androID:layout_marginleft="5dp" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="5dp" androID:textSize="15dp" androID:text="Msg" androID:ID="@+ID/card_msg" /> </linearLayout></linearLayout>
修改主活动的代码:
//linearlayoutmanager layoutManager=new linearlayoutmanager(this);//recyclerVIEw.setLayoutManager(layoutManager);//横向的效果linearlayoutmanager layoutManager=new linearlayoutmanager(this);layoutManager.setorIEntation(linearlayoutmanager.HORIZONTAL);recyclerVIEw.setLayoutManager(layoutManager);//网格布局GrIDLayoutManager grIDLayoutManager = new GrIDLayoutManager( this,3);recyclerVIEw.setLayoutManager(grIDLayoutManager);//流式布局StaggeredGrIDLayoutManager layoutManager = newStaggeredGrIDLayoutManager(3, StaggeredGrIDLayoutManager.VERTICAL);recyclerVIEw.setLayoutManager(layoutManager);
横向布局:
流式布局:
以上是内存溢出为你收集整理的Android快速入门之滚动控件RecyclerView全部内容,希望文章能够帮你解决Android快速入门之滚动控件RecyclerView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)