本文实例为大家分享了AndroID RecyclerVIEw使用的具体代码,供大家参考,具体内容如下
package com.itheima74.recyclervIEw; import androID.os.Bundle; import androID.support.v7.app.AppCompatActivity; import androID.support.v7.Widget.GrIDLayoutManager; import androID.support.v7.Widget.RecyclerVIEw; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.ImageVIEw; import androID.Widget.TextVIEw; import java.util.ArrayList; /** * RecyclerVIEw * 1.增强版的ListvIEw * 2.官方推荐 * 3.使用步骤: * a.build.gradle文件中添加依赖compile 'com.androID.support:recyclervIEw-v7:25.2.0' * b.xml布局中引入,注意完整包名+类名 * c.具体见代码 */ public class MainActivity extends AppCompatActivity { ArrayList<Fruit> mFruitList; private RecyclerVIEw recyclervIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEw(); initData(); initAdapter(); } /** * 初始化数据适配器 */ private voID initAdapter() { recyclervIEw.setAdapter(new FruitAdapter()); } /** * 初始化控件 */ private voID initVIEw() { recyclervIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclervIEw); // 设置为linearlayoutmanager布局方式,线性布局 // linearlayoutmanager layoutManager = new linearlayoutmanager(this); // 两种方式,水平或垂直排布,默认垂直排布 // layoutManager.setorIEntation(linearlayoutmanager.HORIZONTAL); // recyclervIEw.setLayoutManager(layoutManager); // 设置为GrIDLayoutManager布局方式,3列排布 GrIDLayoutManager grIDLayoutManager = new GrIDLayoutManager(this,3); recyclervIEw.setLayoutManager(grIDLayoutManager); } /** * 初始化数据,水果集合 */ private voID initData() { mFruitList = new ArrayList<>(); mFruitList.add(new Fruit("apple",R.drawable.apple_pic)); mFruitList.add(new Fruit("banana",R.drawable.banana_pic)); mFruitList.add(new Fruit("cherry",R.drawable.cherry_pic)); mFruitList.add(new Fruit("grape",R.drawable.grape_pic)); mFruitList.add(new Fruit("mango",R.drawable.mango_pic)); mFruitList.add(new Fruit("orange",R.drawable.orange_pic)); mFruitList.add(new Fruit("pear",R.drawable.pear_pic)); mFruitList.add(new Fruit("pineapple",R.drawable.pineapple_pic)); mFruitList.add(new Fruit("strawBerry",R.drawable.strawBerry_pic)); mFruitList.add(new Fruit("watermelon",R.drawable.watermelon_pic)); } class FruitAdapter extends RecyclerVIEw.Adapter<VIEwHolder> { // 返回VIEwHolder对象 @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { return new VIEwHolder(VIEw.inflate(getApplicationContext(),R.layout.ListvIEw_item,null)); } // 刷新界面,为VIEwHolder两个属性设置内容 @OverrIDe public voID onBindVIEwHolder(VIEwHolder holder,int position) { Fruit fruit = mFruitList.get(position); holder.iv.setimageResource(fruit.imageID); holder.tv.setText(fruit.name); } @OverrIDe public int getItemCount() { // FruitAdapter是MainActivity的内部类,可以直接使用外部类的成员变量 return mFruitList.size(); } } static class VIEwHolder extends RecyclerVIEw.VIEwHolder { ImageVIEw iv; TextVIEw tv; VIEwHolder(VIEw itemVIEw) { super(itemVIEw); iv = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.iv); tv = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv); } } }
package com.itheima74.recyclervIEw; /** * Created by My on 2017/3/2. */ class Fruit { String name; int imageID; Fruit(String name,int imageID) { this.name = name; this.imageID = imageID; } }
复制代码 代码如下:<img src="http://img.blog.csdn.net/20170303085928987?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1NjIwMTk3Ng==/Font/5a6L5L2T/Fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android RecyclerView的简单使用全部内容,希望文章能够帮你解决Android RecyclerView的简单使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)