The item layout of the Android ListView contains a button that does not respond to the Onitemclick monitoring of the ListView as well as the Onitemlongclick listener problem resolution

Source: Internet
Author: User

:

Layout XML for activity:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "com.example.testlistviewandbutton.MainActivity" >    <listview        android:id= "@+id/ Lv_main "        android:layout_width=" match_parent "        android:layout_height=" match_parent ">    </ Listview></relativelayout>

ListView Item Layout:

<?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: Descendantfocusability= "blocksdescendants"    android:gravity= "center_horizontal"    android:orientation= " Horizontal "    android:weightsum=" 2 ">    <!--android:descendantfocusability=" Blocksdescendants " This property of the root layout is focused--    <button        android:id= "@+id/btn_item"        android:layout_width= "0DP"        android: layout_height= "Wrap_content"        android:layout_weight= "1"        android:text= "button"/></linearlayout >

Adapter code for the ListView:

Package Com.example.testlistviewandbutton;import Java.util.list;import Android.content.context;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.button;import Android.widget.toast;public class MyAdapter extends Baseadapter {private Context context;private list<string> str_list;public myadapter (context context, list< String> str_list) {this.context = Context;this.str_list = str_list;} @Overridepublic int GetCount () {return str_list = = null? 0:str_list.size ();} @Overridepublic String getItem (int position) {return str_list.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {if (Convertview = = null) {Viewholer = new Viewholer (); Convertview = view.inflate (context, r.layout.listview_item, null); Viewholer.btn_item = (Button) Convertview.findviewbyid (R.id.btn_item); Convertview.Settag (Viewholer);} else {Viewholer = (Viewholer) Convertview.gettag ();} ViewHoler.btn_item.setText (GetItem (position)); ViewHoler.btn_item.setOnClickListener (new Onclicklistener () {@ overridepublic void OnClick (View v) {toast.maketext (Context, "clicked Button", Toast.length_short). Show ();}); return Convertview;} Private Viewholer Viewholer;public Static class Viewholer {Button btn_item;}}

Package Com.example.testlistviewandbutton;import Java.util.arraylist;import Java.util.list;import Android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.widget.adapterview;import Android.widget.AdapterView.OnItemLongClickListener; Import Android.widget.toast;import Android.widget.adapterview.onitemclicklistener;import Android.widget.ListView; public class Mainactivity extends Activity {private ListView lv_main;private list<string> str_list;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview ();} private void Initview () {//TODO auto-generated method Stublv_main = (ListView) Findviewbyid (r.id.lv_main); str_list = new Arraylist<string> (); for (int i=0;i<20;i++) {Str_list.add ("This is the first" +i+ "button:");} Myadapter adapter = new Myadapter (mainactivity.this, str_list); Lv_main.setadapter (adapter);//listview Short-click Monitoring LV_MAIN.SEtonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {toast.maketext (Mainactivity.this, "clicked Onitem Monitoring of the ListView", Toast.length_short). Show ();}); /listview Long Press Listen Lv_main.setonitemlongclicklistener (new Onitemlongclicklistener () {@Overridepublic Boolean Onitemlongclick (adapterview<?> arg0, View arg1,int arg2, long Arg3) {Toast.maketext (Mainactivity.this, " Click on the ListView Onitem long Press to listen ", Toast.length_short). Show (); return true;//returns false here by default, but the president presses and then responds with a short press, returning true to resolve});}}


There are three types of solutions

1. Set the child control Focusable property in the item layout in the ListView to False
2. Set Button.setfocusable (False) in the GetView method

3. Set the root layout of the item the properties of android:descendantfocusability= "Blocksdescendant"

We can see that all three of these methods are designed so that buttons and other controls cannot get the focus, so that item can respond to click events.

The third method is relatively convenient to use because it sets all other controls in the item layout to not get the focus.

The Android:descendantfocusability attribute has a total of three values, respectively

Beforedescendants:viewgroup takes precedence over its subclass control and gets to focus
Afterdescendants:viewgroup gets focus only if its subclass control does not need to get focus
Blocksdescendants:viewgroup overrides the subclass control and gets the focus directly


Hey, the code is simple, and there are comments, there is no more explanation.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The item layout of the Android ListView contains a button that does not respond to the Onitemclick monitoring of the ListView as well as the Onitemlongclick listener problem resolution

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.