Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView

 
The post "Implement Gallery-like HorizontalScrollView" and "Implement custom LinearLayout for Gallery-like HorizontalScrollView" explain how to implement Gallery-like HorizontalScrollView.

In this post, we will implement OnClickListener for the ImageView(s) to handle user click.

Modify MyHorizontalLayout.java from the exercise "Implement custom LinearLayout for Gallery-like HorizontalScrollView" to call setOnClickListener() when create ImageView, in getImageView() method. For "Implement Gallery-like HorizontalScrollView" without custom LinearLayout, you can do the same in insertPhoto() method.

package com.example.androidhorizontalscrollviewgallery;
import java.util.ArrayList; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast; public class MyHorizontalLayout extends LinearLayout { Context myContext;
ArrayList<String> itemList = new ArrayList<String>(); public MyHorizontalLayout(Context context) {
super(context);
myContext = context;
} public MyHorizontalLayout(Context context, AttributeSet attrs) {
super(context, attrs);
myContext = context;
} public MyHorizontalLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
myContext = context;
} void add(String path){
int newIdx = itemList.size();
itemList.add(path);
addView(getImageView(newIdx));
} ImageView getImageView(final int i){
Bitmap bm = null;
if (i < itemList.size()){
bm = decodeSampledBitmapFromUri(itemList.get(i), 220, 220);
} ImageView imageView = new ImageView(myContext);
imageView.setLayoutParams(new LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bm); imageView.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
Toast.makeText(myContext,
"Clicked - " + itemList.get(i),
Toast.LENGTH_LONG).show();
}}); return imageView;
} public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null; // First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options); return bm;
} public int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
} return inSampleSize;
} }

Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView的更多相关文章

  1. Android零基础入门第25节:最简单最常用的LinearLayout线性布局

    原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...

  2. WordPress主题制作教程10:添加文章类型插件Custom Post Type UI

    下载 Custom Post Type UI>> 用Custom Post Type UI添加自定义文章类型对于新手来说最简单不过了,下载安装后,在插件栏启用一下,就可以开始添加文章类型了 ...

  3. Record:逻辑分区下新建简单卷后其他卷被删除

    上图是恢复后的磁盘情况,恢复前的情况没有截图. 事情是这样:扩展分区中原本有4个逻辑分区.想将其中一个分区(MySpace,第一个分区)压缩出部分空间新建一个分区.经过 压缩卷->新建简单卷 后 ...

  4. Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages

    从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stre ...

  5. python爬虫(六)_urllib2:handle处理器和自定义opener

    本文将介绍handler处理器和自定义opener,更多内容请参考:python学习指南 opener和handleer 我们之前一直使用的是urllib2.urlopen(url)这种形式来打开网页 ...

  6. Nodejs事件引擎libuv源码剖析之:句柄(handle)结构的设计剖析

    声明:本文为原创博文,转载请注明出处. 句柄(handle)代表一种对持有资源的索引,句柄的叫法在window上较多,在unix/linux等系统上大多称之为描述符,为了抽象不同平台的差异,libuv ...

  7. 原生js:click和onclick本质的区别(转https://www.cnblogs.com/web1/p/6555662.html)

    原生javascript的click在w3c里边的阐述是DOM button对象,也是html DOM click() 方法,可模拟在按钮上的一次鼠标单击. button 对象代表 HTML 文档中的 ...

  8. python+selenium自动化软件测试(第2章):WebDriver API

    2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...

  9. 【Bugly 技术干货】Android开发必备知识:为什么说Kotlin值得一试

    1.Hello, Kotlin Bugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明出处. 1. ...

随机推荐

  1. 实现password框中显示文字提示的方式

    其实实际上实现中并不能让password中显示文字提示,但是我们在工作中有这样的需求,当没输入东西的时候,框内有提示输入密码,但是当输入东西的时候又显示的是*号,那么是如何实现的呢?其实原理很简单,就 ...

  2. SQL 存储过程 解析XML

    第一种说明: 我看过这样一篇文章,如下 在SQL   Server2005中,微软延续了   2000中一个特性(即支持XML类型的数据),并加强了对XML   数据列.XML变量以及XML索引的支持 ...

  3. ElasticSearch部署安装

    测试版本:elasticsearch-5.1.1 1.Windows环境下安装(win10系统) 1)解压elasticsearch-5.1.1.zip. 2)执行elasticsearch.bat启 ...

  4. Entity Framework – (复数)Plural and (单数)Singular 表名Table names

    By default, the Entity Framework will assume that all of the names of your tables in your database a ...

  5. linux 系统负载高 如何检查

    1:load Average 1.1:什么是Load?什么是Load Average?    Load 就是对计算机干活多少的度量(WikiPedia:the system Load is a mea ...

  6. LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0

    https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...

  7. 1171. Lost in Space

    http://acm.timus.ru/problem.aspx?space=1&num=1171 一天的时间,WA了N遍,居然是因为数组开小了呀,我勒个去!鄙视自己...... 我是从第 1 ...

  8. python 版 mldivide matlab 反除(左除)《数学建模算法与程序》Python笔记

    今天在阅读数学建模的时候看到了差分那章 其中有一个用matlab求线性的代码,这里我贴出来 这里我送上 Python代码 In [39]: import numpy as np ...: from s ...

  9. JavaScript toFixed 用法

    在JS中进行乘法运算时 我们又是会遇到一下情况 <input type="text" id="num1" value="" /> ...

  10. web前端从0开始--1

    博主以前没接触过web前端,最近刚开始学习. 在学习的同时,希望能不断整理总结.于是便有了此博客. 博主技术浅薄,并且第一次写此类博客,希望各位大牛能多多保函. 好了废话不多说了,开始正文. web前 ...