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: ...
随机推荐
- Silverlight中在MVVM模式下对DatagridRow选择控件封装
在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...
- WPF Window对象
户通过窗口与 Windows Presentation Foundation (WPF) 独立应用程序进行交互.窗口的主要用途是承载可视化数据并使用户可以与数据进行交互的内容.独立 WPF 应用程序使 ...
- WPF样式资源文件简单运用
WPF通过资源来保存一些可以被重复利用的样式,下面的示例展示了简单的资源样式文件的使用: 一.xaml中定义资源及简单的引用 <Window.Resources > <!--wpf窗 ...
- 《深入剖析Tomcat》阅读(一)
第一章 一个简单的Web服务器 该应用程序仅接受位于指定目录的静态资源的请求,如HTML文件和图像文件.它也可以将传入的HTTP请求字节流显示到控制台上.但是,它并不发送任何头信息到浏览器,如日期或者 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 【技术贴】解决127.0.0.1和http://localhost均被拦截跳转到另一个网页
很艰难的历程. 今天安装一个OA系统,要用到http://127.0.0.1输入完成之后,可以进入安装界面,but,我输入完了之后,自动跳到了129129垃圾网站,艹,我真TM服了,我把本地连接网线都 ...
- UIImageView之我的动画为什么停了?UIImageView, highLighted,animationImages
如果你的动画总是停了!停了!停了!不管你想不想都停,这里有个参考,你可以看看!这只是一种可能性!!! 受最近看到段子影响,画风略诡异,不喜勿喷. 最近在“刻”动画!!! 为什么是“刻”,动画写了3周啊 ...
- Java数据类型和MySql数据类型对应一览
类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lang.String 12 CHAR N ...
- random随机函数
SQL> select * from (select ename,job from emp order by dbms_random.value() ) where rownum <5 2 ...
- bzoj3143
之前我们曾经用dp解决过数学期望问题,这次我们用的是解方程的方法首先在编号之前,肯定要求出每条边的期望经过次数然后可以转化为求边端点的期望次数这种做法我一开始接触是noip2013的初赛问题求解,是类 ...