Precautions for using android ListView

Source: Internet
Author: User

Precautions for using android ListView

To sum up my considerations when using android Listview:

1. to hand over the Click Event of the Child control in the listview item to the item for processing, two steps are required:

(1) override the onTouchEvent method of the child Control

@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn false;}

(2) Add the following sentence to the root layout in the xml file of item:

android:descendantFocusability="blocksDescendants" 
2. listview optimization policy viewholder. The Code is as follows:

Class MyAdapter extends BaseAdapter {Context mContext; LinearLayout linearLayout = null; LayoutInflater inflater; TextView tex; final int VIEW_TYPE = 2; final int TYPE_1 = 0; final int TYPE_2 = 1; public MyAdapter (Context context) {mContext = context; inflater = LayoutInflater. from (mContext) ;}@ Overridepublic int getCount () {return listString. size () ;}// this method is called for each convert view to obtain the currently required view style @ Overridepublic int getItemViewType (int position) {int p = position % 6; if (p = 0) return TYPE_1; else if (p <3) return TYPE_2; elsereturn TYPE_1 ;}@ Overridepublic int getViewTypeCount () {return 2 ;} @ Overridepublic Object getItem (int arg0) {return listString. get (arg0) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {viewHolder1 holder1 = null; viewHolder2 holder2 = null; int type = getItemViewType (position); // if (convertView = null) does not have convertView, You need to generate a new control if (convertView = null) {// according to the current style, determine the new Layout switch (type) {case TYPE_1: convertView = inflater. inflate (R. layout. listitem1, parent, false); holder1 = new viewHolder1 (); holder1.textView = (TextView) convertView. findViewById (R. id. textview1); holder1.checkBox = (CheckBox) convertView. findViewById (R. id. checkbox); convertView. setTag (holder1); break; case TYPE_2: convertView = inflater. inflate (R. layout. listitem2, parent, false); holder2 = new viewHolder2 (); holder2.textView = (TextView) convertView. findViewById (R. id. textview2); holder2.imageView = (ImageView) convertView. findViewById (R. id. imageview); convertView. setTag (holder2); break;} else {// convertView exists, according to the style, get the Layout switch (type) {case TYPE_1: holder1 = (viewHolder1) convertView. getTag (); break; case TYPE_2: holder2 = (viewHolder2) convertView. getTag (); break;} // set resource switch (type) {case TYPE_1: holder1.textView. setText (Integer. toString (position); holder1.checkBox. setChecked (true); break; case TYPE_2: holder2.textView. setText (Integer. toString (position); holder2.imageView. setBackgroundResource (R. drawable. icon); break ;}}return convertView ;}// control resource static class viewHolder1 {CheckBox checkBox; TextView textView;} static class viewHolder2 {ImageView imageView; textView textView ;}

3. The event listening of the Listview subcontrol must be written in the getview method of the adapter, rather than in the activity. If viewholder needs to be used in getview, The viewholder compiling method should be different from the common compiling method. The following content should not appear:

if (view == null) {...} else{holder = (ViewHolder) view.getTag();...}

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.