这一章主要是提到adapterview和adapter。
什么是adapterview?猜也猜能猜到,是一种控件View,也是我们经常见到的一些视图,列表,网格,下拉列表等,,而adapter
就是适配器,用来自动填充内容的,这章有难度,做好准备。
来看一下类图:
在这里我们这章只说一些常用的ListView(列表),GridView(网格)和Spinner(下拉列表)。
ListView就类似我们手机QQ好友,聊天界面那样,通过手指上下滑动可以显示。
有多行,每一行是一个组信息,其实叫做一个Item。
由于是第一次写ListView,可以先不用adapter适配器填充内容,用string字符串数组填充,来看一下效果:
数据够多超过一个页面放不下可以上下滑动,也可以点右边滚动条滑动。
便于观察每一个Item,我们设置了item的监听事件,使用Log日志打印一下:
由此也可以看到,第一个Item的位置是0,和数组一样是从0开始算起的。
下面附下代码:
Mainactivity:
package com.example.lv01;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.main_lv);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Log.i("lpl", "你点击了第" + position + "个item");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
界面的xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lv01.MainActivity" >
<ListView
android:id="@+id/main_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0f0"
android:cacheColorHint="#0000000"
android:divider="@android:drawable/divider_horizontal_bright"
android:dividerHeight="2dp"
android:entries="@array/lvcontent"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:scrollbars="vertical" >
</ListView>
</RelativeLayout>
里面有ListView的独特属性:
cacheColorHint是用来设置它的透明度,由于在滑动过程中可能会出现颜色的渐变,为防止发生冲突,最好加上这个透明度为0。
divider是用来设置分割线的样式。我调用了android本地的图片样式。
dividerHeight 是分割线的高度。
entries用来在xml中填充ListView每一个item。lvcontent这个是存放在values里面的strings中的(方便观察,删除了部分item):
fastScrollEnabled 是否出现快速滚动条
注意:默认只有当ListView的内容大于4页时,才会显示快速滑动块。
footerDividersEnabled 在尾部是否显示分割线
headerDividerEnabled 在头部是否显示分割线
scrollbars 出现滚动条的方式(none(没有),vertical(垂直), horizontal(水平))