Android学习记录之ListView和ScrollView区别

来源:互联网 发布:中云数据科技有限公司 编辑:程序博客网 时间:2024/04/29 18:47

ScrollView中不管有多少个数据项都会全部加载


MainActivity.java

package com.example.android.listviewandscrollview;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.scrolllist);        LayoutInflater mflater = LayoutInflater.from(this);        LinearLayout scroll = (LinearLayout)findViewById(R.id.scrollLayout);        for(int i =0 ;i<120;i++){            View infoView  = mflater.inflate(R.layout.listitem, null);            scroll.addView(infoView);        }    }}

scrolllist.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ScrollView        android:id="@+id/scrollList"        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:orientation="vertical"            android:id="@+id/scrollLayout"            android:layout_width="match_parent"            android:layout_height="match_parent">        </LinearLayout>    </ScrollView></LinearLayout>


listitem.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"/>    <TextView        android:id="@+id/textView"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="Tom"/></LinearLayout>

1.用Android Studio中的Memory监视器可以看出不管怎么滑动都不会变化


ListView


MainActivity.java

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.listview);        ListView listView = (ListView)findViewById(R.id.listView);        ListAdapter listAdapter = new SimpleAdapter(this, getData(), R.layout.listitem        , new String[]{"image", "text"}, new int[]{R.id.imageView, R.id.textView});        listView.setAdapter(listAdapter);    }    private List<Map<String, Object>> getData(){        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();        for(int i=0; i<120; i++) {            Map<String, Object> map = new HashMap<String, Object>();            map.put("image", R.mipmap.ic_launcher);            map.put("text", "Tom");            list.add(map);        }        return list;    }}

listview.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>


listitem.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView        android:id="@+id/textView"        android:layout_height="wrap_content"        android:layout_width="wrap_content"/></LinearLayout>

1.同样用监视器看会有细微的变化


0 0