最近学习android开发,感触颇多,和网站开发对比,还是有很大的差距,在这里记录一下。

android listview展示图片

在网站开发上,展示图片非常简单,一个HTML img标签就搞定,加上服务器无非就是在view里动态展示,或者用ajax动态获取,或者用vue等动态获取js展示。

但在android上就非常麻烦,先要把网络图片下载,转换成流文件,再转换成drawable资源文件才能在app上显示,里面的xml各种配置,对于一个写惯HTML的人来说很烦。

主要步骤。

假设在一个Listshow的二级页面展示,

首先要准备的东西,

Adapter,Adapter是连接后端数据和前端显示的适配器接口,

一些自定义的类,下载图片的类imageloader,缓存的类memorycache,存文件的类filecache

xml文件,主列表的xml,listview的xml,每次下载图片覆盖的drawable资源xml

具体代码:

主要的activity

  1. package com.example.huibeb;
    import android.os.Bundle;
    import android.widget.ListView;
    import androidx.appcompat.app.AppCompatActivity;
    import com.example.huibeb.model.ListmAdapter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class Listshow extends AppCompatActivity {
    private List<Map<String, Object>> cats = new ArrayList<Map<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listshow);
    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 4; i++) {
    HashMap<String, String> map = new HashMap<String, String>();

    map.put("listname", "暹罗猫"+i);
    map.put("listimgs", "http://img.bjyiyoutech.com/appimage/5de78277d60ee.jpg");
    songsList.add(map);
    }

    ListmAdapter adapter = new ListmAdapter(this,songsList);
    ((ListView) findViewById(R.id.listlist)).setAdapter(adapter);
    }
    }

  1. ListmAdapterAdapter是连接后端数据和前端显示的适配器接口,
  1. package com.example.huibeb.model;
    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    import androidx.annotation.NonNull;
    import com.example.huibeb.R;
    import com.example.huibeb.help.ImageLoader;
    import java.util.ArrayList;
    import java.util.HashMap;

    public class ListmAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; //用来下载图片的类,后面有介绍

    public ListmAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d; //赋值列表
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
    }
    public int getCount() {
    return data.size();
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
    vi = inflater.inflate(R.layout.lists, null);

    TextView listname = (TextView)vi.findViewById(R.id.listname); // 标题
    ImageView listimg=(ImageView)vi.findViewById(R.id.listimg); // 缩略图

    HashMap<String, String> listm = new HashMap<String, String>();
    listm = data.get(position);
    // 设置ListView的相关值
    String aa = listm.get("listname");// 标题
    listname.setText(listm.get("listname"));// 缩略图
    imageLoader.DisplayImage(listm.get("listimgs"), listimg);//下载并返回图片资源
    return vi;

    }

    }
    memorycache缓存MemoryCache
  1. package com.example.huibeb.help;
    import android.graphics.Bitmap;

    import java.lang.ref.SoftReference;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;

    public class MemoryCache {
    private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());//软引用

    public Bitmap get(String id){
    if(!cache.containsKey(id))
    return null;
    SoftReference<Bitmap> ref=cache.get(id);
    return ref.get();
    }

    public void put(String id, Bitmap bitmap){
    cache.put(id, new SoftReference<Bitmap>(bitmap));
    }

    public void clear() {
    cache.clear();
    }

    }
    file缓存FileCache
  1. package com.example.huibeb.help;
    import android.content.Context;

    import java.io.File;

    public class FileCache {

    private File cacheDir;

    public FileCache(Context context){
    //找一个用来缓存图片的路径
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cacheDir=new File(context.getExternalCacheDir(),"huibenb");
    else
    cacheDir=context.getCacheDir();
    if(!cacheDir.exists())
    cacheDir.mkdirs();
    }

    public File getFile(String url){

    String filename=String.valueOf(url.hashCode());
    File f = new File(cacheDir, filename);
    return f;

    }

    public void clear(){
    File[] files=cacheDir.listFiles();
    if(files==null)
    return;
    for(File f:files)
    f.delete();
    }

    }

    xml文件
    activity布局文件 activity_listshow.xml
  1. <?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="match_parent"
    tools:context=".Listshow">

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="1dp"
    tools:layout_editor_absoluteY="1dp">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
    android:id="@+id/button5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    </LinearLayout>

    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
    android:id="@+id/button5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    </LinearLayout>
    </LinearLayout>

    <ListView
    android:id="@+id/listlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    </LinearLayout>
    </ScrollView>
    </androidx.constraintlayout.widget.ConstraintLayout>
    listView内容文件lists.xml
  1. <?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="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
    android:id="@+id/listimg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

    <TextView
    android:id="@+id/listname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="TextView" />
    </LinearLayout>

    </LinearLayout>
    </LinearLayout>
    每次下载图片要替换的drawable资源文件listimgtest.xml,注意这个要放在drawable文件夹下,@drawable/ihblogo是一个jpg图片,默认加载的图片请自行添加
  1. <?xml version="1.0" encoding="utf-8"?>
    <bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ihblogo"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:gravity="center"
    android:mipMap="false"
    android:tileMode="disabled"
    />
  1.  

android listview展示图片的更多相关文章

  1. Android 控件使用教程(一)—— ListView 展示图片

    起因 最近在看一些开源项目时,经常看到了RecyclerView,这是安卓5.0推出的一个新的控件,可以代替传统的ListView,已经这么久了还没有用过,所以决定试一试.另外在做这个的工程中看到了另 ...

  2. Android ListView从网络获取图片及文字显示

    上一篇文章说的是ListView展示本地的图片以及文本,这一篇说一下如何从网络获取图片以及文本来显示.事实上,一般是先获取Josn或sml数据,然后解释显示.我们先从网上获取xml,然后对其进行解析, ...

  3. Android ListView滑动过程中图片显示重复错乱闪烁问题解决

    最新内容建议直接访问原文:Android ListView滑动过程中图片显示重复错乱闪烁问题解决 主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及L ...

  4. Android ListView分页加载时图片显示问题

    场景:Android ListView需要分页加载,每个item中会有图片,图片又是从网络下载的. 问题:在滑动加载下一页时,上一页的图片明明已经下载完成了,但是无法显示出来. Bug重现: 1,加载 ...

  5. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  6. Android在ListView显示图片(重复混乱闪烁问题)

    Android在ListView显示图片(重复混乱闪烁问题) 1.原因分析 ListView item缓存机制: 为了使得性能更优,ListView会缓存行item(某行相应的View). ListV ...

  7. Xamarin.Android 调用Web Api(通过ListView展示远程获取的数据)

    xamarin.android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin.android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...

  8. Android数据展示之ListView

    Android应用程序中经常需要使用ListView展示数据,一个ListView通常将数据填充到布局文件中,在填充的过程中需要知道连接数据与ListView的适配器.适配器是一个连接数据和Adapt ...

  9. Android微信九宫格图片展示控件

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/214 Android微信九宫格图片展示控件 半年前,公司产 ...

随机推荐

  1. 【二食堂】Alpha - 事后分析

    事后分析 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? Alpha阶段要解决的问题是:根据用户标注的信息完成知识图谱的生成渲染.要解决的问题定义得比较 ...

  2. Java High Level REST Client 使用地理位置查询

    Java High Level REST Client 使用地理位置查询 一.需求 二.对应的query语句 三.对应java代码 1.引入 jar 包 2.创建 RestHighLevelClien ...

  3. matplotlib散点图

    我们常用的统计图如下: 1.学会绘制散点图 一个小demo: 假设通过爬虫你获取到了北京2016年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间(天)变化的某种规 ...

  4. 聊了聊宏内核和微内核,并吹了一波 Linux

    看这里!!!https://mp.weixin.qq.com/s?__biz=MzI0ODk2NDIyMQ==&mid=2247494048&idx=1&sn=cacfc6a4 ...

  5. 如何利用SimpleNVR建立全天候远程视频监控系统

    随着社会经济的发展,5G.AI.云计算.大数据.物联网等新兴技术迭代更新的驱动下,传统的安防监控早已无法满足我们的需求.那么我们如何建立全天候远程视频监控系统来替代传统监控呢?如何进一步优化城市管理. ...

  6. 『学了就忘』Linux基础命令 — 31、grep命令和通配符

    目录 1.grep命令介绍 2.find命令和grep命令的区别(重点) (1)find命令 (2)grep命令 3.通配符与正则表达式的区别 (1)通配符: (2)正则表达式: 1.grep命令介绍 ...

  7. [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/

    关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...

  8. .net core api 请求实现接口幂等性

    简单实现接口幂等性,根据参数的hascode实现: 参数介绍  WaitMillisecond : 请求等待毫秒数 CacheMillisecond:请求结果缓存毫秒数 参数具体使用场景 WaitMi ...

  9. celery ValueError: invalid literal for int() with base 10: '26379;sentinel'

    celery使用redis sentinel作为broker的时候,因为redis sentinel配置字符串格式解析报错 ValueError: invalid literal for int() ...

  10. VM的三种连接方式(转载)

    概述: VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模 ...