如何从Android中的视图中的特殊子视图id? [英] how to get the particular sub view id from the view in android?

查看:134
本文介绍了如何从Android中的视图中的特殊子视图id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含的FrameLayout 动态视图,它包含的ImageView 秒。现在,当我触摸的框架布局的特定图像,我想知道的ID 的ImageView

所以,这里是我的问题:


  1. 如何设定此ID的的ImageView


  2. 我如何能识别特定的的ImageView 感动?


下面是code的示例代码段:

 为(INT J = 0; J< _pageslist.size(); J ++){
    的FrameLayout帧=新的FrameLayout(HLActivity.this);
    的LayoutParams PARAMS =新FrameLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    frame.setLayoutParams(PARAMS);    ImageView的mainimage =新ImageView的(HLActivity.this);    mainimage.setImageBitmap(ReusableMethods.getBitmapFromURL(_pageslist.get(J)
            。.getThumbnail()的toString()));
    mainimage.setScaleType(ScaleType.FIT_XY);
    mainimage.setLayoutParams(PARAMS);    frame.addView(mainimage,则params);    如果(_pageslist.get(j)条.isHasspots()){
        的System.out.println(_pageslist.get(j)条.isHasspots());
        的System.out.println(_pageslist.get(j)条.getSPOTS());        ArrayList的<&热点GT; hotspots_array = _pageslist.get(J).getSPOTS();
        的for(int i = 0; I< hotspots_array.size();我++){
            热点的热点= hotspots_array.get(ⅰ);
            的System.out.println(热点::+ hotspot.getType());            ImageView的spotimage =新ImageView的(HLActivity.this);
            spotimage.setBackgroundColor(Color.parseColor(#88676767));            浮法运行startx,starty,endx,恩迪;
            运行startx =(浮点)(Float.parseFloat(hotspot.getX())* IVW)/ 100;
            starty =(浮点)(Float.parseFloat(hotspot.getY())* IVH)/ 100;
            endx =(浮点)((Float.parseFloat(hotspot.getX())+
                    Float.parseFloat(hotspot.getWidth()))* IVW)/ 100;
            恩迪=(浮点)((Float.parseFloat(hotspot.getY())+
                    Float.parseFloat(hotspot.getHeight()))* IVH)/ 100;            PARAMS =新FrameLayout.LayoutParams(
                    (中间体)((Float.parseFloat(hotspot.getWidth())* IVW)/ 100),
                    (中间体)((Float.parseFloat(hotspot.getHeight())* IVH)/ 100));            params.leftMargin =(INT)startx的;
            params.topMargin =(INT)starty;            frame.addView(spotimage,则params);
        }
    }
    _view.add(架);
}适配器=新ViewPagerAdapter(HLActivity.this,_view,
        _pageslist,IVW,IVH,getStatusBarHeight());
viewPager.setAdapter(适配器);


解决方案

您必须ontouch监听器设置为你的形象:

  yourImage.setOnTouchListener(新OnTouchListener(){        @覆盖
        公共布尔onTouch(视图V,MotionEvent ARG1){
            //这是你的ID,你可以通过它
            v.getId()
            // TODO自动生成方法存根
            返回false;
        }
    });

I created a dynamic view that contains FrameLayout, and it contains ImageViews. Now, when I touch the particular image on frame layout, I want know the ID of the ImageView.

So, here are my questions:

  1. How can I set the ID for the ImageView?

  2. How can I recognize particular ImageView is touched?

Here is the sample snippet of the code:

for (int j = 0; j < _pageslist.size(); j++) {
    FrameLayout frame = new FrameLayout(HLActivity.this);
    LayoutParams params = new FrameLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    frame.setLayoutParams(params);

    ImageView mainimage = new ImageView(HLActivity.this);

    mainimage.setImageBitmap(ReusableMethods.getBitmapFromURL(_pageslist.get(j)
            .getThumbnail().toString()));
    mainimage.setScaleType(ScaleType.FIT_XY);
    mainimage.setLayoutParams(params);

    frame.addView(mainimage, params);

    if (_pageslist.get(j).isHasspots()) {
        System.out.println(_pageslist.get(j).isHasspots());
        System.out.println(_pageslist.get(j).getSPOTS());

        ArrayList<Hotspot> hotspots_array = _pageslist.get(j).getSPOTS();
        for (int i = 0; i < hotspots_array.size(); i++) {
            Hotspot hotspot = hotspots_array.get(i);
            System.out.println("hotspot :: " + hotspot.getType());

            ImageView spotimage = new ImageView(HLActivity.this);
            spotimage.setBackgroundColor(Color.parseColor("#88676767"));

            float startx, starty, endx, endy;
            startx = (float) (Float.parseFloat(hotspot.getX()) * ivw) / 100;
            starty = (float) (Float.parseFloat(hotspot.getY()) * ivh) / 100;
            endx = (float) ((Float.parseFloat(hotspot.getX()) + 
                    Float.parseFloat(hotspot.getWidth())) * ivw) / 100;
            endy = (float) ((Float.parseFloat(hotspot.getY()) + 
                    Float.parseFloat(hotspot.getHeight())) * ivh) / 100;

            params = new FrameLayout.LayoutParams(
                    (int) ((Float.parseFloat(hotspot.getWidth()) * ivw)/100),
                    (int) ((Float.parseFloat(hotspot.getHeight()) * ivh)/100));

            params.leftMargin = (int) startx;
            params.topMargin = (int) starty;

            frame.addView(spotimage, params);
        }
    }
    _view.add(frame);
}

adapter = new ViewPagerAdapter(HLActivity.this, _view,
        _pageslist, ivw, ivh, getStatusBarHeight());
viewPager.setAdapter(adapter);

解决方案

you have to set ontouch listener to your image:

yourImage.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            // this is your id you can pass it
            v.getId()
            // TODO Auto-generated method stub
            return false;
        }
    });

这篇关于如何从Android中的视图中的特殊子视图id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆