前言

最近在项目中用到了ListView,不知道为什么总是出现数据错乱的情况,另外RecyclerView包含很多Item动画,所以改成了RecyclerView。

简单使用

  1. RecyclerView使用的时候需要引入依赖
implementation 'com.android.support:recyclerview-v7:28.0.0'

后面的版本号根据自己的项目版本更改,引入完成之后点击上面的Sync Now

  1. 在activity中添加控件
<android.support.v7.widget.RecyclerView
android:id="@+id/focus_list_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"></android.support.v7.widget.RecyclerView>
  1. 编写item.xml布局文件
<?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="70dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="4dp"
android:background="#fff"
android:orientation="horizontal"> <com.onepilltest.others.SwipeMenu
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:id="@+id/focus_list_show"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center_vertical"> <ImageView
android:layout_marginLeft="20dp"
android:id="@+id/focus_list_item_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:scaleType="centerCrop"
android:src="@drawable/user"> </ImageView> </LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="5dp"> <TextView
android:id="@+id/focus_list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:gravity="left"
android:text="李四 18831107935" /> <TextView
android:id="@+id/focus_list_item_tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="河北师范大学软件学院302" />
</LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="3"
android:gravity="center"> <ImageView
android:id="@+id/focus_list_item_add"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout> <LinearLayout
android:layout_width="160dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/focus_list_adds"
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="#f70"
android:gravity="center"
android:text="查看"
android:textColor="#ffffff"
android:textSize="14sp" />
<TextView
android:id="@+id/focus_list_dels"
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="@color/holo_red_light"
android:gravity="center"
android:text="取消关注"
android:textColor="#ffffff"
android:textSize="14sp" />
</LinearLayout> </com.onepilltest.others.SwipeMenu>
</LinearLayout>
  1. 创建Adapter
package com.onepilltest.personal;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.onepilltest.R;
import com.onepilltest.URL.Connect;
import com.onepilltest.entity.ToFocus;
import com.onepilltest.entity.focus;
import com.onepilltest.index.DoctorDetailsActivity;
import com.onepilltest.index.FoundDoctorActivity; import java.util.List; public class FocusListAdapter2 extends RecyclerView.Adapter<FocusListAdapter2.ViewHolder> { private List<ToFocus> focusList; public FocusListAdapter2(List<ToFocus> baseList) {
focusList = baseList;
for(ToFocus f:focusList){
Log.e("focus数据源",""+f.toString());
} } static class ViewHolder extends RecyclerView.ViewHolder{
ImageView img ;
TextView name;
TextView more;
TextView add;
TextView del; public ViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.focus_list_item_img);
name = itemView.findViewById(R.id.focus_list_item_name);
more = itemView.findViewById(R.id.focus_list_item_tag);
add = itemView.findViewById(R.id.focus_list_adds);
del = itemView.findViewById(R.id.focus_list_dels);
}
}
private Context mContext;
// //点击和长按
// public interface OnItemClickListener {
// void onClick(int position);
// }
// private OnItemClickListener listener;
// public void setOnItemClickListener(OnItemClickListener listener) {
// this.listener = listener;
// }
//
// public interface OnItemLongClickListener {
// void onClick(int position);
// }
// private OnItemLongClickListener longClickListener;
// public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
// this.longClickListener = longClickListener;
// } @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.focus_liste_item,viewGroup,false);
mContext = viewGroup.getContext();
ViewHolder holder = new ViewHolder(view);
return holder;
} @Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
ToFocus focus = focusList.get(i);
//加载头像
RequestOptions requestOptions = new RequestOptions().circleCrop();
Glide.with(mContext)
.load(Connect.BASE_URL + focus.getImg())
.apply(requestOptions)
.into(viewHolder.img);
viewHolder.name.setText(focus.getName());
viewHolder.more.setText(focus.getMore());
viewHolder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("focusAdapter","查看第"+i+"条数据");
Toast.makeText(mContext,"查看第"+i+"条数据,Type:"+focus.toString(),Toast.LENGTH_SHORT).show();
if (focus.getType() == 1){
Intent intent = new Intent(mContext, DoctorDetailsActivity.class);
intent.putExtra("id",focus.getTypeId());
mContext.startActivity(intent);
}else{
Intent intent = new Intent(mContext, ProductActivity.class);
intent.putExtra("id",focus.getTypeId());
mContext.startActivity(intent);
} }
});
viewHolder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("focusAdapter","删除第"+i+"条数据");
Toast.makeText(mContext,"删除第"+i+"条数据",Toast.LENGTH_SHORT).show();
}
});
} @Override
public int getItemCount() {
return focusList.size();
} }
  1. 在MainActivity中绑定Adapter
        recyclerView = findViewById(R.id.focus_list_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new FocusListAdapter2(baseList);
recyclerView.setAdapter(adapter);

关于RecyclerView(一)基本使用的更多相关文章

  1. RecyclerView使用大全

    RecylerView介绍 RecylerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字recyler ...

  2. 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载

    title: 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载 tags: -RecyclerView,下拉刷新,上拉加载更多 grammar_cjkRuby: true - ...

  3. 安卓易学,爬坑不易——腾讯老司机的RecyclerView局部刷新爬坑之路

    针对手游的性能优化,腾讯WeTest平台的Cube工具提供了基本所有相关指标的检测,为手游进行最高效和准确的测试服务,不断改善玩家的体验.目前功能还在免费开放中. 点击地址:http://wetest ...

  4. Android Studio开发RecyclerView遇到的各种问题以及解决(二)

    开发RecyclerView时候需要导入别人的例子,我的是从github导入的,下载下github的压缩包之后解压看你要导入的文件是priject还是Module.(一般有app文件夹的大部分是pro ...

  5. Android Studio开发RecyclerView遇到的各种问题以及解决(一)

    以前一直在用ListView,,,最近才看RecyclerView发现好强大.RecyclerView前提是Android版本在5.0以上,本人以前用的是eclipse只支持到4.4.索性就安装一个A ...

  6. Android的Kotlin秘方(II):RecyclerView 和 DiffUtil

    作者:Antonio Leiva 时间:Sep 12, 2016 原文链接:http://antonioleiva.com/recyclerview-diffutil-kotlin/ 如你所知,在[支 ...

  7. Android RecyclerView 实现支付宝首页效果

    Android RecyclerView 实现支付宝首页效果 [TOC] 虽然我本人不喜欢支付宝的,但是这个网格本身其实还是不错的,项目更新中更改了一个布局为网格模式,类似支付宝.(估计是产品抄袭的= ...

  8. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  9. 打造android偷懒神器———RecyclerView的万能适配器

    转载请注明出处谢谢:http://www.cnblogs.com/liushilin/p/5720926.html 很不好意思让大家久等了,本来昨天就应该写这个的,无奈公司昨天任务比较紧,所以没能按时 ...

  10. 安卓v7支持包下的ListView替代品————RecyclerView

    RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术般的控件. 项目已经同步至github:https://github.com/ ...

随机推荐

  1. 【贪心】Emergency Evacuation

    题目 大致题意 把指定的人从同一出口送出车外,且同一位置不能同时有两个人,求所需的最短时间. 分析 第一感觉就是利用贪心思想解决问题,但是这道题的数据范围用模拟的话肯定是会爆掉的,所以这是不可取的.我 ...

  2. hive命令查询数据不显示表头解决办法

    在hive命令行中查询数据如下: 表头未显示出来 解决办法: 修改hive安装包conf/hive-site.xml配置文件: <property> <name>hive.cl ...

  3. scrapy分布式抓取基本设置

    scrapy本身并不是一个为分布式爬取而设计的框架,但第三方库scrapy-redis为其扩展了分布式抓取的功能,在分布式爬虫框架中,需要使用某种通信机制协调各个爬虫工作 (1)当前的爬取任务,下载+ ...

  4. 数据的编码和解码--java例子

    昨天借了一本<网络程序设计实验教程(java语言)>,然后看了第一章,一个Swing例子,于是为大家分享一下! 关于数据的编码与解码,我觉得就例子而言已经交待得非常清楚了,两种方法做的. ...

  5. 树莓派搭建Nexus2私服

    使用树莓派搭建Nexus2私服需要的材料有: 树莓派3B+(或者4B) 移动硬盘一个 1. 下载nexus2.x安装包 由于nexus2.x官方的启动环境并不支持arm架构的树莓派,所以这里采用tom ...

  6. css modules是什么?

    什么是CSS Modules? 官方的介绍是: 所有的 class 的名称和动画的名称默认属于本地作用域的 CSS 文件.所以 CSS Modules 不是一个官方的规范,也不是浏览器的一种机制,它是 ...

  7. HTTP前世今生

    HTTP 是浏览器中最重要且使用最多的协议,是浏览器和服务器之间的通信语言.随着浏览器的发展,HTTP 为了能适应新的形式也在持续进化.已经历经0.9,1.0,1.1,2.0等几个阶段, 以及未来的3 ...

  8. java 面向对象(三):类结构 属性

    类的设计中,两个重要结构之一:属性 对比:属性 vs 局部变量 1.相同点: * 1.1 定义变量的格式:数据类型 变量名 = 变量值 * 1.2 先声明,后使用 * 1.3 变量都其对应的作用域 2 ...

  9. 数据可视化之powerBI基础(四)深入了解PowerBI的工具提示功能

    https://zhuanlan.zhihu.com/p/36804592 在PowerBI上个月的更新中,增加了工具提示功能,这项功能允许您将自己的可视化作品放置到工具提示中,通过鼠标悬停的方式来展 ...

  10. AcWing 94. 递归实现排列型枚举

    AcWing 94. 递归实现排列型枚举 题目链接 把 1~n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序. 输入格式 一个整数n. 输出格式 按照从小到大的顺序输出所有方案,每行1个. ...