Android Gridview和ImageView

时间:2016-04-30 11:15:43

标签: android android-gridview

我的XML文件中有一个gridview,其中存储了多个图像。单击单个图像时,将出现带有图像视图的警告对话框。我想在该imageview上显示特定的点击图像。我怎样才能做到这一点?

gv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            final picturebean pb=ilist.get(arg2);
            AlertDialog.Builder abpic=new AlertDialog.Builder(ShowPicActivity.this);
            abpic.setTitle("Pic View");
            abpic.setCancelable(true);
            LayoutInflater li=getLayoutInflater();
            View vi = li.inflate(R.layout.picmenu, null);
            abpic.setView(vi);
            ImageView iv=(ImageView)vi.findViewById(R.id.imageView1);

2 个答案:

答案 0 :(得分:0)

只需在onItemClickListener(...)中调用此方法即可。我认为你在picturebean中有数据集。所以在这里你可以得到你的imagePath / Bitmap。

public void showDialogImage(String imageName //Bitmap img) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setPositiveButton("Nice Image", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    }).setNegativeButton("Not like", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    final AlertDialog dialog = builder.create();
    LayoutInflater inflater = getLayoutInflater();
    View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
    dialog.setView(dialogLayout);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.show();

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface d) {
            ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
           //If you have imagepath than do like this else you set the bitmap directly to the imageView. 
            Bitmap icon = BitmapFactory.decodeResource(imageName);
            float imageWidthInPX = (float) image.getWidth();

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                    Math.round(imageWidthInPX * (float) icon.getHeight() / (float) icon.getWidth()));
            image.setLayoutParams(layoutParams);

        }
    });
}

这是对话框的xml。

   <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">

  <ImageView
    android:id="@+id/goProDialogImage"
    android:layout_width="wrap_content"
    android:layout_height="350dp"
    android:src="@drawable/whygoprodialogimage"/>
</LinearLayout>

答案 1 :(得分:0)

这里很简单就是如何做到这一点

gv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        final picturebean pb=ilist.get(arg2);
        AlertDialog.Builder abpic=new AlertDialog.Builder(ShowPicActivity.this);
        abpic.setTitle("Pic View");
        abpic.setCancelable(true);
        LayoutInflater li=getLayoutInflater();
        View vi = li.inflate(R.layout.picmenu, null);
        abpic.setView(vi);
        ImageView iv=(ImageView)vi.findViewById(R.id.imageView1);
        iv.setImageResource(listofimages[position]);
        // Here listofimages is the List or Arraylist from where you adding images to gridview