列表视图和适配器的绑定

列表视图既可以使用ListView组件,也可以继承ListActivity。显示可以是ArrayAdapter,也可以是游标SimpleCursorAdapter,还可以是继承BaseAdapter展示其它视图。

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);

//获得通讯录联系人游标对象Cursor
startManagingCursor(c); //实例化列表适配器
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME} ,
new int[] {android.R.id.text1}); setListAdapter(adapter);

14.网格视图(GridView)

网格视图配合BaseAdapter示例,图片缩略图网格显示

public class MainActivity extends Activity {

   private GridView gv;

    @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gv = (GridView)findViewById(R.id.GridView01);
gv.setNumColumns(4); // gv.setNumColumns(3); // String[] strs = {"a","a1","a2","b","b1","b2","c","c1","c2"}; // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_gallery_item,strs); gv.setAdapter(new MyAdapter(this)); } class MyAdapter extends BaseAdapter{ private Integer[] imgs = { R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8, R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8 }; Context context; MyAdapter(Context context){ this.context = context; } public int getCount() { return imgs.length; } public Object getItem(int item) { return item; } public long getItemId(int id) { return id; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(45, 45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView;
} imageView.setImageResource(imgs[position]); return imageView;
}
}
}

15.画廊视图

画廊视图和BaseAdapter配合,底部显示图片缩略图上面显示放大图片

public class MainActivity extends Activity implements OnItemSelectedListener,

          ViewFactory {

   private ImageSwitcher mSwitcher;

   private Integer[] mThumbIds = { R.drawable.sample_thumb_0,
R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,
R.drawable.sample_thumb_7 }; private Integer[] mImageIds = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main); mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out)); Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this); } public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
} public int getCount() {
return mThumbIds.length;
} public Object getItem(int position) {
return position;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext); i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
} @Override
public void onItemSelected(AdapterView<?> adapter, View v, int position,
long id) {
mSwitcher.setImageResource(mImageIds[position]);
} @Override
public void onNothingSelected(AdapterView<?> arg0) { } @Override
public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return i;
}
}

<Android>列表、网格、画廊视图及适配器的绑定的更多相关文章

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

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

  2. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

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

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

  4. Android列表视图(List View)

    Android列表视图(ListView) ListView是一个显示滚动项列表的示视图组(viewgroup),通过使用适配器(Adapter)把这些列表项自动插入到列表中.适配器比如从一个数组或是 ...

  5. Android应用开发学习之画廊视图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 画廊视图Gallery能按水平方向显示一组图片,并可以拖动图片.下面我们来看一个使用画廊视图的例子,其运行效果如下: ...

  6. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

    Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...

  7. Android——列表选择框(Spinner)

    通常情况下,如果列表选择框中要显示的列表项是可知的,那么可以将其保存在数组资源文件中,然后通过数组资源来为列表选择框指定列表项.这样就可以在不编写Java代码的情况下实现一个下拉选择框. 1.在布局文 ...

  8. Android RecyclerView网格布局

    一个简单的网格布局activity_main.xml <?xml version="1.0" encoding="utf-8"?> <andr ...

  9. Android列表控件ListView详解

    ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...

随机推荐

  1. Window10 Electron 开发环境搭建及打包exe程序

    1.安装 Electron 首先要安装Node.js     (安装方法:https://www.cnblogs.com/inkwhite/p/9685520.html) 我这里已经安装好了. 2:安 ...

  2. python3网络爬虫系统学习:第二讲 基本库requests(一)

    之前,我们学习了基本库urllib的相关用法,但是在网页验证.Cookies处理等方面是比较繁琐的,需要用到Handler并且还需自己构建Opener.requests库的出现很好的解决了这个问题,下 ...

  3. ONTAK 2010 aut

    Autostrady https://szkopul.edu.pl/problemset/problem/f2dSBM7JteWHqtmVejMWe1bW/site/?key=statement 题意 ...

  4. Docker - 容器中的tomcat如何使用startup.sh启动

    网上大多介绍的catalina.sh启动,因为docker容器中,无法直接启动startup.sh. 解决方法: 编辑catalina.sh,找到 >> "$CATALINA_O ...

  5. c++ reference can not be reassigned

    #include <iostream> using namespace std; int main () { // declare simple variables int i; int ...

  6. 「日常训练」Skills(Codeforce Round #339 Div.2 D)

    题意(CodeForces 614D) 每个人有\(n(n\le 10^5)\)个技能,技能等级都在\([0,10^9]\)的范围,每个技能有一个当前等级,所有技能的最高等级都为A.一个人的力量被记做 ...

  7. 加油吧 骚年QAQ

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2017 年 11 月 08 日 原地址:https://niaobulashi.com/archives/fighting.html --- 想 ...

  8. python中为什么 if/while/def/class语句需要冒号?

    python中冒号主要用于增强可读性(ABC语言实验的结果之一).考虑一下这个: if a == b print(a) 与 if a == b: print(a) 注意第二种方法稍微容易一些.请进一步 ...

  9. 【icon】 图标组件说明

    小程序默认了几种类型图标,其组件原型如下: <icon type="[success | success_no_circle | info | warn | waiting | can ...

  10. 搜索二维矩阵 II

    描述 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 ...