我需要檢測哪個是我應用程序中點擊的圖像。我嘗試了很多方法來做到這一點。我從整數數組中生成Imageview對象,我檢測到一個圖像被點擊,但沒有被點擊。如何檢測哪個圖像被點擊
我需要把兩排圖像,但我需要這個圖像的運動同時展位,而不是單獨。我做了一個horizontalScrollView兩個佈局,一個垂直一個水平。我在垂直方向放置了兩個圖像,並通過循環將這個佈局發送到水平佈局。
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
爲您在java中動態添加的每張圖片添加id –