Android RecyclerView的简单使用
2019/7/7 20:50:43
本文主要是介绍Android RecyclerView的简单使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了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的简单使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路