android中RecyclerView控件的使用
1、RecyclerView控件不在标准的库里面,需要先引入,引入比较简单,点击控件右边的下载按钮即可

2、先添加一个新闻实体类,用来为新闻列表提供数据,news.java:
package com.example.chenrui.common;
public class News {
    private String title;
    private int pic;
    public News(String title, int pic) {
        this.title = title;
        this.pic = pic;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getPic() {
        return pic;
    }
    public void setPic(int pic) {
        this.pic = pic;
    }
}
3、在res/drawable/xhdpi目录下引用几个图片,做为新闻图片的资源

4、添加一个Layout XML File,做为列表项的内容,news_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="100dp"> <ImageView
android:id="@+id/newsPic"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/newsTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/newsPic"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/newsPic" />
</android.support.constraint.ConstraintLayout>
5、添加一个适配器,这个适配器用于为RecyclerView指定使用的Layout数据项模板,以及对应的数据操作,NewsAdapter.java:
package com.example.chenrui.app1; import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import com.example.chenrui.common.News; import java.util.List; public class NewsAdapter extends RecyclerView.Adapter<News2Adapter.ViewHolder> { private List<News> newsList; public News2Adapter(List<News> newsList) {
this.newsList = newsList;
} @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item,viewGroup,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
} @Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
News news = newsList.get(i);
viewHolder.newsImage.setImageResource(news.getPic());
viewHolder.newsTitle.setText(news.getTitle());
} @Override
public int getItemCount() {
return newsList.size();
} static class ViewHolder extends RecyclerView.ViewHolder { ImageView newsImage;
TextView newsTitle; public ViewHolder(@NonNull View itemView) {
super(itemView); newsImage = itemView.findViewById(R.id.newsPic);
newsTitle = itemView.findViewById(R.id.newsTitle);
}
}
}
6、接下来就是主体Activity了
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main13Activity"> <android.support.v7.widget.RecyclerView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.chenrui.app1; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout; import com.example.chenrui.common.News; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); List<News> newsList = new ArrayList();
newsList.add(new News("新闻标题1",R.drawable.o1));
newsList.add(new News("新闻标题2",R.drawable.o2));
newsList.add(new News("新闻标题3",R.drawable.o3));
newsList.add(new News("新闻标题4",R.drawable.o4));
newsList.add(new News("新闻标题5",R.drawable.o5));
NewsAdapter newsAdapter = new NewsAdapter(newsList); RecyclerView view = findViewById(R.id.list1);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
view.setLayoutManager(layoutManager);
view.setAdapter(newsAdapter);
}
}
到此一个简单的RecyclerView控件示例就完成了,显示效果如下:

android中RecyclerView控件的使用的更多相关文章
- android中RecyclerView控件实现点击事件
		RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ... 
- android中RecyclerView控件实现瀑布流布局
		本文是在之前文章的基础上做的修改:android中RecyclerView控件的使用 1.修改列表项news_item.xml: <?xml version="1.0" en ... 
- android中RecyclerView控件的列表项横向排列
		本文是在上一篇文章的基础上做的修改:android中RecyclerView控件的使用 1.修改列表项news_item.xml:我这里是把新闻标题挪到了新闻图片的下面显示 <?xml vers ... 
- android中RecyclerView控件实现长按弹出PopupMenu菜单功能
		之前写过一篇文章:android中实现简单的聊天功能 现在是在之前功能的基础上,添加一个长按聊天记录,删除对应聊天记录的功能 RecyclerView控件,没有对应的长按事件,我们需要自己手工添加,修 ... 
- Android中ListView控件的使用
		Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ... 
- android中ListView控件&&onItemClick事件中获取listView传递的数据
		http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ... 
- Android中ExpandableListView控件基本使用
		本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ... 
- android中倒计时控件CountDownTimer分析
		android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long milli ... 
- Android 中常见控件的介绍和使用
		1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ... 
随机推荐
- quartz 2.0 与1.0功能对比
			日常开发来说,相对于1.0版,2.0版在使用上有以下几点需要注意的变化 变化一 比1.0多引用了C5.dll C5.dll 一个C#和其他CLI语言的泛型集合类..Net2.0及以上才可以使用.简介地 ... 
- 【jvm】java查看内存使用jmap,jstat和jstack使用 ,docker启动服务下查看jvm使用情况
			[声明,如果是docker启动的服务,可以在进入容器内后,再使用如下命令操作] [docker exec -it 容器ID /bin/bash 即可进入容器内] [如果不是docker启动的 ... 
- 神奇女侠Wonder Woman迅雷下载
			亚马逊公主戴安娜·普林斯(盖尔·加朵 Gal Gadot 饰),经过在家乡天堂岛的训练,取得上帝赐予的武器 与装备,化身神奇女侠,与空军上尉史蒂夫·特雷弗(克里斯·派恩 Chris Pine 饰)一同 ... 
- node.js模块的坑
			在写一个工具的时候,需要将xml转为json方便处理,以前电脑上装的node.js的版本为0.8,结果我再安装node-xml2json时提示版本过低,然后我又重装安装了最新版本. 然后再次尝试安装, ... 
- 详细解读Volley(四)—— 自定义Request
			Volley中提供了几个Request,如果我们有特殊的需求,完全可以自定义Request的,自定义Request自然要继承Request,那么本篇就教大家来一步一步地定义一个自己的Request类. ... 
- 第三方IDC性能测评主要指标
			弹性计算性能弹性计费模式就是 "即用即付 ",最小单位可以按小时来计算.随着云计算负载的增长,企业购买服务器带宽时的资源. 1.弹性计算性能 弹性计费模式就是"即 ... 
- Arcgis  ArcMap 10 如何生成msd地图文档定义【 arcgis mxd怎么转换成msd】
			.mxd是arcgis 的地图文档后缀名. .msd是arcgis 的地图服务定义文件,是 map service definition 的缩写. 基于 MSD 的服务支持 Maplex.制图表达和新 ... 
- Guava之FluentIterable使用示例
			FluentIterable 是guava集合类中常用的一个类,主要用于过滤.转换集合中的数据:FluentIterable是一个抽象类,实现了Iterable接口,大多数方法都返回FluentIte ... 
- ASP.NET MVC:UrlHelper.cs
			ylbtech-funcation-Utility: ASP.NET MVC:UrlHelper.cs 充当表示 ASP.NET Razor 页的类的基类. 1.UrlHelper 类返回顶部 1-1 ... 
- 给Spring的placeholder设置默认值
			问题:使用Spring时,可以方便地通过placeholder的形式${key}将key对应的properities定义value,注入到Bean中.但是如果在properities文件中,没有对ke ... 
