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的更多相关文章
随机推荐
- acdream 1409 Musical 状压DP
链接:http://acdream.info/problem? pid=1409 题意:整个国家有n座城市,每座城市有三种粉丝. 第一种一周看一场音乐剧,挑选的音乐剧是已经在周围城市播放上演过的次数最 ...
- 让python整型计算结果为浮点型
这是个入门知识,我记录下. 在python中,默认情况下,如果表达式中全是整型,结果也会仅仅是整型.有时候没注意会出现意想不到的结果,比如: >>> a = 7 >>&g ...
- 剑指 offer set 6 打印从 1 到 N 的所有数
总结 1. 求全排列的变形题, 有些隐晦, 没看出来
- JVM剖析
JVM剖析 这篇文章详细解释了Java虚拟机的内部架构.以下这幅图展示了Java虚拟机里面的关键组件(是依据Java SE 7版本的Java虚拟机). 这些组件将在下面的两个章节一一展开.第一章节涵盖 ...
- Deepin Linux 安装JDK
最近在自己的笔记本上安装了Deepin Linux,虽然使用过程中发现一些bugs,但是总体感觉还不错,准备把她作为开发系统使用.系统自带的JDK是Open JDK,但是在做JAVA开发的时候通常需要 ...
- The Kernel Newbie Corner: Kernel Debugging Using proc "Sequence" Files--Part 1
转载:https://www.linux.com/learn/linux-career-center/37985-the-kernel-newbie-corner-kernel-debugging-u ...
- MR-为什么MR类的变量喜欢定义在方法外面?
写了一些MR后发现不管是别人写的还是官方给的,比如map方法,变量都是定义在方法外面. 因为之前都是照猫画虎的写的,所以自己也一直这么写,可是为什么呢? 我试了试定义在方法里面,程序照样跑,试了几个程 ...
- Triangular Sums
描述 The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number ...
- 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)[1]
转载:http://quweiprotoss.wap.blog.163.com/ Push data to Solr or have Solr pull it 尽管一个应用通过HTTP方式与Solr通 ...
- PN-Traniger
首先先从Bezier说起: 一条直线上有两个端点,P0和P1,那么直线可以写成 y = kx+b ,其实也就是P(t) = (1-t)P0 + P1 (这是个插值函数),(小注,我时常把这两个 ...