0

我需要检测在我的应用程序中哪个是点击的图像。我尝试了很多方法来做到这一点。我从一个整数数组动态地生成 Imageview 对象,我检测到一个图像被点击但不是哪个。

我需要放置两行图像,但我需要这些图像的移动同时进行,而不是单独进行。我制作了一个带有两种布局的horizo​​ntalScrollView,一种是垂直的,一种是水平的。我将两个图像视图垂直放置并通过循环将此布局发送到水平布局。

XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/layoutContenedor"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingBottom="@dimen/activity_vertical_margin"
         android:paddingLeft="@dimen/activity_horizontal_margin"
         android:paddingRight="@dimen/activity_horizontal_margin"
         android:paddingTop="@dimen/activity_vertical_margin"
         tools:context=".MainActivity" >

   <HorizontalScrollView
    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

       <LinearLayout
            android:id="@+id/linearLayoutHorizontal"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

       </LinearLayout>

     </HorizontalScrollView>

    </LinearLayout>

我的活动:

    public class MainActivity extends Activity implements OnClickListener{

    private LinearLayout layoutDinamicoHorizontal;
    private LinearLayout layoutGlobal;

    private Integer[] listaPeliculas = { R.drawable.android1, R.drawable.android2,
            R.drawable.android3, R.drawable.android4, R.drawable.android5, R.drawable.android6,
            R.drawable.android7, R.drawable.android8, R.drawable.android9, R.drawable.android10,
            R.drawable.android11, R.drawable.android12, R.drawable.android13, R.drawable.android14,
            R.drawable.android15, R.drawable.android16, R.drawable.android17, R.drawable.android18,
            R.drawable.android19, R.drawable.android20, R.drawable.android21, R.drawable.android22,
            R.drawable.android23, R.drawable.android24, R.drawable.android25, R.drawable.android26,
            R.drawable.android27, R.drawable.android28, R.drawable.android29, R.drawable.android30};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // layout in variable of LinearLayout type
        layoutDinamicoHorizontal = (LinearLayout) findViewById(R.id.linearLayoutHorizontal);
        layoutGlobal = (LinearLayout) findViewById(R.id.layoutContenedor);


        layoutGlobal.setBackgroundResource(R.drawable.fondodepantallacarrusel);

        // create ImageView
        ImageView imageViewDinamicoArriba;
        ImageView imageViewDinamicoAbajo;

        for (int i = 0; i < 30; i = i+2){

            // Creamos a new object from ImageView class
            imageViewDinamicoArriba = new ImageView(this);
            imageViewDinamicoAbajo = new ImageView(this);

            // padding to objects of ImageView class
            imageViewDinamicoArriba.setPadding(10, 15, 10, 15);
            imageViewDinamicoAbajo.setPadding(10, 15, 10, 15);


            Drawable imagenArriba = getResources().getDrawable(listaPeliculas[i]);
            Drawable imagenAbajo = getResources().getDrawable(listaPeliculas[i+1]);

            // Se los pasamos a los objetos
            imageViewDinamicoArriba.setImageDrawable(imagenArriba);
                    imageViewDinamicoAbajo.setImageDrawable(imagenAbajo);



            LinearLayout vertical = new LinearLayout(this);
                    vertical.setOrientation(LinearLayout.VERTICAL);

                    // Añadimos las vistas al objeto vertical
                    vertical.addView(imageViewDinamicoArriba);  
                    //vertical.setOnClickListener(this);

                    vertical.addView(imageViewDinamicoAbajo);

            vertical.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // I Tryed with every metods of View 
                    Log.i("galeria", "Imagen: " + v.getId());


                }
            });


                    // Add vertical Layout to horizontal Layout 
            layoutDinamicoHorizontal.addView(vertical);
        } // End for        
     } // End onCreate

      @Override
      public void onClick(View v) {

      }

        } // End Activity
4

2 回答 2

0

imageViewDinamicoArriba.setTag(new Integer(i))或者 imageViewDinamicoAbajo.setTag(new Integer(i))

然后你会得到你的索引Log.i("galeria", "Imagen: " + v.getTag());

int index = (Integer) v.getTag();

于 2013-11-15T08:12:37.867 回答
0

当您动态创建视图时,默认情况下不会添加 id。如果要使用 id 进行过滤,则应手动添加 id:

imageViewDinamicoArriba.setId(i);

有关详细信息,请查看 setId 方法文档

希望能帮助到你。

于 2013-11-15T08:24:30.427 回答