ListView principle and Optimization

Source: Internet
Author: User

I. Principle:

ListView is a composite list control.

That is, unlike TextView, it does not rely on other controls. You can directly use textView. setText () to display data, but ListView cannot display data. It displays data in MVC mode. When adding data, you must first specify an Adapter object (C Controller); ListView is equivalent to V (View) for displaying data; the List that provides data is equivalent to M (model ).

ListView obtains the displayed data through the Adapter object. The two controls are also independent of each other. That is, ListView only knows whether the displayed data is from the Adapter and does not know whether it is from the List or array. For data, it only knows to add the written data to the Adapter object, it is unknown whether the data will be used for the ListView control or other controls.

Ii. Example:

1. Create a ListView Layout

<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="match_parent"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true" >    </ListView></RelativeLayout>

2. Create an Adapter Layout

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:id="@+id/tv_one"        android:layout_width="wrap_content"        android:layout_height="60dp"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="Large Text"        android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>

3. Code

Import java. util. arrayList; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. view. view. onClickListener; import android. widget. baseAdapter; import android. widget. listAdapter; import android. widget. listView; import android. widget. relativeLayout; import android. widget. textView; public class MainActivity extends Activity implements OnClickListener {private ArrayList <String> mTitle = new ArrayList <String> (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); for (int I = 0; I <40; I ++) {mTitle. add ("title" + I);} ListView listView = (ListView) findViewById (R. id. listView1); BaseAdapter adapter = new BaseAdapter () {@ Override public View getView (int position, View convertView, ViewGroup parent) {Log. e ("getView", "position" + position); // line layout, converts the layout file xml into the layout object LayoutInflater layoutInflater = getLayoutInflater (); RelativeLayout relativeLayout = (RelativeLayout) layoutInflater. inflate (R. layout. item_relativ, null); // layout object content TextView textView = (TextView) relativeLayout. findViewById (R. id. TV _one); String string = mTitle. get (position); // data textView. setText (string); // return relativeLayout;} @ Override public int getCount () {// TODO Auto-generated method stub return mTitle. size (); // Number of displayed rows} @ Override public long getItemId (int position) {// TODO Auto-generated method stub return 0 ;} @ Override public Object getItem (int position) {// TODO Auto-generated method stub return null ;}; // load the Adapter to ListView. setAdapter (adapter) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public void onClick (View v) {switch (v. getId () {default: break ;}}}


Iii. Supplement:

1. The function of getLayoutInflater is to convert the layout into an object. Just like you want to use a method in a class, it is reasonable to first obtain the object of this class.

2. I don't know much about callback. Let's take a look at what I wrote in the previous article.

3. When you drag the ListView in the simulator, you can View the LOG and you will find that each update View item will create a row layout object. This is wrong, which will waste a lot of memory. This is the optimization of the ListView to be written in the next article.


This article is from the "Android key" blog and will not be reproduced!

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.