Android ListView 的简单用法

Android ListView 的简单用法参考API和《第一行代码》-ListViewisaviewgroupthatdisplaysalistofscrollableitems.ThelistitemsareautomaticallyinsertedtothelistusinganAdapterthatpullscontentfromasourcesuchasan…

大家好,又见面了,我是你们的朋友全栈君。

参考 API 和《第一行代码》

  1. ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.
  2. When the content for your layout is dynamic or not pre-determined, you can use a layout that subclasses AdapterView to populate the layout with views at runtime. A subclass of the AdapterView class uses an Adapter to bind data to its layout. The Adapter behaves as a middleman between the data source and the AdapterView layout—the Adapter retrieves the data (from a source such as an array or a database query) and converts each entry into a view that can be added into the AdapterView layout.

ListView 用来显示一个可以垂直滚动的列表,其中列表的每一项由其相关联的适配器提供,适配器起到一个中间人的作用,即连接列表数据和ListView布局
-
使用默认 ListView, 先定义一个数组(ArrayList)保存要在ListView里显示的数据,然后新建一个适配器,构造函数传入系统默认的子项目布局( android.R.layout.simple_list_item_1 )–(显示一个 TextView )和数据存在的数组,再直接调用 ListView 的 setAdapter() 方法,传入新建好的适配器,即可显示数据.

使用自定义的 ListView, 可以在子项目中不只显示一个 TextView, 而是可以显示其他自己定义的布局,所以要

  1. 先新建一个子项目布局,里面可以添加 TextView, Button, ImageView 等控件;
  2. 后新建一个类 Item 与子项目布局里的控件对应,用作 List<\T> 的范型和要继承的 ArrayAdapter<\T> 的范型,也是要显示的数据类型;
  3. 自定义 Adapter 类扩展自 ArrayAdapter<\T>, 重写 getView() 方法,先 getItem(position) 获取当前 Item 实例,然后 LayoutInflater.from(getContext()).inflate(resourceId,parent,false) 获取子项目布局 View 实例,再通过 View.findViewById() 获取子项目布局里的控件实例,最后调用 setText() 等控件方法完成控件的操作,返回 View.
  4. 然后和使用默认 ListView 一样,新建一个自定义的适配器,传入自定义的子项目布局和要显示的数据,再直接调用 ListView 的 setAdapter() 方法,传入新建好的适配器,即可显示数据.
  5. 要增加新的数据,只需要调用 Adapter.add(Item) 即可.

例 :

public class Item { 
   
    private String text;
    private int button;
    
    public Item(String text,int button){ 
   
        this.text=text;
        this.button=button;
    }
    public String getText(){ 
   
        return text;
    }
    public int getButton(){ 
   
        return button;
    }
}
public class ItemAdapter extends ArrayAdapter<Item> { 
   
    private int resourceId;
    public ItemAdapter(Context context,int resource,List<Item> data){ 
   
        super(context,resource,data);
        resourceId=resource;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){ 
   
        final Item item=getItem(position);
        View view;
        ViewHolder holder=new ViewHolder();  // viewHolder 是提升 ListView 运行效率
        if(convertView==null){ 
   
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            holder.tv=view.findViewById(R.id.text_view);
            holder.bn=view.findViewById(R.id.button);
            view.setTag(holder);
        } else { 
   
            view = convertView;
            holder=(ViewHolder) view.getTag();
        }
        holder.tv.setText(item.getText());
        holder.bn.setOnClickListener(new View.OnClickListener() { 
   
            @Override
            public void onClick(View v) { 
   
                Toast.makeText(getContext(),"you clicked button"+item.getButton(),Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
    class ViewHolder{ 
   
        TextView tv;
        Button bn;
    }
}
public class MainActivity extends AppCompatActivity { 
   

    private ArrayList<Item> data=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        ItemAdapter adapter=new ItemAdapter(this,R.layout.list_view_item,data);
        ListView listView=(ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }

    private void initData(){ 
   
        for(int i=0;i<20;++i){ 
   
            Item a=new Item("list view test. no "+i,i);
            data.add(a);
        }
    }
}

运行效果 :

这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163277.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • murmurhash算法_MurmurHash与随机数

    murmurhash算法_MurmurHash与随机数unsignedintmurMurHash(constvoid*key,intlen){constunsignedintm=0x5bd1e995;constintr=24;constintseed=97;unsignedinth=seed^len;//Mix4bytesatatimeintothehashconstunsigne…

    2022年10月18日
    4
  • Pytest(13)命令行参数–tb的使用

    Pytest(13)命令行参数–tb的使用前言pytest使用命令行执行用例的时候,有些用例执行失败的时候,屏幕上会出现一大堆的报错内容,不方便快速查看是哪些用例失败。–tb=style参数可以设置报错的时候回溯打印内容,可以设置参

    2022年7月29日
    7
  • 加密门禁卡复制

    加密门禁卡复制本文主要是针对一下加密门禁卡解密复制问题。最近发现补一张要50,成本的话白卡2块钱一张,读卡器可以白嫖也可以24多买一个不带壳pn532模块。一.卡类型IC卡是智能卡的总称。普通IC卡,0扇区不可以修改,其他扇区可反复擦写,我们使用的电梯卡、门禁卡等智能卡发卡商所使用的都是M1卡,可以理解为物业发的原卡。UID卡普通复制卡,可以重复擦写所有扇区,主要应用在IC卡复制上,遇到带有防火墙的读卡器就会失效。CUID卡可擦写防屏蔽卡,可以重复擦写所有扇区,UID卡复制无效的情况

    2022年6月25日
    115
  • 判断网站被搜索引擎降权

    怎么判断网站被降权了,觉得这篇文章写的很好。其实我一直觉得很多压力都是人自己给自己的,像我现在就没有压力,但我不知道为什么经理压力那么大,刚才测试自己的心理年龄,与我实际年龄一样一样的,我觉得自己变

    2021年12月25日
    36
  • GO 第一 变量

    GO 第一 变量

    2021年5月25日
    168
  • kindeditor<=4.1.5上传漏洞复现

    kindeditor<=4.1.5上传漏洞复现0x00漏洞描述漏洞存在于kindeditor编辑器里,你能上传.txt和.html文件,支持php/asp/jsp/asp.net,漏洞存在于小于等于kindeditor4.1.5编辑器中这里

    2022年7月4日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号