强大的滚动控件——RecyclerView(一)基本用法
来源:互联网 发布:淘宝怎么找代理商 编辑:程序博客网 时间:2024/06/10 18:51
用前准备
打开app/build.gradle文件,在dependencies闭包中添加如下内容:
compile ‘com.android.support:recyclerview-v7:24.2.1’
如下
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:recyclerview-v7:24.2.1' testCompile 'junit:junit:4.12'}
- XML文件
接下来是xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.sky.recyclerview.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" ></android.support.v7.widget.RecyclerView></LinearLayout>
这里面没什么好说的,width和height要设置为match_parent
- 接下来是适配器,新建的FruitAdapter类,让这个适配器继承RecyclerView.Adapter,并将泛型指定为FruitAdapter.ViewHolder。其中ViewHolder是我们在FruitAdapter中定义的一个内部类,代码如下:
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> { private List<Fruit> mFruitList; static class ViewHolder extends RecyclerView.ViewHolder{ ImageView fruitImage; TextView fruitName; public ViewHolder(View itemView) { super(itemView); fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image); fruitName = (TextView) itemView.findViewById(R.id.fruit_name); } } /** * 用于把要展示的数据源传进来,并赋值给一个全局变量mFruitList,我们后续的操作都将在这个数据源的基础上进行。 * @param mFruitList */ public FruitAdapter(List<Fruit> mFruitList) { this.mFruitList = mFruitList; } /** * 用于创建ViewHolder实例的,并把加载出来的布局传入到构造函数当中,最后将viewholder的实例返回 * @param parent * @param viewType * @return */ @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } /** * 用于对RecyclerView子项的数据进行赋值的,会在每个子项被滚动到屏幕内的时候执行,这里我们通过 * position参数得到当前项的Fruit实例,然后再将数据设置到ViewHolder的Imageview和textview当中即可, * @param holder * @param position */ @Override public void onBindViewHolder(ViewHolder holder, int position) { Fruit fruit = mFruitList.get(position); holder.fruitImage.setImageResource(fruit.getImageId()); holder.fruitName.setText(fruit.getName()); } @Override public int getItemCount() { return mFruitList.size(); }}
- 接下来就是Activity.class了
其中一些重要的我都已经在代码中注释
public class MainActivity extends Activity { private List<Fruit> fruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruit(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); /** * 创建一个linearlayoutmaneger对象,并将他设置到recyclerview当中。layoutmanager用于指定 * recyclerview的布局方式,这里是线性布局的意思。可以实现和listview类似的效果。 * * 接下来我们创建了Fruitadapter的实例,并将数据传进去 */ LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); FruitAdapter adapter = new FruitAdapter(fruitList); recyclerView.setAdapter(adapter); } private void initFruit() { Fruit apple = new Fruit("Apple",R.mipmap.ic_launcher); fruitList.add(apple); Fruit banana = new Fruit("banana",R.mipmap.ic_launcher); fruitList.add(banana); Fruit oragen = new Fruit("orange",R.mipmap.ic_launcher); fruitList.add(oragen); Fruit watermelon = new Fruit("watermelon",R.mipmap.ic_launcher); fruitList.add(apple); Fruit pear = new Fruit("pear",R.mipmap.ic_launcher); fruitList.add(pear); Fruit grape = new Fruit("grape",R.mipmap.ic_launcher); fruitList.add(grape); Fruit pineapple = new Fruit("pineapple",R.mipmap.ic_launcher); fruitList.add(pineapple); Fruit strawberry = new Fruit("strawberry",R.mipmap.ic_launcher); fruitList.add(strawberry); Fruit cherry = new Fruit("cherry",R.mipmap.ic_launcher); fruitList.add(cherry); Fruit a = new Fruit("a",R.mipmap.ic_launcher); fruitList.add(a); Fruit b = new Fruit("b",R.mipmap.ic_launcher); fruitList.add(b); Fruit c = new Fruit("c",R.mipmap.ic_launcher); fruitList.add(c); Fruit d = new Fruit("d",R.mipmap.ic_launcher); fruitList.add(d); Fruit e = new Fruit("e",R.mipmap.ic_launcher); fruitList.add(e); Fruit f = new Fruit("f",R.mipmap.ic_launcher); fruitList.add(f); Fruit g = new Fruit("g",R.mipmap.ic_launcher); fruitList.add(g); Fruit h = new Fruit("h",R.mipmap.ic_launcher); fruitList.add(h); }}
- 对了还有一个Fruit类我也顺便贴上来
/** * Created by sky on 2017/2/23. */public class Fruit { private String name; private int imageId; public Fruit(String name, int imageId) { this.name = name; this.imageId = imageId; } public String getName() { return name; } public int getImageId() { return imageId; }}
github地址:https://github.com/skysunlei/RecyclerViewBeat
接下来关于RecyclerView我还会更新几篇进阶型的,有问题的可以留言我会积极回复的
- 效果图
0 0
- 强大的滚动控件——RecyclerView(一)基本用法
- 强大的滚动控件——RecyclerView(二)横向滚动
- 识更加强大的滚动控件——RecyclerView
- 强大的滚动控件RecyclerView
- 强大的滚动控件——RecyclerView(三)瀑布流效果
- RecyclerView -- 更强大的滚动控件1
- RecyclerView--更强大的滚动控件2
- Android 强大的滚动控件 RecyclerView
- RecyclerView的基本用法 (一)
- 10读书笔记之更强大的滚动控件-----RecyclerView
- recyclerview的基本用法(一)
- Material Design学习之RecyclerView(更强大的滚动控件)(4)
- Android控件RecyclerView的基本用法
- android RecyclerView的基本介绍及用法(一)
- RecyclerView 使用方法总结(一):RecyclerView的基本用法,及实现ListView
- RecyclerView的基本用法二--滚动时RecyclerView之外的布局联动
- RecyclerView的基本用法
- RecyclerView的基本用法
- 震惊!!!潜伏11年的Linux内核提权漏洞曝光
- usaco4.4.1 Shuttle Puzzle
- 316. Remove Duplicate Letters
- 父元素无宽高度(或框高度不够),子元素浮动
- java Web知识点(1)
- 强大的滚动控件——RecyclerView(一)基本用法
- 【解惑】H5和HTML5?
- Conos6安装mysql
- [转]pycharm的一些快捷键
- 剑指Offer系列-面试题23:从上到下打印二叉树
- 文件行数统计【find /v /c "&#@"】
- Json和List的转换
- Tomcat7.0.75安装部署
- 外卖购物车源码