ListVIEw是一个非常强大的控件,先看看它的简单用法。
首先,我们在主界面的xml代码中添加这段代码
<ListVIEw androID:ID="@+ID/List_item" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" ></ListVIEw>
然后我们在MainActivity中使用适配器来生成界面。
package com.example.ListvIEwtext01;import androIDx.appcompat.app.AppCompatActivity;import androID.app.Activity;import androID.os.Bundle;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;public class MainActivity extends AppCompatActivity { private String[] data={ "aa","bb","cc" ,"aa","bb","cc", "aa","bb","cc", }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //第一个参数上下文环境,第二个参数默认的适配布局,第三个参数是数据 ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), androID.R.layout.simple_expandable_List_item_1,data); ListVIEw ListVIEw=(ListVIEw)findVIEwByID(R.ID.List_vIEw); ListVIEw.setAdapter(adapter); }}
效果如下
自定义ListVIEw1.创建在ListVIEw中的子布局界面
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <ImageVIEw androID:ID="@+ID/image" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" ></ImageVIEw> <TextVIEw androID:ID="@+ID/text" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" ></TextVIEw></linearLayout>
创建要添加的数据类型
package com.example.ListvIEwtext01;public class Fruit { private String name; private int imageID; public Fruit(String name,int imageID) { this.imageID=imageID; this.name=name; } public int getimageID() { return imageID; } public String getname() { return name; }}
创建自己的适配器
package com.example.ListvIEwtext;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import androIDx.annotation.NonNull;import androIDx.annotation.Nullable;import java.util.List;public class FruitAdapter extends ArrayAdapter<Fruit> { private int resouleID; public FruitAdapter(Context context, int textVIEwResouleID, List<Fruit> object) { super(context,textVIEwResouleID,object); resouleID=textVIEwResouleID; } @NonNull @OverrIDe public VIEw getVIEw(int position, @Nullable VIEw convertVIEw, @NonNull VIEwGroup parent) { Fruit fruit=getItem(position);//获取实例 VIEw vIEw= LayoutInflater.from(getContext()).inflate(resouleID,parent,false); ImageVIEw fruitimage=(ImageVIEw)vIEw.findVIEwByID(R.ID.fruit_imag); TextVIEw fruitname=(TextVIEw)vIEw.findVIEwByID(R.ID.fruit_name); fruitimage.setimageResource(fruit.getimageID()); fruitname.setText(fruit.getname()); return vIEw; }}
使用界面
package com.example.ListvIEwtext;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private String data[]= { "hello","jasbd","asnbd","sdhnambd","Sdhabd","sndba" }; private List<Fruit>fruitList=new ArrayList<>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); /* ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this ,androID.R.layout.simple_List_item_1,data);*/ initFruits(); FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.activity_fruit_layout ,fruitList); ListVIEw ListVIEw=(ListVIEw)findVIEwByID(R.ID.List_VIEw); ListVIEw.setAdapter(adapter); } public voID initFruits() { for(int i=0;i<15;i++) { Fruit apple=new Fruit("apple",R.drawable.ic_launcher_background); fruitList.add(apple); } }}
点赞收藏分享文章举报不学好学习不改网名发布了135 篇原创文章 · 获赞 9 · 访问量 7041私信 关注 总结
以上是内存溢出为你收集整理的安卓学习之ListView全部内容,希望文章能够帮你解决安卓学习之ListView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)