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:横着滚动的列表的更多相关文章

  1. Android BaseAdapter Gallery 画廊视图 (左右拖动图片列表拖至中间时图片放大显示)

    画廊视图使用Gallery表示,能够按水平方向显示内容,并且可以手指直接拖动图片和移动,一般用来浏览图片,,被选中的选项位于中间,并且可以响应事件显示信息.在使用画廊视图时,首先在屏幕上添加Galle ...

  2. [转] Android SDK manager 无法获取更新版本列表

      打开这个网址(LINK)就可以看到adt的详细信息. 或者直接在你的eclipse的Help > Install New Software里面add,地址直接输入 https://dl-ss ...

  3. Android Material Design-Creating Lists and Cards(创建列表和卡)-(三)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40430319 翻译自:http://developer.android.com/trainin ...

  4. Android 解决Gallery下ScrollView滑动事件冲突

    在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...

  5. android学习Gallery和ImageSwitch的使用

    Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了, * @deprec ...

  6. Android之Gallery和Spinner-Android学习之旅(二十九)

    Spinner简介 spinner是竖直方向展开一个列表供选择.和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可 ...

  7. android学习---Gallery画廊视图

    Gallery与Spinner有共同父类:AbsPinner.说明Gallery与Spinner都是一个列表框. 它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是 ...

  8. android SDK manager 无法获取更新版本列表【转载】

    http://mirrors.neusoft.edu.cn/eclipse/releases/luna/打开这个网址就可以看到adt的详细信息:  http://developer.android.c ...

  9. Android TextView文字横向自动滚动(跑马灯)

    TextView实现文字滚动需要以下几个要点:   1.文字长度长于可显示范围:android:singleLine="true"   2.设置可滚到,或显示样式:android: ...

随机推荐

  1. 一个c++给程序打log的单例模式类

    开发过程中需要给程序打log. 所以照着网上写了个单例模式的log类 #ifndef MISCLOGWRITER_H_ #define MISCLOGWRITER_H_ #include <io ...

  2. CocoaPods ADD private Spec Repo

    Private Pods CocoaPods is a great tool not only for adding open source code to your project, but als ...

  3. https WebAPi

    前言 话说又来需求了,之前对于在SelfHost中需要嵌套页面并操作为非正常需求,这回来正常需求了,客户端现在加了https,老大过来说WebAPi访问不了了,这是什么情况,我去试了试,还真是这个情况 ...

  4. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  5. 一步一步自定义SpringMVC参数解析器

    随心所欲,自定义参数解析器绑定数据. 题图:from Zoommy 干货 SpringMVC解析器用于解析request请求参数并绑定数据到Controller的入参上. 自定义一个参数解析器需要实现 ...

  6. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  7. Java判断空字符串

    今天碰到java中去判断String是否为空字符串的时候,用了S.length() ==0, s.equals(null), s.isEmpty(), 都失败. 后来用S.trim().equals( ...

  8. WordPress ‘get_allowed_mime_types’函数安全漏洞(2)

    漏洞名称: WordPress ‘get_allowed_mime_types’函数安全漏洞 CNNVD编号: CNNVD-201309-169 发布时间: 2013-09-13 更新时间: 2013 ...

  9. Node.js权威指南 (2) - Node.js中的交互式运行环境——REPL

    2.1 REPL运行环境概述 / 102.2 在REPL运行环境中操作变量 / 102.3 在REPL运行环境中使用下划线字符 / 122.4 在REPL运行环境中直接运行函数 / 122.5 在RE ...

  10. Google Map API 学习六

    今天其实收货很大的 1.new google.maps.Circle 就是如何在地图上标注一个圆 3.getAnimation 在这里是通过获取Marker是否存在动作,然后如果存在动作的话,就将动作 ...