Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView

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

时间: 2024-08-02 04:26:26

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

Eclipse 提示:This RelativeLayout layout or its LinearLayout parent is possibly useless

提示告诉你在viewgroup里面仅仅套了一个viewgroup 那eclipse就会觉得你最外面的是没用的:例如:告诉你RelativeLayout 外面有一个 LinearLayout,这个 LinearLayout是无效的. 知识点: Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器,其本身也是从View派生出来的.AndroidUI界面的一般结构可参见下面的示意图: 可见,作为容器

ie8兼容性_1:document.onclick失效

问题描述:在某些情况下,document.onclick在ie8浏览器下会不起作用. 问题原因:与某些第三方插件有关. 解决方案:把添加到document上的点击事件改为添加到body上的点击事件即可解决. eg:document.onclick=fun:改为document.body.onclick=fun: note:不建议使用禁用第三方插件的方式解决该类问题,因为我们的客户很多时候是不了解插件这方便的知识.

开发系列:03、Spark Streaming Custom Receivers(译)

Spark Streaming can receive streaming data from any arbitrary data source beyond the one’s for which it has in-built support (that is, beyond Flume, Kafka, files, sockets, etc.). This requires the developer to implement a receiver that is customized

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

下载 Custom Post Type UI>> 用Custom Post Type UI添加自定义文章类型对于新手来说最简单不过了,下载安装后,在插件栏启用一下,就可以开始添加文章类型了 添加后,可以看见左栏多了“产品”: 你还可以创建完类型后,在CPT UI菜单下的import/Export->Get Code的这段代码贴到functions.php中,这样不需要安装该插件也可以实现文章类型的定义.

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

从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stream的两个独立端点,而Flow处于stream Source和Sink中间可能由多个通道式的节点组成,每个节点代表某些数据流元素转化处理功能,它们的链接顺序则可能代表整体作业的流程.一个完整的数据流(可运行数据流)必须是一个闭合的数据流,即:从外表上看,数据流两头必须连接一个Source和一个Sin

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

本文将介绍handler处理器和自定义opener,更多内容请参考:python学习指南 opener和handleer 我们之前一直使用的是urllib2.urlopen(url)这种形式来打开网页,它是一个特殊的opener(也就是模块帮我们建好的),opener是urllib2.OpenerDirectory的实例. 但是基本的urlopen()方法不支持代理.cookie等其他的HTTP/HTTPS高级功能.所以要支持这些功能: 使用相关的Handler处理器来创建特定功能的处理器对象:

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

opener和handleer 我们之前一直使用的是urllib2.urlopen(url)这种形式来打开网页,它是一个特殊的opener(也就是模块帮我们建好的),opener是urllib2.OpenerDirectory的实例. 但是基本的urlopen()方法不支持代理.cookie等其他的HTTP/HTTPS高级功能.所以要支持这些功能: 使用相关的Handler处理器来创建特定功能的处理器对象: 然后通过urllib2.build_opener()方法来使用这些处理器对象,创建自定义

设计模式笔记之三:Android DataBinding库(MVVM设计模式)

本博客转自郭霖公众号:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650236908&idx=1&sn=9e53f42e18a81795ef0cfe6fe3959ec2&scene=24&srcid=0910cK3vXJpNzY0CO28i1Qhs#wechat_redirect 什么是MVVM 说到DataBinding,就有必要先提起MVVM设计模式.Model–View–ViewModel(MVV

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

1.Hello, Kotlin Bugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明出处. 1.1 Kotlin的身世 写了许久Java,有没有发现其实你写了太多冗余的代码? 后来你体验了一下Python,有没有觉得不写分号的感觉真是超级爽? 你虽然勤勤恳恳,可到头来却被NullPointerException折磨的死去活来,难道就没有受够这种日子么? 直到有一天你发现自己已经写了好几十万行代