GridViewWIthSubActivityDemo
GridViewWIthSubActivityDemo
拥有一个sub-activity的GridView示例:
显示一系列图片,点击其中之一的话,在新的sub-activity中放大显示。
ImageAdapter是自定义的Adapter,继承自BaseAdapter,是GridView与待展示数据之间的桥梁。
ImageAdapter imageAdapter = new ImageAdapter(this);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleViewActivity.class);
intent.putExtra("position", position);
startActivity(intent);
} });
position参数是被点击的图片在Adapter中的位置,Intent中携带数据应该是position,而不是id。
Intent intent = getIntent();
int position = intent.getExtras().getInt("position");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageResource(imageAdapter.mImageIds[position]);
通过Intent的Extras参数传递数据,putExtras() 和 getExtras().
package com.example.shad_fnst.gridviewwithsubactivitydemo; import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ImageAdapter imageAdapter = new ImageAdapter(this);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleViewActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
MainActivity.java
package com.example.shad_fnst.gridviewwithsubactivitydemo; import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView; public class SingleViewActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_view); Intent intent = getIntent();
int position = intent.getExtras().getInt("position"); ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageResource(imageAdapter.mImageIds[position]);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_single_view, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
SingeViewActivity.java
package com.example.shad_fnst.gridviewwithsubactivitydemo; import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView; /**
* Created by shad-fnst on 2015/08/04.
*/
public class ImageAdapter extends BaseAdapter{
private Context mContext; public Integer[] mImageIds = {
R.drawable.sample0, R.drawable.sample1,
R.drawable.sample2, R.drawable.sample3,
R.drawable.sample4, R.drawable.sample5,
R.drawable.sample6, R.drawable.sample7,
R.drawable.sample0, R.drawable.sample1,
R.drawable.sample2, R.drawable.sample3,
R.drawable.sample4, R.drawable.sample5,
R.drawable.sample6, R.drawable.sample7,
R.drawable.sample0, R.drawable.sample1,
R.drawable.sample2, R.drawable.sample3,
R.drawable.sample4, R.drawable.sample5,
R.drawable.sample6, R.drawable.sample7
}; public ImageAdapter(Context context){
mContext = context;
} @Override
public int getCount() {
return mImageIds.length;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(115, 115));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else{
imageView = (ImageView) convertView;
} imageView.setImageResource(mImageIds[position]);
return imageView;
} @Override
public long getItemId(int position) {
return 0;
} @Override
public Object getItem(int position) {
return null;
}
}
ImageAdapter.java
<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=".MainActivity"> <GridView android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="120dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth">
</GridView> </LinearLayout>
activity_main.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="vertical"> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageview"/> </LinearLayout>
activity_single_view.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shad_fnst.gridviewwithsubactivitydemo" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SingleViewActivity"
android:label="@string/title_activity_single_view" >
</activity>
</application> </manifest>
AndroidManifest.xml
GridViewWIthSubActivityDemo的更多相关文章
随机推荐
- Netsh配置端口
开启防火墙端口 netsh firewall set portopening all 3389 RDP_Rule enable (TCP/UDP) #配置RPC高位端口段Netsh int ipv4 ...
- 如何选择Javascript模板引擎(javascript template engine)?
译者 jjfat 日期:2012-9-17 来源: GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: ...
- 深入剖析 HTML5
作为新一代的 HTML 标准,HTML5 不仅强化了 Web 网页的表现性能,还追加了本地数据库等 Web 应用的功能.虽然 HTML5 标准仍处于完善之中.然而,大部分新版本的浏览器已经能够支持某些 ...
- Redis缓存服务搭建及实现数据读写
发现博客园中好多大牛在介绍自己的开源项目是很少用到缓存,比如Memcached.Redis.mongodb等,今天得空抽时间把Redis缓存研究了一下,写下来总结一下,跟大家一起分享 一下.由于小弟水 ...
- change netbeans look and feel
change netbeans look and feel: 方法一: 下载地址:https://kenai.com/projects/nbsubstance/downloads/directory/ ...
- Python之美[从菜鸟到高手]--深刻理解原类(metaclass)
本来想自己写这篇文章的,可当我读了这篇文章http://blog.jobbole.com/21351/,我打消了这个念头,因为肯定写的没有人家的好,说的通俗易懂,面面俱到.就厚着面皮修改下格式,测试下 ...
- java最简单的方式实现httpget和httppost请求
java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.ht ...
- 使用javaScript解决asp.net中mvc使用ajax提交数组参数的匹配问题
想到在asp.net的mvc中如果使用ajax向服务端传递参数时如果参数是一个类或者是个数组(或List集合)以及更复杂的对象时,服务端总是会发生取不到值的情况,当然网上也有很多解决的例子,但都是在服 ...
- 虚拟文件系统VFS
Linux的文件系统是由虚拟文件系统作为媒介搭建起来的,虚拟文件系统VFS(Virtual File System)是Linux内核层实现的一种架构,为用户空间提供统一的文件操作接口.它在内核内部为不 ...
- WinForm中DefWndProc、WndProc与IMessageFilter的区别
这篇文章主要介绍了WinForm中DefWndProc.WndProc与IMessageFilter的区别,较为详细的分析了WinForm的消息处理机制,需要的朋友可以参考下 一般来说,Win ...