Android listview adapter and various listeners, improve efficiency, androidlistview

Source: Internet
Author: User

Android listview adapter and various listeners, improve efficiency, androidlistview

I wrote a blog about listview before. Now I feel that the blog about listview is not comprehensive enough. However, some methods are recommended, such as flexible listeners and write adapters. The link is here android listview long press, and click various event capture. The click listener is an item to set the listener, Which is inefficient.

The process of continuous work is also a process of continuous summarization. Now I have a better understanding of listview, So I re-wrote a practical demo. This demo involves the improvement of listview data sources, layout, adapters, various listeners, and efficiency. Now, I want to write a general adapter for this understanding, but it is still a bit difficult, that is, the data source problem and layout. However, you can use an Inheritance Method to write a universal adapter. Function call is used to implement the data source and layout of listview, and override these methods after inheritance. Because the same is true for projects in the company. The following describes listview.

I. entity class ItemTest

This is a custom listview, so write an object class as the adaptive type of the listview adapter.

Package com. example. customlistviewdemo;/*** entity class ItemTest * @ author mmsx * blog website: http://blog.csdn.net/qq_16064871 */public class ItemTest {private int mImageViewID; private String mstrName; public ItemTest () {} public ItemTest (int ImageViewID, String strName) {this. mImageViewID = ImageViewID; this. mstrName = strName;} public int getImageViewID () {return mImageViewID;} public String getstrName () {return mstrName ;}}
Ii. listview adapter for entity ItemTest SelfAdapt

The listview adapter inherits from the BaseAdapter and passes in the activity lifecycle, listview layout, and data source through the SelfAdapt constructor.

It uses the function of determining whether convertView is empty to reduce the loading layout and improve efficiency.

In addition, an internal class ViewHolder is used to reduce the id in the search layout and label the convertView. setTag (viewHolder);, and then obtain viewHolder = (ViewHolder) convertView. getTag ();

In this way, the listview efficiency will be very good.

Package com. example. customlistviewdemo; import java. util. arrayList; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. toast; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. I MageView; import android. widget. textView;/*** listview adapter inherited from BaseAdapter * @ author mmsx * blog site: http://blog.csdn.net/qq_16064871 */public class SelfAdapt extends BaseAdapter implements OnCheckedChangeListener {private Context mContext; private int mresourceLayoutID; private LayoutInflater mLayoutInflater; private ArrayList <ItemTest> mList;/*** constructor of the adapter * @ param context * @ param resourceLayoutID * @ Param list */public SelfAdapt (Context context, int resourceLayoutID, ArrayList <ItemTest> list) {this. mContext = context; this. mresourceLayoutID = resourceLayoutID; this. mList = list; // get the view defined in xml and instantiate mLayoutInflater = (LayoutInflater) mContext. getSystemService (Context. LAYOUT_INFLATER_SERVICE);} @ Overridepublic int getCount () {return mList! = Null? MList. size (): 0 ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic long getItemId (int position) {return position ;} @ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null; if (convertView = null) {// you do not need to load the layout each time, and find the id to improve the efficiency of listview convertView = mLayoutInflater. inflate (mresourceLayoutID, null); viewHolder = new ViewHolder (); viewHolder. imageView = (ImageView) convertView. findViewById (R. id. imageView1); viewHolder. textView = (TextView) convertView. findViewById (R. id. textViewTest); viewHolder. checkbox = (CheckBox) convertView. findViewById (R. id. checkBox1); // Add TagconvertView to the label. setTag (viewHolder);} else {// when convertView is not empty, retrieve ViewHolder = (viewHolder) convertView from the tag ViewHolder. getTag () ;}itemtest ItemTest = new itemTest (); ItemTest = mList. get (position); viewHolder. imageView. setImageResource (itemTest. getImageViewID (); viewHolder. textView. setText (itemTest. getstrName (); // sets viewHolder for the checkbox listener. checkbox. setOnCheckedChangeListener (this); viewHolder. checkbox. setTag (position); return convertView;} // internal class implementation improves listview efficiency class ViewHolder {public ImageView imageView; public TextView textView; public CheckBox checkbox ;} @ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {int nPosition = (Integer) buttonView. getTag ()). intValue (); Toast. makeText (mContext, String. valueOf (nPosition) + "CheckBox click" + String. valueOf (isChecked), Toast. LENGTH_SHORT ). show ();}}
3. MainActivity

In this case, the listview click and long-pressed interface listener is set.

ListView. setOnItemClickListener (this); // you can call this operation to set the listener to listView. setOnItemLongClickListener (this). // you can call this operation to set the listener to a long-pressed listener.

Set these two interfaces

implements OnItemClickListener,OnItemLongClickListener
See all the code below

Package com. example. customlistviewdemo; import java. util. arrayList; import android. OS. bundle; import android. app. activity; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemLongClickListener; import android. widget. listView; import android. widget. adapterView. onItemClickListener; import android. widget. toast;/*** compared with standard listview writing and improved listview efficiency * @ author mmsx * Main blog site: http://blog.csdn.net/qq_16064871 */public class MainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {private ArrayList <ItemTest> mArrayList = new ArrayList <ItemTest> (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); InitUI ();} private void InitUI () {if (mArrayList! = Null) {mArrayList. clear (); for (int I = 0; I <50; I ++) {ItemTest itemTest1 = new ItemTest(R.drawable.bmp 1, "Image 1"); mArrayList. add (itemTest1); ItemTest itemTest2 = new ItemTest(R.drawable.bmp 2, "Image 2"); mArrayList. add (itemTest2) ;}} ListView listView = (ListView) findViewById (R. id. listView1); SelfAdapt selfAdapt = new SelfAdapt (getApplicationContext (), R. layout. listview_item, mArrayList); listView. setAdapter (SelfAdapt); listView. setOnItemClickListener (this); // you can click listener to implement listView. setOnItemLongClickListener (this); // you can call this operation to set a long-pressed listener.} @ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (this, "listview click" + parent. getItemAtPosition (position), Toast. LENGTH_SHORT ). show () ;}@ Overridepublic boolean onItemLongClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (this, "long press of listview", Toast. LENGTH_SHORT). show (); return true ;}}
Iv. layout of listview listview_item

The entity class ItemTest is to be implemented. I have added the chexbox control listener, as shown in the following example.

This sentence is sometimes important for android: descendantFocusability = "blocksDescendants"

If your custom ListViewItem contains a Button or Checkable subclass control, the default focus is handed over to the child control. Therefore, you need to add this sentence.

<? 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 = "50dp" android: descendantFocusability = "blocksDescendants" android: orientation = "horizontal"> <ImageView android: id = "@ + id/imageView1" android: layout_width = "50dp" android: layout_height = "50dp" android: src = "@ drawable/BMP 1"/> <TextView android: id = "@ + id/textViewTest" android: layout_width = "0dp" android: layout_height = "50dp" android: layout_weight = "1" android: gravity = "center" android: text = "TextView" android: textColor = "# FF236A9C"/> <CheckBox android: id = "@ + id/checkBox1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textColor = "# FF236A9C" android: text = "select"/> </LinearLayout>
5. activity_main

<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=".MainActivity" >    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true" >    </ListView></RelativeLayout>
Vi. Implemented



In addition, I am still collecting a variety of small knowledge about listview, such as separation line, click not to default effect, listview does not scroll with the scroll bar, etc. Later I will write a blog by myself.
Download project resources:
Reprinted please indicate the source of blog Web site: http://blog.csdn.net/qq_16064871
If any reposted blog website is not indicated, or the author's consent is not obtained. The author holds copyright ownership.

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.