将ImageView中的特定图像链接到Url?

时间:2016-01-24 22:50:09

标签: android imageview uri onitemclicklistener

我有一个ImageView,我正在循环图像。例如,这是我的代码:

//Home screen Slideshow ----->
final int[] imageArray = {R.drawable.image1, R.drawable.image2, R.drawable.image3};


        for (int j = 0; j < 10; j++) {
            final Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                int i = 0;

                public void run() {
                    imageview.setImageResource(imageArray[i]);
                    i++;
                    if (i > imageArray.length - 1) {
                        i = 0;
                    }
                    handler.postDelayed(this, 19000);  //for interval...

                }
            };
            handler.postDelayed(runnable, 19000); //for initial delay..
            //End Slideshow ----->
        }

我知道如何使用以下代码将整个ImageView链接到URL:

imageview.setOnClickListener(new View.OnClickListener() {
            public void onClick (View v){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("http://google.com"));
            startActivity(intent);    }

如果我尝试将每个图片链接到google.com,那就太棒了。但实际上,我想要做的是将滑块中的每个不同图像链接到不同的WebPage。我怎么能做到这一点?

编辑:

//Home screen Slideshow ----->
        final int[] imageArray = {R.drawable.image1, R.drawable.image2, R.drawable.image3,
                R.drawable.image4, R.drawable.image5, R.drawable.image6, R.drawable.image11, R.drawable.image7,
                R.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image4,
                R.drawable.image2, R.drawable.image6, R.drawable.image12, R.drawable.image8, R.drawable.image3,
                R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12};


        for (int j = 0; j < 10; j++) {
            final Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                int i = 0;

                public void run() {
                    imageview.setImageResource(imageArray[i]);
                    i++;
                    if (i > imageArray.length - 1) {
                        i = 0;
                    }
                    handler.postDelayed(this, 19000);  //for interval...


                    if (imageArray[i]==1) {
                    imageview.setOnClickListener(new View.OnClickListener() {

                                                   @Override
                                                   public void onClick(View arg0) {

                                                       Intent intent = new Intent(context, Restaurants.class);

                                                       startActivity(intent);
                                                   }
                                               }

                    );
                }

                }

            };
            handler.postDelayed(runnable, 19000); //for initial delay..
            //End Slideshow ----->
        }

2 个答案:

答案 0 :(得分:1)

尝试在runnable

中放置onClickListener

答案 1 :(得分:0)

试试这个

onClick(View v){
   Integer i=v.getTag();//getId()
    switch(i){
      case R.drawable.image :
        //do things
       break;
    }

  }