zoukankan      html  css  js  c++  java
  • ListView总结

    ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习。现在这里大概总结一下。

    基于数组的ListView:使用android:entries属性可以指定列表项数组,这种方式最简洁方便,但内容只能是文本,可定制的内容少之又少。

    使用ArrayAdapter创建ListView:也仅限于将数组或集合里的元素包装成列表项,比直接使用数组好在,可以指定列表项的列表项组件。例如:使用TextView作为列表项组件,不过也只能使用TextView。

    例子:

    1.列表项布局文件arraylist_item.xml:是的,没错,布局文件只包含一个TextView,这也是行的。

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

    2.主界面布局文件activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context="com.example.zjlyyq.test.MainActivity">
    
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView" />
    </LinearLayout>

    3.MainActivity:

    package com.example.zjlyyq.test;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends AppCompatActivity {
        ListView listView;
        String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView)findViewById(R.id.listView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.arraylist_item,names);
            listView.setAdapter(adapter);
        }
    }

    使用SimpleAdapter创建ListView:SimpleAdapter可以满足更强的定制,每个列表项对应一个布局文件,将一个Map里的元素对应在布局文件里的各组件,例如:

    1.列表项布局文件simpleadapter_item.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:layout_weight="0.15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/header"/>
        <LinearLayout
            android:layout_weight="0.85"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/username"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/talk"/>
        </LinearLayout>
    </LinearLayout>

    2.MainActivity:

    package com.example.zjlyyq.test;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
        ListView listView;
        String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
        int[] headers = new int[]{R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane};
        String[] talks = new String[]{"晚上吃了吗?","吃了","你呢?","还没?","哦哦"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView)findViewById(R.id.listView);
            List<Map<String,Object>> itemslist = new ArrayList<Map<String,Object>>();
            for(int i = 0;i < 5;i ++){
                Map<String,Object> evetyItem = new HashMap<String,Object>();
                evetyItem.put("header",headers[i]);
                evetyItem.put("name",names[i]);
                evetyItem.put("talk",talks[i]);
                itemslist.add(evetyItem);
            }
            //创建一个SimpleAdapter
            SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemslist,R.layout.simpleadapter_item,
            new String[]{"header","name","talk"},
            new int[]{R.id.header,R.id.username,R.id.talk});
    
            listView.setAdapter(simpleAdapter);
        }
    }

  • 相关阅读:
    O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)
    北京网选赛第二题(最大仰望角度)
    最小圆覆盖(随机增量法&模拟退火法)
    模拟退火算法A Star not a Tree?(poj2420)
    模拟退火算法(run away poj1379)
    模拟退火算法(西安网选赛hdu5017)
    最小费用流判负环消圈算法(poj2175)
    中国邮递员问题(一)
    破坏行动问题
    进化树问题
  • 原文地址:https://www.cnblogs.com/zjlyyq/p/6158468.html
Copyright © 2011-2022 走看看