Android如何使用Recylerview和api获取列表_android 外部获取recyclerview中所有imageview(1)

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

Dependency/Gradle:

//recyclerview

implementation ‘androidx.recyclerview:recyclerview:1.1.0’

//Image Library

implementation ‘com.squareup.picasso:picasso:2.71828’

//for calling api

implementation ‘com.android.volley:volley:1.1.0’

Add Permission in Manifest.xml


api调用的步骤:


在activitymain.xml中添加回收器视图



<androidx.constraintlayout.widget.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=“.MainActivity”>

​ <androidx.recyclerview.widget.RecyclerView

​ android:id=“@+id/recyclerview”

​ android:layout_width=“409dp”

​ android:layout_height=“729dp”

​ app:layout_constraintStart_toStartOf=“parent”

​ app:layout_constraintTop_toTopOf=“parent” />

</androidx.constraintlayout.widget.ConstraintLayout>

Create item view : itemfeed.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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=“wrap_content”

​ android:background=“#FFFFFF”>

​ <androidx.constraintlayout.widget.ConstraintLayout

​ android:layout_width=“match_parent”

​ android:layout_height=“wrap_content”

​ android:layout_marginStart=“4dp”

​ android:layout_marginLeft=“4dp”

​ android:layout_marginTop=“4dp”

​ android:layout_marginEnd=“4dp”

​ android:layout_marginRight=“4dp”

​ android:layout_marginBottom=“4dp”

​ android:background=“#F4EFEF”

​ app:layout_constraintBottom_toBottomOf=“parent”

​ app:layout_constraintEnd_toEndOf=“parent”

​ app:layout_constraintStart_toStartOf=“parent”

​ app:layout_constraintTop_toTopOf=“parent”>

​ <ImageView

​ android:id=“@+id/profile”

​ android:layout_width=“60dp”

​ android:layout_height=“59dp”

​ android:layout_marginStart=“4dp”

​ android:layout_marginLeft=“4dp”

​ android:layout_marginTop=“16dp”

​ app:layout_constraintStart_toStartOf=“parent”

​ app:layout_constraintTop_toTopOf=“parent”

​ tools:srcCompat=“@tools:sample/avatars” />

​ <TextView

​ android:id=“@+id/title”

​ android:layout_width=“wrap_content”

​ android:layout_height=“wrap_content”

​ android:layout_marginStart=“24dp”

​ android:layout_marginLeft=“24dp”

​ android:layout_marginTop=“16dp”

​ android:text=“Title”

​ android:textColor=“#000000”

​ android:textSize=“24dp”

​ app:layout_constraintStart_toEndOf=“@+id/profile”

​ app:layout_constraintTop_toTopOf=“parent” />

​ <TextView

​ android:id=“@+id/status”

​ android:layout_width=“wrap_content”

​ android:layout_height=“wrap_content”

​ android:layout_marginStart=“2dp”

​ android:layout_marginLeft=“2dp”

​ android:layout_marginTop=“24dp”

​ android:layout_marginEnd=“2dp”

​ android:layout_marginRight=“2dp”

​ android:paddingLeft=“4dp”

​ android:paddingRight=“4dp”

​ android:text=“lorem posem lorem posemlorem posem lorem posemlorem pose posem”

​ android:textSize=“22dp”

​ app:layout_constraintEnd_toEndOf=“parent”

​ app:layout_constraintStart_toStartOf=“parent”

​ app:layout_constraintTop_toBottomOf=“@+id/profile” />

​ <ImageView

​ android:id=“@+id/image”

​ android:layout_width=“match_parent”

​ android:layout_height=“200dp”

​ android:layout_marginTop=“8dp”

​ app:layout_constraintEnd_toEndOf=“parent”

​ app:layout_constraintHorizontal_bias=“0.0”

​ app:layout_constraintStart_toStartOf=“parent”

​ app:layout_constraintTop_toBottomOf=“@+id/status”

​ tools:srcCompat=“@tools:sample/backgrounds/scenic” />

​ <TextView

​ android:id=“@+id/time”

​ android:layout_width=“wrap_content”

​ android:layout_height=“wrap_content”

​ android:layout_marginStart=“28dp”

​ android:layout_marginLeft=“28dp”

​ android:layout_marginTop=“10dp”

​ android:text=“TextView”

​ app:layout_constraintStart_toEndOf=“@+id/profile”

​ app:layout_constraintTop_toBottomOf=“@+id/title” />

​ </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


基于数据创建模型类:


这里我们使用了api,根据数据进行解析,因此它是modelclass.java



package com.saurabhjadhav.quantsapp_task;

public class model_class {

​ private String title;

​ private String profilepic;

​ private String image;

​ private String status;

​ private String time;

​ public model_class(String title, String profilepic, String image, String status, String time) {

​ this.title = title;

​ this.profilepic = profilepic;

​ this.image = image;

​ this.status = status;

​ this.time = time;

​ }

​ public String getTitle() {

​ return title;

​ }

​ public void setTitle(String title) {

​ this.title = title;

​ }

​ public String getProfilepic() {

​ return profilepic;

​ }

​ public void setProfilepic(String profilepic) {

​ this.profilepic = profilepic;

​ }

​ public String getImage() {

​ return image;

​ }

​ public void setImage(String image) {

​ this.image = image;

​ }

​ public String getStatus() {

​ return status;

​ }

​ public void setStatus(String status) {

​ this.status = status;

​ }

​ public String getTime() {

​ return time;

​ }

​ public void setTime(String time) {

​ this.time = time;

​ }

}


现在为回收器视图创建适配器类:


回收适配器.java



package com.saurabhjadhav.quantsapp_task;

import android.content.Context;

import android.text.format.DateUtils;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class recycler_adapter extends RecyclerView.Adapter<recycler_adapter.recyclerviewHolder> {

​ private Context mcontext;

​ private ArrayList<model_class> mmodelClassArrayList;

​ public recycler_adapter(Context context,ArrayList<model_class> modelClassArrayList)

​ {

​ mcontext=context;

​ mmodelClassArrayList=modelClassArrayList;

​ }

​ @NonNull

​ @Override

​ public recyclerviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

​ View v= LayoutInflater.from(mcontext).inflate(R.layout.item_feed,parent,false);

​ return new recyclerviewHolder(v);

​ }

​ @Override

​ public void onBindViewHolder(@NonNull recyclerviewHolder holder, int position) {

​ model_class currentitem=mmodelClassArrayList.get(position);

​ String title1=currentitem.getTitle();

​ String image1=currentitem.getImage();

​ String profile1=currentitem.getProfilepic();

​ String status1=currentitem.getStatus();

​ holder.status.setText(status1);

​ holder.title.setText(title1);

​ CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(

​ Long.parseLong(currentitem.getTime()),

​ System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);

​ holder.time2.setText(timeAgo);

​ Picasso.get().load(profile1).into(holder.profile2);

​ Picasso.get().load(image1).into(holder.Image2);

​ }

​ @Override

​ public int getItemCount() {

​ return mmodelClassArrayList.size();

​ }

​ public class recyclerviewHolder extends RecyclerView.ViewHolder{

​ public ImageView profile2,Image2;

​ public TextView title,status,time2;

​ public recyclerviewHolder(@NonNull View itemView) {

​ super(itemView);

​ profile2=itemView.findViewById(R.id.profile);

​ Image2=itemView.findViewById(R.id.image);

​ title=itemView.findViewById(R.id.title);

​ status=itemView.findViewById(R.id.status);

​ time2=itemView.findViewById(R.id.time);

​ }

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

temView.findViewById(R.id.image);

​ title=itemView.findViewById(R.id.title);

​ status=itemView.findViewById(R.id.status);

​ time2=itemView.findViewById(R.id.time);

​ }

[外链图片转存中…(img-RKumkxlh-1715801523422)]
[外链图片转存中…(img-rIwswpML-1715801523422)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 30
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值