posts - 103,  comments - 41,  views - 20万

ListView主要包括view和数据源。其数据适配器列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。

ListView的没有oom原因。经典图:

1.democoderjoy中使用,这里我们新建一个ListViewActi的activity。布局文件listview比较简单

复制代码
<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ListView01"
        android:layout_weight="1"
        />
    <ListView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list_view"
        android:layout_weight="1"
        android:stackFromBottom="true"
        android:background="@drawable/icon"
        android:scrollbars="none"

        />
</LinearLayout>
复制代码

2.此外还需要一个item项

复制代码
<?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">
d
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_alignParentRight="true"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:textSize="20dp"
            android:text="New Text"
            android:id="@+id/textView01"

             />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView02"
            android:layout_below="@+id/textView01"
            />

</RelativeLayout>
复制代码

3.arrayAdapter的使用采用布局的第二个listview id

核心代码:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(
                this,R.layout.simple_list_item_1,
                mData);
        ListView listView=(ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

4.simpleAdater使用

复制代码
ListView list = (ListView) findViewById(R.id.ListView01);
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<10;i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.icon);//图像资源的ID
            map.put("ItemTitle", "Level "+i);
            map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");
            listItem.add(map);
        }
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源
                R.layout.listview_item,//ListItem的XML实现
                //动态数组与ImageItem对应的子项
                new String[] {"ItemImage","ItemTitle", "ItemText"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[] {R.id.imageView,R.id.textView01,R.id.textView02}
        );

        View view = LayoutInflater.from(this).inflate(R.layout.head_view_layout, null);
        list.addHeaderView(view, null, true);
        list.setOverscrollHeader(getResources().getDrawable(R.drawable.icon));
        list.addFooterView(view);
        list.setHeaderDividersEnabled(true);
        list.setFooterDividersEnabled(true);
        list.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        list.setAdapter(listItemAdapter);

        //list.setOverScrollMode(View.OVER_SCROLL_NEVER);
        //view.setVisibility(View.GONE);
        //添加点击
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                setTitle("点击第"+arg2+"个项目");
                Toast.makeText(getApplicationContext(),"点击第"+arg2+"个项目",Toast.LENGTH_SHORT).show();
            }
        });
        SimpleCursorAdapter d;
        //添加长按点击 响应餐单
        list.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("长按菜单-ContextMenu");
                menu.add(0, 0, 0, "弹出长按菜单0");
                menu.add(0, 1, 0, "弹出长按菜单1");
            }
        });
复制代码

5.效果如下:

Tips:

a.  footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条,默认为true

     headerDividersEnabled:是否在headerView(表头)前绘制一个分隔条,默认为true

b. listview可以在布局中设置背景

c. listview具有headview 和footview 如程序中所使用的,

list.addHeaderView(view, null, true);
        list.setOverscrollHeader(getResources().getDrawable(R.drawable.icon));
list.addFooterView(view);

使用的子view的布局如下:

复制代码
<?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="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="head foot list"/>
<CheckBox
    android:id="@+id/group_selection_all"
    android:layout_width="40dip"
    android:layout_height="40dip"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="8dip"
    android:layout_marginRight="0dip"
    android:focusable="false"
    android:clickable="true"
    android:gravity="center"
    android:scaleType="centerInside"/>
</LinearLayout>
复制代码

 d.设置从底向上排列参数 布局中定义stackFromBottom

 

posted on   zCoderJoy  阅读(278)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示