質問

I am using Imageview 2 times in my MainActivity.java class by using LayoutInflater.In that activity i am inflate so many layouts by using scroll every thing works fine.But this imageview is not working properly.When user click on imageview it shows popup.It works when i am show first time inflate.It's not working second time inflate.

In my main activity contain edittext.when user enter High in edittext i want to show 2 imageview times.If user enter Low i want to show single imageview.One imageview shows top of the screen and one shows bottom of the screen.

Here my bit of code.

This is my imageview.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/attach_photo_layout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp" >

    <TextView
        android:id="@+id/attach_photo_titile_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="5dp"
        android:text="@string/attach_photo_to_job"
        android:textColor="@android:color/white" />

    <ImageView
        android:id="@+id/attach_photo_camera_ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/attach_photo_titile_textView"
        android:contentDescription="@string/app_name"
        android:clickable="true"
        android:src="@drawable/image_placeholder" />
</RelativeLayout>

I am using this layout in my main.xml by using .

And i am call that imageview.xml layout in My MainActivity.java by using LayoutInflater.

Here is my code:

public void getAudioRecordLayout() {
    testView = testInflater.inflate(
            getResources().getLayout(
                    R.layout.imageview), null); 

    mWorkDetailAttachImageView = (ImageView) findViewById(R.id.attach_photo_camera_ImageView);

    mIncludeHighLayout = (LinearLayout) findViewById(R.id.include_High);
    mIncludeLowLayout = (LinearLayout) findViewById(R.id.include_Low);

    mIncludeRecordAudioInWorkDetailLayout.setVisibility(View.VISIBLE);
    mIncludeRecordAudioInRecordOfInspectionLayout
            .setVisibility(View.GONE);

    mWorkDetailAttachImageView.setOnClickListener(this);

}

Edit # 1 :

When i am showing imageview second time that imageview shows inside another inflate view.

Image view OnclickListiener :

 if (v == mWorkDetailAttachImageView) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.add_picture_image_dialog,
            null);
    final AlertDialog alertDialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(
            TestResultsActivity.this);

    builder.setView(view);
    builder.setInverseBackgroundForced(true);
    builder.setCancelable(true);
    alertDialog = builder.create();
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.show();

    Button cancel = (Button) view
            .findViewById(R.id.cancel_img_dialog_btn);
    Button takeNew = (Button) view
            .findViewById(R.id.take_new_photo_btn);
    Button chooseExisting = (Button) view
            .findViewById(R.id.choose_from_existing_img_btn);

    takeNew.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            alertDialog.dismiss();
        }
    });
    chooseExisting.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
            alertDialog.dismiss();
        }
    });
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            alertDialog.dismiss();
        }
    });
} 

Edit # 2

MainActivity.java

package rajesh.dropdowndemo;

               import java.security.PublicKey;

           import android.app.Activity;
          import android.app.AlertDialog;
          import android.content.Intent;
          import android.database.Cursor;
       import android.graphics.Bitmap;
       import android.graphics.BitmapFactory;
        import android.net.Uri;
       import android.os.Bundle;
       import android.provider.MediaStore;
        import android.view.LayoutInflater;
         import android.view.View;
       import android.view.View.OnClickListener;
        import android.view.Window;
        import android.view.animation.Animation;
        import android.view.animation.Animation.AnimationListener;
        import android.view.animation.ScaleAnimation;
        import android.widget.Button;
       import android.widget.ImageView;
         import android.widget.LinearLayout;
         import android.widget.RelativeLayout;
         import android.widget.TextView;
         import android.widget.Toast;

           public class MainActivity extends Activity {

/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;

/** Textview holding the title of the droddown */
private TextView mDropdownTitle;

ImageView imagView;
LinearLayout commonIncludeLayout, includeHighLayout;
int i = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getImageViewLayout(1);
    getImageViewLayout(2);
    // getHighLayout();

    mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
    mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
    commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
    includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);

    final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
    final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
    final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
    final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);

    dropDownTextView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
                openDropdown();
            } else {
                closeDropdown();
            }
        }
    });

    alt0.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            includeHighLayout.setVisibility(View.VISIBLE);
            dropDownTextView.setText(R.string.alt0);
            closeDropdown();
            alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.icn_dropdown_checked, 0);
            alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            Toast.makeText(getBaseContext(), R.string.alt0,
                    Toast.LENGTH_SHORT).show();
        }
    });

    alt1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            includeHighLayout.setVisibility(View.GONE);
            dropDownTextView.setText(R.string.alt1);
            closeDropdown();
            alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.icn_dropdown_checked, 0);
            alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            Toast.makeText(getBaseContext(), R.string.alt1,
                    Toast.LENGTH_SHORT).show();
        }
    });

    alt2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            includeHighLayout.setVisibility(View.GONE);
            dropDownTextView.setText(R.string.alt2);
            closeDropdown();
            alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.icn_dropdown_checked, 0);
            Toast.makeText(getBaseContext(), R.string.alt2,
                    Toast.LENGTH_SHORT).show();
        }
    });
}

/**
 * Animates in the dropdown list
 */
private void openDropdown() {
    if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
        ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
        anim.setDuration(getResources().getInteger(
                R.integer.dropdown_amination_time));
        mDropdownFoldOutMenu.startAnimation(anim);
        mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                R.drawable.icn_dropdown_close, 0);
        mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
    }
}

/**
 * Animates out the dropdown list
 */
private void closeDropdown() {
    if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
        ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
        anim.setDuration(getResources().getInteger(
                R.integer.dropdown_amination_time));
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDropdownFoldOutMenu.setVisibility(View.GONE);
            }
        });
        mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                R.drawable.icn_dropdown_open, 0);
        mDropdownFoldOutMenu.startAnimation(anim);
    }
}

/**
 * 
 * This represents Inflate the layout into main.xml layout
 */
public void getImageViewLayout(final int aa) {

    // LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    // View view = inflater.inflate(R.layout.imageview, null);

    RelativeLayout outer;

    if (aa == 1)
        outer = (RelativeLayout) findViewById(R.id.image_layout);
    else
        outer = (RelativeLayout) findViewById(R.id.image_layout1);

    imagView = (ImageView) outer
            .findViewById(R.id.attach_photo_camera_ImageView);

    imagView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("Imageview Clicked");
            if (aa == 1)
                imagView.setTag(1);
            else
                imagView.setTag(2);
            LayoutInflater inflater = LayoutInflater
                    .from(MainActivity.this);
            View view = inflater.inflate(R.layout.add_picture_image_dialog,
                    null);
            final AlertDialog alertDialog;
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);

            builder.setView(view);
            builder.setInverseBackgroundForced(true);
            builder.setCancelable(true);
            alertDialog = builder.create();
            alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            alertDialog.show();

            Button cancel = (Button) view
                    .findViewById(R.id.cancel_img_dialog_btn);
            Button takeNew = (Button) view
                    .findViewById(R.id.take_new_photo_btn);
            Button chooseExisting = (Button) view
                    .findViewById(R.id.choose_from_existing_img_btn);

            takeNew.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent cameraIntent = new Intent(
                                                                                                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 100);

                    alertDialog.dismiss();
                }
            });
            chooseExisting.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                                                                                                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(i, 200);
                    alertDialog.dismiss();
                }
            });
            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    alertDialog.dismiss();
                }
            });
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
        // get Image
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        System.out.println(imagView.getTag().toString());
        String i = imagView.getTag().toString();
        if (imagView.getTag(1) == i) 
            ((ImageView) imagView.getTag(1)).setImageBitmap(thumbnail);
        else if(imagView.getTag(2) != null)
            ((ImageView) imagView.getTag(2)).setImageBitmap(thumbnail);
    } else if (resultCode == RESULT_CANCELED) {
        System.out.println(imagView.getTag());
        Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
                .show();
    }
    if (requestCode == 200 && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        Bitmap bit = (Bitmap) BitmapFactory.decodeFile(picturePath);
        System.out.println(imagView.getTag().toString());
        String i = imagView.getTag().toString();
        if (imagView.getTag() == i) 
            ((ImageView) imagView.getTag(1)).setImageBitmap(bit);
        else if(imagView.getTag(2) != null)
            ((ImageView) imagView.getTag(2)).setImageBitmap(bit);
    } else if (resultCode == RESULT_CANCELED) {

        Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
                .show();
    }

}

    }

main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="10dip" >

<TextView
    android:id="@+id/dropdown_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dropdown_background"
    android:drawableRight="@drawable/icn_dropdown_open"
    android:gravity="center_vertical|left"
    android:padding="10dip"
    android:text="Dropdown alts:" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/dropdown_textview"
    android:layout_marginTop="10dip"
    android:padding="100dip"
    android:text="other content" />

<LinearLayout 
    android:id="@+id/include_imageview_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:orientation="vertical">

<include layout="@layout/imageview"/>
   </LinearLayout>
      <LinearLayout
        android:id="@+id/dropdown_foldout_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dropdown_textview"
        android:layout_marginTop="2dip"
        android:background="@drawable/dropdown_background"
        android:orientation="vertical"
        android:padding="1dip"
        android:visibility="gone" >

    <TextView
        android:id="@+id/dropdown_alt0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dropdown_selector"
        android:drawableRight="@drawable/icn_dropdown_checked"
        android:gravity="center_vertical|left"
        android:padding="10dip"
        android:text="@string/alt0" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#cccccc" />

    <TextView
        android:id="@+id/dropdown_alt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dropdown_selector"
        android:gravity="center_vertical|left"
        android:padding="10dip"
        android:text="@string/alt1" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#cccccc" />

    <TextView
        android:id="@+id/dropdown_alt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dropdown_selector"
        android:gravity="center_vertical|left"
        android:padding="10dip"
        android:text="@string/alt2" />
</LinearLayout>

 <LinearLayout 
    android:id="@+id/include_high_imageview_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    android:visibility="gone">

<include layout="@layout/high_layout"/>
 </LinearLayout>

  </RelativeLayout>

imageview.xml

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:background="@android:color/background_dark" >

 <TextView
    android:id="@+id/attach_photo_titile_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:padding="5dp"
    android:text="Attach Photo"
    android:textColor="@android:color/white" />

<ImageView
    android:id="@+id/attach_photo_camera_ImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/attach_photo_titile_textView"
    android:contentDescription="@string/app_name"
    android:clickable="true"
    android:src="@drawable/ic_launcher" />


    </RelativeLayout>

dailog.xml

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

<Button
    android:id="@+id/take_new_photo_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:text="Take New Photo" />

<Button
    android:id="@+id/choose_from_existing_img_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:text="Choose Existing" />

<Button
    android:id="@+id/cancel_img_dialog_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:text="Cancel" />

 </LinearLayout>

string.xml

  <resources>

<string name="app_name">DropDownDemo</string>
<string name="alt0">High</string>
<string name="alt1">Low</string>
<string name="alt2">General</string>

  </resources>

high_layout.xml

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


<include 
    layout="@layout/imageview"/>
   </LinearLayout>

Please any one help me.

役に立ちましたか?

解決

Try this..

 testView = testInflater.inflate(getResources().getLayout(R.layout.imageview), null); 

 mWorkDetailAttachImageView = (ImageView) testView.findViewById(R.id.attach_photo_camera_ImageView);

add in your click

  mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

    Button cancel = (Button) view
                    .findViewById(R.id.cancel_img_dialog_btn);
            Button takeNew = (Button) view
                    .findViewById(R.id.take_new_photo_btn);
            Button chooseExisting = (Button) view
                    .findViewById(R.id.choose_from_existing_img_btn);

            takeNew.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent cameraIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
                    alertDialog.dismiss();
                }
            });
            chooseExisting.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(i, RESULT_LOAD_IMAGE);
                    alertDialog.dismiss();
                }
            });
            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    alertDialog.dismiss();
                }
            });
        } 


        }
    });

Or

if (v.getId() == R.id.attach_photo_camera_ImageView) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.add_picture_image_dialog,
            null);
    final AlertDialog alertDialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(
            TestResultsActivity.this);

    builder.setView(view);
    builder.setInverseBackgroundForced(true);
    builder.setCancelable(true);
    alertDialog = builder.create();
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.show();

    Button cancel = (Button) view
            .findViewById(R.id.cancel_img_dialog_btn);
    Button takeNew = (Button) view
            .findViewById(R.id.take_new_photo_btn);
    Button chooseExisting = (Button) view
            .findViewById(R.id.choose_from_existing_img_btn);

    takeNew.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            alertDialog.dismiss();
        }
    });
    chooseExisting.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
            alertDialog.dismiss();
        }
    });
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            alertDialog.dismiss();
        }
    });
} 

EDIT

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
testView = inflater.inflate(R.layout.imageview, null); 

EDIT 1

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="10dip" >

<TextView
    android:id="@+id/dropdown_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dropdown_background"
    android:drawableRight="@drawable/icn_dropdown_open"
    android:gravity="center_vertical|left"
    android:padding="10dip"
    android:text="Dropdown alts:" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/dropdown_textview"
    android:layout_marginTop="10dip"
    android:padding="100dip"
    android:text="other content" />

<LinearLayout 
    android:id="@+id/include_imageview_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:orientation="vertical">

<include 
     android:id="@+id/image_layout"
     layout="@layout/imageview"/>
   </LinearLayout>
      <LinearLayout
        android:id="@+id/dropdown_foldout_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dropdown_textview"
        android:layout_marginTop="2dip"
        android:background="@drawable/dropdown_background"
        android:orientation="vertical"
        android:padding="1dip"
        android:visibility="gone" >

    <TextView
        android:id="@+id/dropdown_alt0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dropdown_selector"
        android:drawableRight="@drawable/icn_dropdown_checked"
        android:gravity="center_vertical|left"
        android:padding="10dip"
        android:text="@string/alt0" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#cccccc" />

    <TextView
        android:id="@+id/dropdown_alt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dropdown_selector"
        android:gravity="center_vertical|left"
        android:padding="10dip"
        android:text="@string/alt1" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#cccccc" />

    <TextView
        android:id="@+id/dropdown_alt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dropdown_selector"
        android:gravity="center_vertical|left"
        android:padding="10dip"
        android:text="@string/alt2" />
</LinearLayout>

 <LinearLayout 
    android:id="@+id/include_high_imageview_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    android:visibility="gone">

<include 
    android:id="@+id/image_layout1"
    layout="@layout/imageview"/>
 </LinearLayout>

  </RelativeLayout>

JAVA

package rajesh.dropdowndemo;

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View; 
 import android.view.View.OnClickListener;
 import android.view.Window;
 import android.view.animation.Animation;
 import android.view.animation.Animation.AnimationListener;
 import android.view.animation.ScaleAnimation;
  import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.Toast;

public class MainActivity extends Activity {

/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;

/** Textview holding the title of the droddown */
private TextView mDropdownTitle;


LinearLayout commonIncludeLayout,includeHighLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getImageViewLayout(1);
    getImageViewLayout(2);

    mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
    mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
    commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
    includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);

    final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
    final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
    final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
    final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);

    dropDownTextView.setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
                        openDropdown();
                    } else {
                        closeDropdown();
                    }
                }
            });

    alt0.setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {

                    includeHighLayout.setVisibility(View.VISIBLE);
                    dropDownTextView.setText(R.string.alt0);
                    closeDropdown();
                    alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                            R.drawable.icn_dropdown_checked, 0);
                    alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    Toast.makeText(getBaseContext(), R.string.alt0, Toast.LENGTH_SHORT).show();
                }
            });

    alt1.setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {
                    includeHighLayout.setVisibility(View.GONE);
                    dropDownTextView.setText(R.string.alt1);
                    closeDropdown();
                    alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                            R.drawable.icn_dropdown_checked, 0);
                    alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    Toast.makeText(getBaseContext(), R.string.alt1, Toast.LENGTH_SHORT).show();
                }
            });

    alt2.setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {
                    includeHighLayout.setVisibility(View.GONE);
                    dropDownTextView.setText(R.string.alt2);
                    closeDropdown();
                    alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                            R.drawable.icn_dropdown_checked, 0);
                    Toast.makeText(getBaseContext(), R.string.alt2, Toast.LENGTH_SHORT).show();
                }
            });
}

/**
 * Animates in the dropdown list
 */
private void openDropdown() {
    if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
        ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
        anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
        mDropdownFoldOutMenu.startAnimation(anim);
        mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                R.drawable.icn_dropdown_close, 0);
        mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
    }
}

/**
 * Animates out the dropdown list
 */
private void closeDropdown() {
    if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
        ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
        anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDropdownFoldOutMenu.setVisibility(View.GONE);
            }
        });
        mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                R.drawable.icn_dropdown_open, 0);
        mDropdownFoldOutMenu.startAnimation(anim);
    }
}


/**
 * 
 * This represents Inflate the layout into main.xml layout 
 */
public void getImageViewLayout(int aa){

LinearLayout outer;

 if(aa == 1)        
      outer = (LinearLayout) findViewById(R.id.image_layout);
 else
      outer = (LinearLayout) findViewById(R.id.image_layout1);

    ImageView imagView = (ImageView) outer.findViewById(R.id.attach_photo_camera_ImageView);

    imagView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        System.out.println("Imageview Clicked");
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View view = inflater.inflate(R.layout.add_picture_image_dialog,
                null);
        final AlertDialog alertDialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                MainActivity.this);

        builder.setView(view);
        builder.setInverseBackgroundForced(true);
        builder.setCancelable(true);
        alertDialog = builder.create();
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.show();

        Button cancel = (Button) view
                .findViewById(R.id.cancel_img_dialog_btn);
        Button takeNew = (Button) view
                .findViewById(R.id.take_new_photo_btn);
        Button chooseExisting = (Button) view
                .findViewById(R.id.choose_from_existing_img_btn);

        takeNew.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                alertDialog.dismiss();
            }
        });
        chooseExisting.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                alertDialog.dismiss();
            }
        });
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alertDialog.dismiss();
            }
        }); 
    }
});

}   
}

他のヒント

Did you checked view in your onClick function like below..

    @Override
public void onClick(View v) {

    int id = v.getId();

    switch (id) {

    case R.id.attach_photo_camera_ImageView:
            //your click code
        break;

}

or try below code instad of your code..

   mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top