Android开发学习之路--UI之ListView
这里再学习写android的ListView,其实我们都使用过ListView,就像手机的联系人,就是用的ListView了。下面就实现下简单的ListView吧,首先是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" > <ListView
android:id="@+id/list_view"
android:layout_height="match_parent"
android:layout_width="match_parent" /> </LinearLayout>
然后在MainActivity代码中实现如下:
package com.example.jared.uilistview; import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends AppCompatActivity { private String[] data = {"苹果", "香蕉", "梨子", "西瓜", "蓝莓", "葡萄", "橘子",
"甘蔗", "火龙果", "柚子", "荔枝", "猕猴桃"};
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide(); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1, data);
listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
这里定义了ArrayAdapter适配器,用来适配ListView,可以泛型编程,这里只用String,所以用这个倒是挺合适。
运行代码如下:
这里实现的最简单就是文字,下面在添加图片试试。
首先新建fruit类,代码如下:
package com.example.jared.uilistview; /**
* Created by jared on 16/2/9.
*/
public class Fruit {
private String name;
private int imageId; public Fruit (String name, int imageId) {
this.name = name;
this.imageId = imageId;
} public String getName() {
return name;
} public int getImageId() {
return imageId;
}
}
Fruit类主要是提供一个名字和一张图片,所以提供了获取名字和imageid的接口。
接着新建一个fruit.xml用来提供imageview和textview,当然还可以扩展,代码如下:
<?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"> <ImageView
android:id="@+id/fruit_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/> <TextView
android:id="@+id/fruit_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"/> </LinearLayout>
创建自定义适配器FruitAdapter,代码如下:
package com.example.jared.uilistview; 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 java.util.List; /**
* Created by jared on 16/2/9.
*/
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId; public FruitAdapter (Context context, int textViewResourceId,
List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
} public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
最后修改MainActivity代码,如下所示:
package com.example.jared.uilistview; import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<Fruit>();
private ListView listView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide(); setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit, fruitList); listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
} private void initFruits() {
Fruit apple = new Fruit("苹果", R.drawable.apple);
fruitList.add(apple);
Fruit banana = new Fruit("香蕉", R.drawable.banana);
fruitList.add(banana);
Fruit pear = new Fruit("梨子", R.drawable.pear);
fruitList.add(pear);
Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon);
fruitList.add(watermelon);
Fruit grape = new Fruit("葡萄", R.drawable.grape);
fruitList.add(grape);
Fruit orange = new Fruit("橙子", R.drawable.orange);
fruitList.add(orange);
Fruit litchi = new Fruit("荔枝", R.drawable.litchi);
fruitList.add(litchi);
Fruit strawberry = new Fruit("草莓", R.drawable.strawberry);
fruitList.add(strawberry);
Fruit pineapple = new Fruit("菠萝", R.drawable.pineapple);
fruitList.add(pineapple);
}
}
运行看效果如下:
图片和文字都显示着了,那么怎么实现点击的效果呢?那下面就来学习下了。
首先是在MainActivity中添加代码如下:
package com.example.jared.uilistview; import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<Fruit>();
private ListView listView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide(); setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit, fruitList); listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fruit fruit = fruitList.get(i);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
} private void initFruits() {
Fruit apple = new Fruit("苹果", R.drawable.apple);
fruitList.add(apple);
Fruit banana = new Fruit("香蕉", R.drawable.banana);
fruitList.add(banana);
Fruit pear = new Fruit("梨子", R.drawable.pear);
fruitList.add(pear);
Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon);
fruitList.add(watermelon);
Fruit grape = new Fruit("葡萄", R.drawable.grape);
fruitList.add(grape);
Fruit orange = new Fruit("橙子", R.drawable.orange);
fruitList.add(orange);
Fruit litchi = new Fruit("荔枝", R.drawable.litchi);
fruitList.add(litchi);
Fruit strawberry = new Fruit("草莓", R.drawable.strawberry);
fruitList.add(strawberry);
Fruit pineapple = new Fruit("菠萝", R.drawable.pineapple);
fruitList.add(pineapple);
}
}
这里添加了setOnItemClickListener方法,然后就类似于Button一样的。运行看下效果,如下:
ViewList基本上就学习这些知识了。
附:参考《第一行代码》
Android开发学习之路--UI之ListView的更多相关文章
- Android开发学习之路--UI之简单聊天界面
学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下: <?xml version="1.0" encoding="utf-8 ...
- Android开发学习之路--UI之自定义布局和控件
新的一年已经开始了,今天已经是初二了,两天没有学习了,还是要来继续学习下.一般手机的title都是actionbar,就像iphone一样可以后退,可以编辑.这里自定义布局就来实现下这个功能,首先准备 ...
- Android开发学习之路--UI之基本布局
上一篇文章中主要介绍了ui的控件,这里就学习下布局吧.android的基本布局在layout下主要如图: 从上图可以看出有FrameLayout(单帧布局),LinearLayout(线性布局),Ta ...
- Android开发学习之路--UI之初体验
之前都是学习Activity,对于布局都没有做过学习,这里就简单学习下吧.下面看下Android Studio下有哪些控件: 这里分为Widgets,Text Fields,Containers,Da ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
- Android开发学习之路--基于vitamio的视频播放器(二)
终于把该忙的事情都忙得差不多了,接下来又可以开始good good study,day day up了.在Android开发学习之路–基于vitamio的视频播放器(一)中,主要讲了播放器的界面的 ...
- Android开发学习之路--Android Studio cmake编译ffmpeg
最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...
- Android开发学习之路--网络编程之xml、json
一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
随机推荐
- 基于GCC的openMP学习与测试
(一).openMP简述 Open Multiprocessing (OpenMP) 框架是一种功能极为强大的规范,可以帮助您利用 C.C++ 和 Fortran 应用程序中的多个核心带来的好处,是基 ...
- 背景重复样式background-repeat
一.background-repeat属性 在CSS中,使用background-repeat属性可以设置背景图像是否平铺,并且可以设置如何平铺. 语法: background-repeat:取值; ...
- Dynamic Web Module 3.0 requires Java 1.6 or newer.的解决
在项目的pom.xml增加 <build> <finalName>xxxxxxxx</finalName> <plugins> <plugin&g ...
- 面向对象+canvas 倒计时
效果参照网上的,用面向对象改写了一下,只写了自己需要的部分. 1.效果: 实现: //html <canvas id="canvas" width="800px&q ...
- HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
IE8报错误: 用户代理: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .N ...
- 02_版本控制工具SVN
SubVersion: 安装:根据电脑版本选择安装64或32位的subversion,尽量不要选择中文或者有空格的目录安装 版本控制仓库: 创建命令:SVNadmin create 目录 启动SVN服 ...
- PHP 表单 - 必需字段
PHP - 必需字段 在上一章节我们已经介绍了表的验证规则,我们可以看到"Name", "E-mail", 和 "Gender" 字段是必须 ...
- webpack 1.x 配合npm scripts管理多站点
需求场景: 希望通过一个webpack文件夹管理多个站点的打包流程. 假设现在我要为站点domain配置打包流程. npm 添加淘宝镜像 你懂得 vim ~/.npmrc registry = htt ...
- Java经典设计模式之五大创建型模式(附实例和详解)
一.概况 总体来说设计模式分为三大类: (1)创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. (2)结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥 ...
- 【伯乐在线】这些 Git 技能够你用一年了
原文出处: Pyper 欢迎分享原创到伯乐头条 用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回复交 ...