简体   繁体   中英

Clickable image on ListView

I want to have a ListView that have an image in every item (star) and that image is clickabe. In other word user can click on star of every row of ListView and i want to define click action for that. How i can do this?

Add an imageView.. make it clickable by adding this to your ImageView tag:

android:clickable="true"
android:focusable = "false"

it should work..

you can make a ImageButton like

   <ImageButton
          android:id="@+id/sound_button"
          android:layout_x="430px" 
          android:layout_y="219px"
          android:layout_width="48px "
          android:layout_height="48px" 
          android:scaleType="center"
          android:src="@android:drawable/volumeicon"
          android:background="@drawable/clearbuttonup"
             />

and make a new xml and name it selector

           <?xml version="1.0" encoding="utf-8"?>
          <selector xmlns:android="http://schemas.android.com/apk/res/android">
             <item android:state_pressed="true"
                   android:drawable="@drawable/button_pressed" /> <!-- pressed -->
             <item android:state_focused="true"
                   android:drawable="@drawable/button_focused" /> <!-- focused -->
            <item android:drawable="@drawable/button_normal" /> <!-- default -->
            </selector>

OR you make a Imageview and setproperty of that ImageView isClickable="true"

you can use an ImageButton,

http://developer.android.com/reference/android/widget/ImageButton.html

you can also simply make the image clickable, and set it's onClickListener(),

http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener )

the problem with this however is that there will be no visual feedback to the user as the image is clicked. an image button looks and acts like a button, and if you are willing to provide normal, pressed, etc versions of the image, you can attach a state list drawable to the image button to make it look very professional.

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM