让我等菜鸟炸锅的自定义ListView
来源:互联网 发布:国外游戏直播软件 编辑:程序博客网 时间:2024/04/26 00:43
自定义ListView学习,学完表示直接蒙逼了…
但是没办法,不能就此放弃了,只能硬着头皮再撸一遍,只能硬上了
先从初级的纯Text的普通ListView撸起,因为普通ListView和自定义ListView都是ListView,肯定有相似之处。
普通ListView
先展示效果:
第1步
新建一个新项目,在activity_main.xml里添加一个ListView控件
<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" tools:context="alex.example.listview.MainActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>
第2步
回到MainActivity.java中,编写如下代码
public class MainActivity extends ActionBarActivity { ListView listView = null; // 首先声明一个String[],并在里面添加内容,这里的内容和内容的个数,就是List中显示的内容和个数 private String[] contentList = { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 上面设置了List的数据,现在需要一个东西把数据添加到List中 // 这里设置一个适配器Adapter,因为上面是内容是数组 // 所以这里要用ArrayAdapter泛型设置为String ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, contentList); // 三个参数依次是当前上下文、ListView子项布局id、要适配的数据 listView = (ListView) findViewById(R.id.listView); listView.setAdapter(adapter); // 将数据通过适配器与ListView适配 } ...}
到这里一个普通的ListView就完成了。
下面再撸自定义ListView
第1步
首先在avitivity_main.xml中添加一个ListView控件,同上。
第2步
先定义一个实体类,作为ListView适配器的适配类型。比如上面ArrayAdapter〈String〉这个适配器的适配类型为String型。
public class Contacts { private String contacts_name; private int contacts_img; public Contacts(String name, int imgId) { this.contacts_name = name; this.contacts_img = imgId; } public String getContacts_name() { return contacts_name; } public int getContacts_img() { return contacts_img; }}
第3步
为我们自定义的ListView子项,指定一个自定义的布局,在layout目录下新建个XML文件
<?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" > <ImageView android:id="@+id/contacts_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/contacts_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:textSize="16sp" /></LinearLayout>
第4步
下面需要新创建一个自定义适配器,这个适配器继承自ArrayAdapter,并将泛型指定为Contacts,前面第2步。新建一个类ContactsAdapter
public class ContactsAdapter extends ArrayAdapter<Contacts> { private int resourceId; // 重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传进来 public ContactsAdapter(Context context, int textViewResourceId, List<Contacts> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } // 又重写了getView方法,这个方法在每个子项被滚动到屏幕内的时候都会被调用 @Override public View getView(int position, View convertView, ViewGroup parent) { // 先通过getItem方法得到当前项Contacts实例 Contacts contacts = getItem(position); // 用LayoutInflater为这个子项加载我们传入的自定义布局 View view = LayoutInflater.from(getContext()).inflate(resourceId, null); // 接着调用View的findViewById方法,分别获取到ImageView 、TextView 的实例 ImageView imageView = (ImageView) view .findViewById(R.id.contacts_image); TextView textView = (TextView) view.findViewById(R.id.contacts_name); // 并分别调用setImageResource、setText设置显示图片和文字 imageView.setImageResource(contacts.getContacts_img()); textView.setText(contacts.getContacts_name()); // 最后将布局返回 return view; }}
最后修改MainActivity中的代码
public class MainActivity extends ActionBarActivity { // 这里不再是建立一个数组了,而是建立了一个泛型为自定义类Contacts的列表 private List<Contacts> contactsList = new ArrayList<Contacts>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 为列表添加数据 initContacts(); // 这里用上了前面自定义的ContactsAdapter适配器,适配器用法还是同普通列表中的一样 ContactsAdapter contactsAdapter = new ContactsAdapter( MainActivity.this, R.layout.contacts, contactsList); ListView listView = (ListView) findViewById(R.id.listView); // 将数据与列表控件适配 listView.setAdapter(contactsAdapter); } private void initContacts() { Contacts i1 = new Contacts("Camera", R.drawable.camera); contactsList.add(i1); Contacts i2 = new Contacts("Cash", R.drawable.cash); contactsList.add(i2); Contacts i3 = new Contacts("Chrome", R.drawable.chrome); contactsList.add(i3); Contacts i4 = new Contacts("Contact", R.drawable.contact1); contactsList.add(i4); Contacts i5 = new Contacts("Documents", R.drawable.documents2); contactsList.add(i5); Contacts i6 = new Contacts("Doubletwist", R.drawable.doubletwist); contactsList.add(i6); Contacts i7 = new Contacts("Drawer", R.drawable.drawer); contactsList.add(i7); Contacts i8 = new Contacts("Ebuddy", R.drawable.ebuddy); contactsList.add(i8); Contacts i9 = new Contacts("Email", R.drawable.email); contactsList.add(i9); Contacts i10 = new Contacts("Db", R.drawable.db); contactsList.add(i10); Contacts i11 = new Contacts("Setting", R.drawable.settings); contactsList.add(i11); Contacts i12 = new Contacts("SMS", R.drawable.sms); contactsList.add(i12); }
到这里就结束了,感觉脑子还是蒙,估计得休息下了。
0 0
- 让我等菜鸟炸锅的自定义ListView
- 一个让我不舒服的自定义键盘
- 菜鸟的我
- 从面试几个案例浅谈我等菜鸟级序员的职业规划
- .NET开源引炸锅,纳德拉的计,微软的谋
- .NET开源引炸锅,纳德拉的计,微软的谋
- 此文一出,程序员的朋友圈炸锅了
- 我在等谁?一篇让我看了不停掉泪的文章…
- fragment、ListFragment使用ListView及自定义Listview等初始化操作
- fragment、ListFragment使用ListView及自定义Listview等初始化操作
- fragment、ListFragment使用ListView及自定义Listview等初始化操作
- fragment、ListFragment使用ListView及自定义Listview等初始化操作
- fragment、ListFragment使用ListView及自定义Listview等初始化操作
- 这一生,让我慢慢等你!
- C# 中,ListView的自定义显示,可用于显示不同的颜色和字体等
- 自定义ListView的Item项在pressed或selected等状态时的颜色
- 自定义ListView的Item项在pressed或selected等状态时的颜色
- 菜鸟我眼中的IPM++
- 关于block使用的5点注意事项
- 接口---interface
- 优衣库电子快报里的“服适人生”
- ganglia监控hadoop各项指标含义
- java.lang.NoClassDefFoundError: javax/mail/Address解决方法
- 让我等菜鸟炸锅的自定义ListView
- 全是老师,淘宝商店形成啦
- MPI学习-MPI_Sendrecv and MPI_Sendrecv_replace
- 工厂模式
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- 水晶报表字体自动缩小
- 路由器mac地址克隆
- 端口大全
- [机器学习实战]k-近邻算法