android 32 Gallery:横着滚动的列表
Gallery:横着滚动的列表

mainActivity.java
package com.sxt.day05_01; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.sxt.day05_01.entity.GeneralBean; public class MainActivity extends Activity {
Gallery mGallery;
List<GeneralBean> mGenerals;//代表十个军事家的集合
GeneralAdapter mAdapter;
int[] resid={
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();//初始化数据
initView();
setListener();
} private void setListener() {
setOnItemClickListener();
setOnItemLongClickListener(); } private void setOnItemLongClickListener() {
mGallery.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被长按", 2000).show();
return true;
}
});
} private void setOnItemClickListener() {
mGallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被短按", 2000).show();
}
});
} private void initView() {
mGallery=(Gallery) findViewById(R.id.galleryGeneral);
mAdapter=new GeneralAdapter();//创建适配器
mGallery.setAdapter(mAdapter);//关联适配器
} private void initData() {
//将资源中的字符串组数转换为Java数组
String[] names=getResources().getStringArray(R.array.generals);
mGenerals=new ArrayList<GeneralBean>();
for (int i = 0; i < names.length; i++) {
GeneralBean bean=new GeneralBean(resid[i], names[i]);
mGenerals.add(bean);
}
} //适配器
class GeneralAdapter extends BaseAdapter{ @Override
public int getCount() {
return Integer.MAX_VALUE;//设置Gallery的长度为21亿,使得水平滚动的列表到最后的时候又从第一个开始
} @Override
public GeneralBean getItem(int position) {
return mGenerals.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
//滚动的时候显示Gallery中第position位置的单个布局,position从0到21亿,使得水平滚动的列表到最后的时候又从第一个开始
public View getView(int position, View convertView, ViewGroup parent) {
//拿到ListViewItem的布局,转换为View类型的对象
View layout=View.inflate(MainActivity.this, R.layout.item_generals, null);
ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);
TextView tvName=(TextView) layout.findViewById(R.id.tvName); //position%mGenerals.size()使得水平滚动的列表到最后的时候又从第一个开始
GeneralBean bean=mGenerals.get(position%mGenerals.size());
ivThumb.setImageResource(bean.getResid());
tvName.setText(bean.getName());
return layout;//返回第position位置的单个布局,也就是item_generals.xml的LinearLayout
} }
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Gallery
android:id="@+id/galleryGeneral"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spacing="2dp"/> </RelativeLayout>
item_generals.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="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivThumb"
android:layout_width="80dp" 上图下字
android:layout_height="80dp"
android:scaleType="fitXY"
android:src="@drawable/baiqi"/>
<TextView
android:id="@+id/tvName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="白起"
android:textSize="20sp"
android:gravity="center_horizontal"/>
</LinearLayout>
GeneralBean.java
public class GeneralBean {
private int resid;//图片的id值
private String name;//军事家的姓名
public int getResid() {
return resid;
}
android 32 Gallery:横着滚动的列表的更多相关文章
- Android BaseAdapter Gallery 画廊视图 (左右拖动图片列表拖至中间时图片放大显示)
画廊视图使用Gallery表示,能够按水平方向显示内容,并且可以手指直接拖动图片和移动,一般用来浏览图片,,被选中的选项位于中间,并且可以响应事件显示信息.在使用画廊视图时,首先在屏幕上添加Galle ...
- [转] Android SDK manager 无法获取更新版本列表
打开这个网址(LINK)就可以看到adt的详细信息. 或者直接在你的eclipse的Help > Install New Software里面add,地址直接输入 https://dl-ss ...
- Android Material Design-Creating Lists and Cards(创建列表和卡)-(三)
转载请注明出处:http://blog.csdn.net/bbld_/article/details/40430319 翻译自:http://developer.android.com/trainin ...
- Android 解决Gallery下ScrollView滑动事件冲突
在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...
- android学习Gallery和ImageSwitch的使用
Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了, * @deprec ...
- Android之Gallery和Spinner-Android学习之旅(二十九)
Spinner简介 spinner是竖直方向展开一个列表供选择.和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可 ...
- android学习---Gallery画廊视图
Gallery与Spinner有共同父类:AbsPinner.说明Gallery与Spinner都是一个列表框. 它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是 ...
- android SDK manager 无法获取更新版本列表【转载】
http://mirrors.neusoft.edu.cn/eclipse/releases/luna/打开这个网址就可以看到adt的详细信息: http://developer.android.c ...
- Android TextView文字横向自动滚动(跑马灯)
TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true" 2.设置可滚到,或显示样式:android: ...
随机推荐
- ubuntu下openGL的配置方法
This is a simple tutorial to show a new linux user (such as myself) how to setup freeglut and OpenGl ...
- 实用lsof常用命令行
1, 使用 lsof 命令行列出所有打开的文件 # lsof 这可是一个很长的列表,包括打开的文件和网络 上述屏幕截图中包含很多列,例如 PID.user.FD 和 TYPE 等等. FD - Fil ...
- bzoj 3744: Gty的妹子序列 主席树+分块
3744: Gty的妹子序列 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 101 Solved: 34[Submit][Status] Descr ...
- html5--indexedDB
http://www.cnblogs.com/Johnny_Z/archive/2012/11/04/2753331.html http://database.51cto.com/art/201202 ...
- Js 表单序列化
http://www.w3cmm.com/javascript/serialize-form.html
- 初识DirectX和COM
一.COM 1.什么是COM对象 一个COM对象事实上是一个或一套实现了大量接口的C++类 2.COM的优点 不用重新编译你的程序就能升级COM模块 3.COM概貌 4.COM对象的接口 QueryI ...
- unity中的mesh合并
在分析shadowgun时,无意中发现所有的环境建筑运行后,都被合并成一个叫做 "Combined Mesha (root: scene)" 的mesh了,但是没有发现任何合并的脚 ...
- Unity Skin Shader Optimized
Shader "Skin Shader" { Properties { _MainTex ("Diffuse (RGB)", 2D) = "white ...
- Objective-c知识小结
1.创建一个类产生.h和.m两个文件,.h中对用到的变量.方法作声明,.m文件中实现,导入时只导入.h文件,如果直接把方法写在.m文件中,未在.h文件中进行声明,则是私有方法 2.@interfac ...
- JavaScript高级程序设计10.pdf
String类型有几种操作字符串的方法 concat()方法拼接任意多个字符串,不修改原字符串 var stringValue=“hello ”; var result=stringValue.con ...