前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之StackView用法和遇到的坑分析

Android开发之StackView用法和遇到的坑分析

作者头像
砸漏
发布2020-11-05 10:47:01
6190
发布2020-11-05 10:47:01
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例分析了Android开发之StackView用法和遇到的坑。分享给大家供大家参考,具体如下:

关于StackView网上已经有很多内容了

这里我着重将一些使用过程中遇到的坑吧

先看下效果,和很多人一样

很多人加完图片后发现图片不显示,这里可能有两个原因:

一、直接闪退,然后报错。一般会有头这么一句话:

Failed to allocate a 74649612 byte allocation with 16765728 free bytes and 59MB until OOM

提示一个很明白了,内存溢出,具体原因是关于Android的内存分配机制的这里就不详细讲了

这不经事StackView常见的问题,所有添加图片的活动都可能发生

怎么办呢?主要有两种办法:

1.暴力直接,用图片转换器(或者直接用windows自带画图工具)将图进行压缩。但很明显治标不治本。

2.将图片转为Bitmap,然后再将其质量和大小进行压缩。

二、加完图片后发现图片不显示

这个一般来说是代码本身的问题

检查下你List对象和Adapter对象的一些名字是否一致

这里以MainActivity为例(改编自疯狂Android)

代码语言:javascript
复制
public class MainActivity extends Activity {
  StackView stackView ;
  int[] imageIds = new int[]{
      R.drawable.a0,R.drawable.a00,R.drawable.a1,R.drawable.a02,
      R.drawable.a1,R.drawable.a2, R.drawable.a3
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    stackView = (StackView) findViewById(R.id.mStackView);
    //创建一个list 对象 元素是MAP
    List<Map<String,Object   listItems =
        new ArrayList<Map<String,Object  ();
    for( int i = 0 ; i < imageIds.length ; i++ ){
      Map<String,Object  listItem = new HashMap<String, Object ();
      listItem.put("image",imageIds[i]);
      listItems.add(listItem);
    }
    //创建一个Simple Adapter
    SimpleAdapter simpleAdapter = new SimpleAdapter(this,
        listItems,
        R.layout.photo,
        new String[]{"image"},
        new int[]{R.id.image1});
    stackView.setAdapter(simpleAdapter);
  }
  public void prev(View source){
    //显示上一个组件
    stackView.showPrevious();
  }
  public void next(View source){
    //显示下一个组件
    stackView.showNext();
  }
}

注意检查一下listItems和simpleAdapter用来存放图片的变量名是否一致

比如我这儿是叫做image

这是比较常见的一种错误,如果是其他错误则需要大家再去Google一下了

鉴于很多同学表示不知道cell (我这里叫做photo)这个layout是什么

其实就是一个很简单的layout 向自定义listView等等,很多时候都得用上这种自定义的layout

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"? 
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  <ImageView
    android:id="@+id/image1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="fitXY"
    android:layout_gravity="center"/ 
</LinearLayout 

我遇到的坑大概就这些了,最后附上布局文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"? 
<RelativeLayout 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/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity" 
  <StackView
    android:id="@+id/mStackView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:loopViews="true"/ 
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal" 
    <Button
      android:id="@+id/button01"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:onClick="prev"
      android:text="上一个" / 
    <Button
      android:id="@+id/button02"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:onClick="next"
      android:text="下一个" / 
  </LinearLayout 
</RelativeLayout 

希望本文所述对大家Android程序设计有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档