In Android, add two click events to listview. One is onitemclick and the other is the onclick event of an image.

Source: Internet
Author: User
Tags hhg

This is the first post I wrote in csdn. In addition, I was a real newbie shortly after I joined the work, so I hope you can provide more guidance.

Here we want to share a solution to adding two click events to a listview. The effect is like clicking a friend in the friend list of Android QQ To Go To The chat interface, click the icon next to it to go to the friend details page. The advantage is that the onitemclick event of the listview is not removed, and a new event is added.

 

The next step is how to implement it. First, we need a custom view. I chose to define a subclass of linearlayout, called clickicon.

Clickicon. Java

 

Package com. HHG. Test. listview2;

Import Android. content. context;
Import Android. util. attributeset;
Import Android. View. view;
Import Android. widget. linearlayout;

Public class clickicon extends linearlayout {

Public clickicon (context, attributeset attrs ){
Super (context, attrs );
}

@ Override
Public void setpressed (Boolean pressed ){

If (pressed & (View) getparent (). ispressed ()){
Return;
}
Super. setpressed (pressed );
}
}

 

 

Then, write the custom View to the layout file of the listview adapter.

List_view_item.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 = "horizontal">

<Imageview Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/photo">
</Imageview>
<Textview Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/name"
Android: layout_marginleft = "20dip">
</Textview>
<Com. HHG. Test. listview2.clickicon
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/clickiconlayout"
Android: layout_marginleft = "50dip">

<Imageview Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/icon"
Android: Id = "@ + ID/IMG">
</Imageview>

</COM. HHG. Test. listview2.clickicon>
</Linearlayout>

 

Then, in the listview adapter, I use the baseadapter subclass. Add a click event for each clickicon in the getview method.

Myadapter. Java

 

Package com. HHG. Test. listview2;

Import java. util. List;

Import Android. content. context;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. imageview;
Import Android. widget. textview;
Import Android. widget. Toast;
Import Android. widget. togglebutton;

Public class myadapter extends baseadapter {

Private context;
Private list <dog> dogs;

Public myadapter (context, list <dog> dogs ){
This. Context = context;
This. Dogs = dogs;
}

@ Override
Public int getcount (){
Return dogs. Size ();
}

@ Override
Public object getitem (INT position ){
Return position;
}

@ Override
Public long getitemid (INT position ){
Return position;
}

@ Override
Public View getview (final int position, view convertview, viewgroup parent ){

Layoutinflater Inflater = (layoutinflater) layoutinflater. From (context );
Viewholder h_= NULL;

If (convertview = NULL ){
Vl = new viewholder ();
Convertview = Inflater. Inflate (R. layout. list_view_item, null );

Vl. Photo = (imageview) convertview. findviewbyid (R. Id. Photo );
Vl. Name = (textview) convertview. findviewbyid (R. Id. Name );
Vl. clickicon = (clickicon) convertview
. Findviewbyid (R. Id. clickiconlayout );

Convertview. settag (Flac );
} Else {
Vl = (viewholder) convertview. gettag ();
}

DOG d = dogs. Get (position );

Vl. Photo. setimageresource (D. getimgid ());
Vl. Name. settext (D. getname ());
Vl. clickicon. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Toast. maketext (context, "icon clicked, position is:" + position,
Toast. length_short). Show ();
}
});

Return convertview;
}

Private Static class viewholder {
Imageview photo;
Textview name;
Clickicon;
}
}
To save trouble, I used a toast to prompt that the part was clicked.

The next step is to generate a listview in the activity, set the adapter, and add the onitemclick event.

Mainactivity. Java

Package com. HHG. Test. listview2;

Import java. util. arraylist;
Import java. util. List;

Import Android. App. activity;
Import Android. content. PM. applicationinfo;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. imageview;
Import Android. widget. Toast;
Import Android. widget. adapterview. onitemclicklistener;
Import Android. widget. listview;

Public class mainactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Listview Lv = (listview) findviewbyid (R. Id. testlistview );

Myadapter adapter = new myadapter (this, getdogs ());

LV. setadapter (adapter );

LV. setonitemclicklistener (New onitemclicklistener (){

@ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2,
Long arg3 ){
Toast. maketext (mainactivity. This, "item clicked, position is:" + arg2,
Toast. length_short). Show ();
}

});
}

Private list <dog> getdogs (){
List <dog> dogs = new arraylist <dog> ();
Applicationinfo appinfo = getapplicationinfo ();
Final string packagename = appinfo. packagename;
For (INT I = 0; I <8; I ++ ){
DOG d = new dog ();
D. setname ("dog no." + I );
D. setimgid (getresources (). getidentifier ("sample_thumb _" + I,
"Drawable", packagename ));
Dogs. Add (d );
}
Return dogs;
}
}

 

Here, a dog class is used to combine images and text. image resources can be found in apidemos.

Dog. Java

Package com. HHG. Test. listview2;

Public class dog {

Private int ID;
 
Private string name;
 
Private int imgid;

Public int GETID (){
Return ID;
}

Public void setid (int id ){
This. ID = ID;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public int getimgid (){
Return imgid;
}

Public void setimgid (INT imgid ){
This. imgid = imgid;
}
 
 
 
}

Now let's see the effect through the texture.

When we click an icon, the system prompts the icon to be clicked.

Then, when we click a listitem, the system prompts that the item is clicked and the position is.

 

Note: If the subclass of linearlayout is changed to any subclass of button, and the onitemclicked event is lost, I don't know why.

 

Thank you for reading this article.

 

If Apsara

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.