我需要检测应用程序中点击的图像。我尝试了很多方法来做到这一点。我从一个整数数组生成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
答案 0 :(得分:0)
imageViewDinamicoArriba.setTag(new Integer(i))
或
imageViewDinamicoAbajo.setTag(new Integer(i))
然后,您将获得Log.i("galeria", "Imagen: " + v.getTag());
int index = (Integer) v.getTag();
答案 1 :(得分:0)
动态创建View时,默认情况下不会添加id。如果要使用ID进行过滤,则应手动添加id:
imageViewDinamicoArriba.setId(i);
了解更多信息View setId method documentation。
希望它有所帮助。