Android实现页面跳转、ListView及其事件
Android实现页面跳转、ListView及其事件
开发工具:Andorid Studio 1.3
运行环境:Android 4.4 KitKat
工程内容
- 进入主页面后,使用ListView实现特定页面
- 点击其中任何一项水果,跳转到另外一个活动,使用Intent转换活动,并使用Bundle传递数据,跳转到特定页面
代码实现
首先在主页面的xml文件中添加ListView控件并给予id
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".FruitList">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fruitListView">
</ListView>
</LinearLayout>
接着设计一个ListView中的Item,用线性水平布局加一个ImageView和一个TextView满足要求,适当调整一下图片的大小和文字的大小以及边距,使得好看一些
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_image"
android:adjustViewBounds="true"
android:maxHeight="@dimen/fruit_image_size"
android:maxWidth="@dimen/fruit_image_size"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/fruit_image_size"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:id="@+id/fruit_name"
android:textSize="@dimen/fruit_name_size"
android:gravity="center_vertical"/>
</LinearLayout>
在主类FruitList中获取ListView的控件,然后给List添加项,新建一个adapter绑定我们建的那个layout和List,最后绑定到ListView中
fruitListView = (ListView)this.findViewById(R.id.fruitListView);
fruitList.add(new Fruit("apple", R.mipmap.apple));
fruitList.add(new Fruit("banana", R.mipmap.banana));
fruitList.add(new Fruit("cherry", R.mipmap.cherry));
fruitList.add(new Fruit("coco", R.mipmap.coco));
fruitList.add(new Fruit("kiwi", R.mipmap.kiwi));
fruitList.add(new Fruit("orange", R.mipmap.orange));
fruitList.add(new Fruit("pear", R.mipmap.pear));
fruitList.add(new Fruit("strawberry", R.mipmap.strawberry));
fruitList.add(new Fruit("watermelon", R.mipmap.watermelon));
FruitAdapter adapter = new FruitAdapter(FruitList.this, R.layout.fruit_item, fruitList);
fruitListView.setAdapter(adapter);
Class Fruit和class FruitAdapter的实现如下
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 this.name;
}
public int getImageId(){
return this.imageId;
}
}
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> fruits){
super(context, textViewResourceId, fruits);
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Fruit fruit = getItem(position);
View view = LayoutInflater.from(this.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;
}
}
点击某一个水果的响应是获取水果的名字,加入到一个Bundle中去,然后绑定到一个Intent,传递到另一个界面
fruitListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listView = (ListView) parent;
Fruit fruit = (Fruit) listView.getItemAtPosition(position);
String name = fruit.getName();
Intent intent = new Intent(FruitList.this, ShowFruitActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
intent.putExtras(bundle);
startActivity(intent);
//FruitList.this.finish();
}
});
长按某一个水果的响应是:找到点击的水果,然后在适配器中删除这一水果,然后适配器发出改变通知,ListView更新删除后的状态
fruitListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
try {
ListView listView = (ListView) parent;
Fruit fruit = (Fruit) listView.getItemAtPosition(position);
FruitAdapter adapter = (FruitAdapter)listView.getAdapter();
adapter.remove(fruit);
adapter.notifyDataSetChanged();
listView.invalidate();
} catch (Exception e) {
Log.d("Hint", "Remove failed!");
return false;
}
return true;
}
});
显示界面中获取传入的Bundle,修改textView中的值即可,给Button添加返回响应
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_fruit);
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
TextView textView = (TextView)this.findViewById(R.id.show_fruit_name);
textView.setText("I love " + name + " !!!");
Button button = (Button)this.findViewById(R.id.btnBack);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent = new Intent(ShowFruitActivity.this, Fruit.class);
//startActivity(intent);
ShowFruitActivity.this.finish();
}
});
}
效果图
初始状态->点击cherry->跳转页面->点击Back ->回到上一个界面->长按banana->删除条目
一些总结
- Import某一个包尽量利用Tab键的自动补全功能,Android Studio会自动添加包进来
- 创建新的一个界面用File->new的方法,避免了手动修改AndroidManifest.xml的错误
- Intent之间进行跳转有技巧,主界面跳转后不需要调用finish函数,而跳转显示的界面只需要调用finish结束即可回到上一个界面,即主界面
- 删除功能要在适配器中删除条目才有效,这点通过OverflowStack找到了解决方案,之前一直都不能成功
分别说明活动生存期在什么时候调用下列函数
- onCreate()
活动被创建的时候或者进程被杀死后用户重新启动该活动时,调用这个函数。 - onStart()
在onCreate()或onRestart()函数调用完成后,仅接着调用这个函数。 - onResume()
在onResume()函数调用完成后或被pause的活动重新回到前台时,仅接着调用这个函数。 - onPause()
当从一个activity跳转到另一个activity的时候,当前activity会调用这个函数。 - onStop()
如果一个已经start的活动长时间没有出现在屏幕中,则调用这个函数。 - onDestory()
当一个已经stop的活动关闭被系统回收资源的时候,调用这个函数 - onRestart()
当一个已经stop的活动需要重新显示在屏幕的时候,调用这个函数
引用一张图来说明,图片来自水印:
活动的启动模式有四种,standard,singleTop,singleTask以及singleInstance,列表说明不同的启动方式有什么不同
活动模式 | 区别 |
---|---|
standard | 默认模式。只要创建了Activity实例,一旦激活该Activity,则会向任务栈中加入新创建的实例,退出Activity则会在任务栈中销毁该实例 |
singleTop | 考虑当前要激活的Activity实例在任务栈中是否正处于栈顶,如果处于栈顶则无需重新创建新的实例,会重用已存在的实例,否则会在任务栈中创建新的实例 |
singleTask | 如果任务栈中存在该模式的Activity实例,则把栈中该实例以上的Activity实例全部移除,调用该实例的newInstance()方法重用该Activity,使该实例处於栈顶位置,否则就重新创建一个新的Activity实例 |
singleInstance | Activity实例在任务栈中创建后,只要该实例还在任务栈中,即只要激活的是该类型的Activity,都会通过调用实例的newInstance()方法重用该Activity,此时使用的都是同一个Activity实例,它都会处于任务栈的栈顶。此模式一般用于加载较慢的,比较耗性能且不需要每次都重新创建的Activity |
工程下载
传送门:下载
Android实现页面跳转、ListView及其事件的更多相关文章
- Android first --- 页面跳转及数据传递
页面跳转即数据传递 创建第二个界面Acivity *需要在清单文件中添加配置一个Actuvity标签 标签中如果带有这个子节点,则会在Android中添加一个快捷图标 <intent-filte ...
- Android项目页面跳转小Demo
近期在做Android项目的开发,刚刚接触会有非常多新东西须要学习,从环境的搭建到语言的熟悉都是须要一步步完毕的,接下来就拿一个页面跳转的样例来和大家分享一下自己的心得体会. 採用的架构: Demo中 ...
- Android tab_Host页面跳转,传值,刷新等问题汇总
之前做了一个项目是关于Tab_Host的,现在完成了恰逢闲余写份总结,主要涉及里面遇到问题以及解决方案的. (首先说明这份代码是在eoe 下载的,这里感谢分享的那位朋友,限于我的工程是公司的不能拿出来 ...
- Android 实现页面跳转并传递参数教程
首先我们来看一下实现的功能: 第二,我们看一下实现这个功能,总共会接触到哪些文件和代码. 1.实现本功能总共涉及如下6个文件 2.实现本功能,总共涉及如下6个文件中的如下代码: (1) 效果: ...
- navigator导航页面跳转与绑定事件
效果图: 1. 新建一个index页面 主页面分为两块,上面是导航条,下面是轮播图. 导航条: <view class='menu'> <scroll-view scroll-x&g ...
- Android实现页面跳转及传递参数的方法
简单的示例 实现的效果是这样的: 第一个页面有一个按钮,一个文本框,点击按钮,将文本框的内容传递到第二个页面,并显示在第二个页面中. 首先是在给按钮添加点击事件 setOnClickListener( ...
- Android中实现activity的页面跳转并传值
一个Android应用程序很少会只有一个Activity对象,如何在多个Activity之间进行跳转,而且能够互相传值是一个很基本的要求. 本次我们就讲一下,Android中页面跳转以及传值的几种方式 ...
- .Net程序猿玩转Android开发---(11)页面跳转
在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值 ...
- [Android应用开发] 04.页面跳转和数据传输
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
随机推荐
- WWF3追踪功能<WWF第六篇>
WWF工作流提供了Tracking跟踪功能来对工作流实例及其所包含的活动在运行时的状态进行跟踪,以便用户在需要时可以通过这些历史信息进行分析.WWF的Tracking跟踪功能是通过"SqlT ...
- 【MySQL】SQL语句嵌套1
mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...
- CSS3 图片悬浮缩放效果
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- AnyCAD三维控件场景节点详解
SceneNode是AnyCAD三维图形平台的AnyViz显示引擎的核心对象之一,只有放在场景管理器(SceneManager)里的节点才能被显示引擎所显示. 1. 节点分类 SceneNode是 ...
- win7如何设置某个软件不弹出用户账户控制
手动修改注册表: 在 HKEY_CURRENT_USERS\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers 键下面 ...
- POJ C程序设计进阶 编程题#1:单词翻转
编程题#1:单词翻转 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一 ...
- foreach 和 list.foreach 初步测试
单纯从速度上讲 小数据量下foreach 较快,list.Foreach 由于 public void ForEach(Action<T> action) { ; i <this._ ...
- php生成随机字符串和验证码的类
网上有很多的php随机数与验证码的代码与文章,真正适用的没有几个. 索性自己搞一个吧. 开始本节的php教程 吧,以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_ch ...
- php中的一些小细节(1)
<?php $a=false; echo $a; //var_dump($a); ?> 输出结果为: (即为空): 为什么会这样子? 查看官网对echo的相关资料得出: 结论:ec ...
- android ping网络是否成功
public static boolean pingHost(String str) { //str 为要ping的IP地址 boolean result = false; try { Process ...