how to get the particular sub view id from the view in android?

半世苍凉 提交于 2019-12-12 04:26:01

问题


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);

回答1:


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;
        }
    });



回答2:


If you only want to identify the view which is touched, you can add listeners to your dynamic image views also. Like below

spotimage.setOnClickListener(new OnClickListener() {`           
                @Override
                public void onClick(View v) {

                }
            });

and in the onClick methord you can write code specific to each image view, or if you strictly want to set the id for image views, you can use
spotimage.setId(1)
Id can be any integer value, but you have to make sure no conflict will occur with other id values. and in any listener like OnClickListenet, you can check the image view id by
int temp = view.getId();



来源:https://stackoverflow.com/questions/25841914/how-to-get-the-particular-sub-view-id-from-the-view-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!