1.AdapteView
AdapteView ¼Ì³ÐViewGroupËüµÄ±¾ÖÊÊÇÈÝÆ÷
AdapterViewÅÉÉúÁË3¸ö×ÓÀà:
AbsListView
AbsSpinner
AdapterViewAnimation
Õâ3¸öÀàÊdzéÏóÀà
ʵ¼ÊʹÓÃÖвÉÓõÄÊÇËüÃǵÄ×ÓÀà

2.Adpate½á¹¹

3.ʹÓÃArrayAdapterÍê³ÉListViewÏÔʾ

ListViewʹÓõÄÊÇListAdapterÀàÐ͵ÄÊÊÅäÆ÷
ÓÉÓÚÖ»ÏÔʾÎÄ×Ö,ËùÓÐʹÓÃArrayAdapter
ArrayAdapter ÊÇListAdapterµÄ×ÓÀà
²¼¾ÖÎļþ
<?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="match_parent"
android:orientation="vertical">
<!--
divider:·Ö¸ôÏßÑÕÉ«
dividerHeight:·Ö¸ôÏ߸߶È
ListView
½¨Òé layout_width:Óë layout_height ¶¼ÉèÖÃΪmatch_parent
ÒªÔ¤ÀÀµÄ»°ÒªÓÐIDÊôÐÔ
-->
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/colorAccent"
android:dividerHeight="2dp">
</ListView>
</LinearLayout> |
Activity
/**
* ListViewʹÓÃ
* ArrayAdapterÊÊÅäÆ÷
public class MainActivity extends AppCompatActivity
{
ListView
private ListView lv;
private String [] items={
"ËïÎò¿Õ",
"Öí°Ë½ä",
"ţħÍõ",
"JAVA",
"Android",
"Ajax",
"XML",
"Spring",
"Hibernate",
"Spring MVC",
"MyBatis"
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
public ArrayAdapter(Context context, @LayoutRes
int resource, @NonNull T[] objects)
context:ÉÏÏÂÎÄ
//ΪListViewÉèÖÃÊÊÅäÆ÷
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,items));
}
} |
android.R.layout.simple_list_item_1
ÄÚ²¿¾ÍÊÇÒ»¸öTextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/> |
4.ʹÓÃSimpleAdapter
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes int[] to) |
context:ÉÏÏÂÎÄ
LIst<? extends Map<String,/.>> data: ¼¯ºÏÀàÐ͵ĶÔÏó,ÿ¸öÔªËØ¶¼ÊÇÒ»¸öMap<String,?>¶ÔÏó
resource:Ìᶨһ¸ö½çÃæ²¼¾ÖµÄID,²¼¾ÖÎļþ
from:¸Ã²ÎÊýÌáÈ¡Map<String,?>µÄkey
to:int[]ÀàÐ͵IJÎÊý¾ö¶¨Ìî³äÄÄЩ¿Ø¼þ

²¼¾ÖÎļþ:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.itheima.listviewsimpleadapter.MainActivity">
<!--
ListView
-->
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
|
listviewÌî³äµÄ²¼¾ÖÎļþ
<?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="match_parent"
android:orientation="horizontal">
<!--
Í·ÏñÏÔʾ
-->
<ImageView
android:id="@+id/iv_icon"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@drawable/libai"/>
<!--
ÓÒ±ßÏÔʾ ÐÕÃûÓëÃèÊö
-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ÕâÀïÏÔʾÐÕÃû"/>
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ÕâÀïÏÔʾÃèÊö"/>
</LinearLayout>
</LinearLayout> |
ActivityÖÐSimpleAdapter
/**
* ListView
* SimpleAdapterʵÏÖ
public class MainActivity extends AppCompatActivity
{
private ListView lv;
ÐÕÃû
private String[] names = {
"ËïÎò¿Õ",
"Öí°Ë½ä",
"ţħÍõ",
"JAVA"
};
* ͼƬ×ÊÔ´ID*/
private int[] resourceID = {
R.drawable.libai,
R.drawable.nongyu,
R.drawable.qingzhao,
R.drawable.tiger
};
private String[] desc = {
"Ò»¸öºï×Ó",
"һͷÖí",
"һͷţ",
"Ò»ÃÅ¿ª·¢ÓïÑÔ"
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//´´½¨Ò»¸öList¼¯ºÏ
List<Map<String, Object>> listItems
= new ArrayList<Map<String, Object>>();
//ΪListÌí¼ÓÔªËØ
for (int i = 0; i < names.length; i++) {
//ÉùÃ÷Ò»¸öMap
Map<String, Object> map = new HashMap<String,
Object>();
map.put("name", names[i]);
map.put("icon", resourceID[i]);
map.put("desc", desc[i]);
//Ìí¼Óµ½¼¯ºÏ
listItems.add(map);
}
//´´½¨Ò»¸öSimpleAdapter
public SimpleAdapter(Context context, List<?
extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes
int[] to) {
context:ÉÏÏÂÎÄ
LIst<? extends Map<String,/.>> data:
¼¯ºÏÀàÐ͵ĶÔÏó,ÿ¸öÔªËØ¶¼ÊÇÒ»¸öMap<String,?>¶ÔÏó
resource:Ìᶨһ¸ö½çÃæ²¼¾ÖµÄID,²¼¾ÖÎļþ
from:¸Ã²ÎÊýÌáÈ¡Map<String,?>µÄkey
to:int[]ÀàÐ͵IJÎÊý¾ö¶¨Ìî³äÄÄЩ¿Ø¼þ
SimpleAdapter simpleAdapter =
new SimpleAdapter(this, listItems, R.layout.simple_item,
new String[]{"name", "icon",
"desc"},new int[]{R.id.tv_name,R.id.iv_icon,R.id.tv_desc});
//ΪlistviewÉèÖÃÊÊÅäÆ÷
lv.setAdapter(simpleAdapter);
}
} |
5.ʹÓÃBaseAdapter
BaseAdapterҪʵÏÖ ÒÔÏ·½·¨:
getCount():·½·¨·µ»Ø¿ØÖƸÃAdapter½«»á°üº¬¶àÉÙ¸öÁбíÏî
getItem(int position): ¸Ã·½·¨µÄ·µ»ØÖµ¾ö¶¨µÚPosition´¦µÄÁбíÏîµÄÄÚÈÝ
getItemId(int position): ¸Ã·½·¨µÄ·µ»ØÖµ¾ö¶¨µÚPosition´¦µÄÁбíÏîµÄÄÚÈݵÄID
getView(int position,View conerView
,ViewGroup parent)
Õâ¸ö·½·¨¾ø¾ö·µ»ØÖµµÚposistion´¦µÄÁбíÏî
5.1.´´½¨Ò»¸öѧÉúÐÅϢ¼Èë,ʹÓÃListViewչʾÐÅÏ¢baseAdapter
 
²¼¾Ö
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="ѧÉúÐÅÏ¢¹ÜÀíϵͳ"
android:textColor="#77ff0000"
android:textSize="29sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ÇëÊäÈëѧÉúÐÕÃû" />
<!-- ÐÔ±ð -->
<RadioGroup
android:id="@+id/rgp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- ÐÔ±ðÄÐ -->
<RadioButton
android:id="@+id/rb_male"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="male" />
<!-- ÐÔ±ðÅ® -->
<RadioButton
android:id="@+id/rb_female"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="female" />
</RadioGroup>
<!-- ±£´æ -->
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="±£´æ" />
<!-- ²éѯ -->
<Button
android:id="@+id/btn_querty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="²éѯȫ²¿Ñ§ÉúÐÅÏ¢" />
<ListView
android:id="@+id/lv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ff0000"
android:dividerHeight="2dp">
</ListView>
</LinearLayout> |
ListViewҪʹÓõIJ¼¾Ö
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="@drawable/mr" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_toRightOf="@id/iv_sex"
>
<TextView
android:id="@+id/tv_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ѧºÅ" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_no"
android:text="ÐÕÃû" />
<ImageView
android:id="@+id/iv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="@drawable/delete" />
</RelativeLayout>
</RelativeLayout>
|
Êý¾Ý¿â´´½¨Àà
/**
* Êý¾Ý¿â´´½¨Àà,Óëά»¤µÄÀà ¼Ì³ÐSQLiteOpenHelper
public class StudentSqliteOpenHelper extends
SQLiteOpenHelper {
private static final String TAG = "StudentSqliteOpenHelper";
public StudentSqliteOpenHelper(Context context)
{
super(context, "student.db", null,
2);
}
* ´´½¨Êý¾Ý¿âʱ,Ö´ÐÐÒ»´Î,Ö»Ö´ÐÐÒ»´Î
* ÓÃÀ´´´½¨Êý¾Ý±í
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "onCreateÖ´ÐÐÁË");
//»ñÈ¡ÔÀ´µÄ°æ±¾,»ñÈ¡²»µ½Îª0
int version = db.getVersion();
if(version==0){
//´´½¨±í
String sql= "create table students ( _id
integer primary key autoincrement, name varchar(30),gender
varchar(10))";
db.execSQL(sql);
}else{
//´´½¨±í
String sql= "create table students ( _id
integer primary key autoincrement, name varchar(30))";
db.execSQL(sql);
}
}
* µ±Êý¾Ý¿â°æ±¾·¢Éú±ä»¯Ê±Ö´ÐÐ
@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
Log.i(TAG, "onUpgradeÖ´ÐÐÁË");
switch (oldVersion) {
case 1:
String sql= "alter table students add gender
varchar(10)";
db.execSQL(sql);
break;
case 2:
break;
default:
break;
}
}
} |
ʵÌåÀà
/**
* ѧÉúµÄ·â×°Àà
public class Student {
* ID
private int id;
* ÐÕÃû
private String name;
* ÐÔ±ð
private String gender;
public Student() {
super();
}
public Student(int id, String name, String gender)
{
super();
this.id = id;
this.name = name;
this.gender = gender;
}
public String getGender() {
return gender;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
|
Êý¾Ý¿â¹¤¾ßÀà
/**
* ѧÉúÊý¾Ý¿â·ÃÎʹ¤¾ßÀà
public class StudentDao {
* Êý¾Ý¿â´´½¨¸üÐÂÀà
private StudentSqliteOpenHelper helper;
public StudentDao(Context context) {
helper = new StudentSqliteOpenHelper(context);
}
* ²åÈëÒ»Ìõ¼Ç¼
* @param name
* ÐÕÃû
* @param gender
* ÐÔ±ð
* @return ·µ»Ø²åÈëºóµÄ_id
public long insert(String name, String gender)
{
// »ñÈ¡Êý¾Ý¿â
SQLiteDatabase db = helper.getWritableDatabase();
// ²åÈëµÄÁÐ
String nullColumnHack = "values(null,?,?)";
// ռλ·û
ContentValues values = new ContentValues();
values.put("name", name);
values.put("gender", gender);
// Ö´ÐÐ
long insert = db.insert("students",
nullColumnHack, values);
// ¹Ø±ÕÁ¬½Ó
db.close();
return insert;
}
* ÐÞ¸ÄÒ»Ìõ¼Ç¼
* @param name
* ÐÕÃû
* @param gender
* ÐÔ±ð
* @return ·µ»Ø²åÈëºóµÄ_id
public long update(String name, String gender)
{
// »ñÈ¡Êý¾Ý¿â
SQLiteDatabase db = helper.getWritableDatabase();
// ²åÈëµÄÁÐ
// ռλ·û
ContentValues values = new ContentValues();
values.put("gender", gender);
// Ö´ÐÐ
int update = db.update("students",
values, "name = ?",
new String[] { name });
// ¹Ø±ÕÁ¬½Ó
db.close();
return update;
}
* ɾ³ýÒ»Ìõ¼Ç¼
* @param name
* ÐÕÃû
* @return ·µ»ØÉ¾³ýºóµÄ_id
public int delete(String name) {
// »ñÈ¡Êý¾Ý¿â
SQLiteDatabase db = helper.getWritableDatabase();
// Ö´ÐÐ
int delete = db.delete("students",
"name = ?", new String[] { name });
// ¹Ø±ÕÁ¬½Ó
db.close();
return delete;
}
* ²éѯȫ²¿Ñ§ÉúÐÅÏ¢
* @return
public List<Student> getAll() {
// ÈÝÆ÷
List<Student> list = new ArrayList<Student>();
// »ñÈ¡Êý¾Ý¿â
SQLiteDatabase db = helper.getReadableDatabase();
String[] columns = { "_id", "name",
"gender" };
Cursor cursor = db.query("students",
columns, null, null, null, null,
null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String gender = cursor.getString(cursor.getColumnIndex("gender"));
// Ìí¼Óµ½¼¯ºÏÖÐ
list.add(new Student(id, name, gender));
}
// ¹Ø±ÕÓαê
cursor.close();
// ¹Ø±Õ
db.close();
return list;
}
}
|
Activity
ѧÉú¹ÜÀíÐÅϢϵͳActivity
* ²½Öè
* 1.»ñÈ¡Óû§ÊäÈëÐÕÃûÓëÑ¡ÔñµÄÐÔ±ð
* 2.ÅжÏÊÇ·ñΪ¿Õ,Ϊ¿Õ¾ÍÌáʾ,²»Îª¿Õ
* 3.±£´æÊý¾Ý
* 4.µã»÷²éѯ,ÔÚÏ·½µÄListViewÖÐÏÔʾËùÓеÄѧÉúÐÅÏ¢
* 5.ΪListViewÉèÖÃBaseAdapter
* 6.ʵÏÖgetCountÓëgetView·½·¨
* 7.·µ»Øview
* 8.µã»÷ɾ³ý,µ¯³öÌáʾ¶Ô»°¿ò,
* 9.È¡ÏûʲôҲ²»×ö,È·¶¨¾Íɾ³ýµ±Ç°¼Ç¼
public class MainActivity extends Activity implements
OnClickListener {
* ÐÕÃû
private EditText et_name;
* ÐÔ±ð
private RadioGroup rgp;
* ±£´æ
private Button btn_save;
* ²éѯ
private Button btn_querty;
* ѧÉúÁбí
private ListView lv_item;
* ѧÉú±íÊý¾Ý¿â²Ù×÷Àà
private StudentDao studentDao;
* ѧÉúÁбí
private List<Student> list;
* ѧÉúÁбíÊÊÅäÆ÷
private StuBaseAdapter stuBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
rgp = (RadioGroup) findViewById(R.id.rgp);
btn_save = (Button) findViewById(R.id.btn_save);
btn_querty = (Button) findViewById(R.id.btn_querty);
lv_item = (ListView) findViewById(R.id.lv_item);
* ³õʼ»¯Êý¾Ý¿âѧÉú±íµÄ¹¤¾ßÀà
studentDao = new StudentDao(this);
* ÉèÖüàÌýʼþ
btn_save.setOnClickListener(this);
btn_querty.setOnClickListener(this);
}
* µ¥»÷ʼþ¼àÌýÆ÷
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
// ±£´æ
save();
break;
case R.id.btn_querty:
// ²éѯ
query();
break;
}
}
* ²éѯѧÉúÁбí
private void query() {
refresh();
}
private void refresh() {
* ²éѯȫ²¿Ñ§ÉúÐÅÏ¢
list = studentDao.getAll();
if (stuBaseAdapter == null) {
// ÅÐ¶Ï Îª¿Õ¾Ínew ,ΪListViewÉèÖÃÊÊÅäÆ÷
stuBaseAdapter = new StuBaseAdapter();
lv_item.setAdapter(stuBaseAdapter);
} else {
// ֪ͨListViewÁбí¸Ä±ä
stuBaseAdapter.notifyDataSetChanged();
}
}
* ±£´æÑ§ÉúÐÅÏ¢
private void save() {
// »ñȡѧÉúÊäÈëµÄÐÅÏ¢
String name = et_name.getText().toString().trim();
// ÅжÏ
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "ѧÉúÐÕÃû²»ÄÜΪ¿Õ", Toast.LENGTH_SHORT).show();
return;
}
// »ñÈ¡ÐÔ±ð
String gender = "male";
switch (rgp.getCheckedRadioButtonId()) {
case R.id.rb_male:
gender = "male";
break;
case R.id.rb_female:
gender = "female";
break;
}
// ¿ªÊ¼±£´æ
long insert = studentDao.insert(name, gender);
if (insert > 0) {
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
et_name.setText("");
} else {
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
}
* ѧÉúÁбíÊÊÅäÆ÷
private class StuBaseAdapter extends BaseAdapter
{
private static final String TAG = "StuBaseAdapter";
@Override
public int getCount() {
return list.size();
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
Log.i(TAG, position + "");
View view;
if (convertView == null) {
view = View.inflate(MainActivity.this, R.layout.stu_item,
null);
} else {
view = convertView;
}
// µÃµ½Ñ§Éú¶ÔÏó
final Student student = list.get(position);
ImageView iv_sex = (ImageView) view.findViewById(R.id.iv_sex);
if ("male".equals(student.getGender()))
{
iv_sex.setImageResource(R.drawable.mr);
} else if ("female".equals(student.getGender()))
{
iv_sex.setImageResource(R.drawable.miss);
}
TextView tv_no = (TextView) view.findViewById(R.id.tv_no);
tv_no.setText("񅧏:" + student.getId());
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
tv_name.setText("ÐÕÃû:" + student.getName());
ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
iv_delete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// ¶Ô»°¿òÀ´ÌáʾÓû§ÊÇ·ñɾ³ý
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("ɾ³ý").setIcon(R.drawable.tools)
.setMessage("È·¶¨ÒªÉ¾³ýÕâÌõ¼Ç¼Âð");
builder.setPositiveButton("È·¶¨",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int
which) {
* ɾ³ý·½·¨
int delete = studentDao.delete(student.getName());
if (delete > 0) {
Toast.makeText(MainActivity.this, "ɾ³ý³É¹¦",
Toast.LENGTH_SHORT).show();
// Ë¢ÐÂÁбí
refresh();
} else {
Toast.makeText(MainActivity.this, "ɾ³ýʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("È¡Ïû", null);
builder.create().show();
}
});
return view;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}
} |
6.GridViewÓëExpandableListView
GridView:ºÍListViewÓй²Í¬µÄ¸¸Àà,¶¼ÊÇAbsListView,ÓëListViewºÜÏàËÆ
Çø±ð:ListViewÖ»ÏÔʾһÁÐ
GridView¿ÉÒÔÏÔʾ¶àÁÐ
numcolumns:ÊôÐÔ¿ÉÒÔÉèÖÃÏÔʾÁÐÊý
ÆäËüÓ÷¨ÓëListViewÒ»Ñù
7.Spinner
SpinnerÆäʵ¾ÍÊÇÒ»¸öÁбíÑ¡Ôñ¿ò,µ¯³öÒ»¸ö²Ëµ¥ÌṩÓû§Ñ¡Ôñ,
Ò²ÐèÒªÉèÖÃAdapter
entries:ʹÓÃXML×ÊÔ´ÎļþÊý¾ÝÉèÖÃÏÂÀ²Ëµ¥
pupupBackground:Áбí Ñ¡Ôñ¿òµÄ±³¾°É«
prompt:ÌáʾÐÅÏ¢

7.1 xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="books">
<item>·è¿ñJAVA½²Òå</item>
<item>Android</item>
<item>JAVA EEÆóÒµÓ¦ÓÃ</item>
<item>Android µÚÒ»ÐдúÂë</item>
</string-array>
</resources> |
7.2 Avtivity
* SpinnerʵÏÖ
* ÉèÖÃÊÊÅäÆ÷
public class MainActivity extends AppCompatActivity
{
//Spinner
private Spinner spinner;
private String [] names={"¶«Ð°","Î÷¶¾","ÄϵÛ","±±Ø¤","ÖÐÉñͨ"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
//ÉèÖÃÊÊÅäÆ÷
ArrayAdapter<String> arrayAdapter
= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
spinner.setAdapter(arrayAdapter);
}
} |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
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.itheima.spinner.MainActivity">
<!--
µÚÒ»¸öSpinnerʹÓÃentriesÅäÖÃXML×ÊÔ´
-->
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/books">
</Spinner>
<!--
µÚ¶þ¸öûÓÐÅäÖÃ,
ÔÚActivityÖÐÉèÖÃAdapter
-->
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
|
8.AdapterViewFlipperµÄ¹¦ÄÜÓëÓ÷¨
AdapterViewFlipper¼Ì³ÐÁËAdapterViewAnimator,ËüÒ²»áÏÔʾAdapterÌṩµÄ¶à¸ö¼¶¼þ,µ«Ëüÿ´ÎÖ»ÄÜÏÔʾһ¸öView×é¼þ,³ÌÐò¿ÉÒÔͨ¹ýshowPrevious()ºÍshowNext()·½·¨¿ØÖÆÏÔʾÉÏÒ»¸ö,ÏÂÒ»¸ö,
ʹÓÃstartFlipper()¿Ø¼þ×Ô¶¯²¥·Å
stopFlipper()Í£Ö¹²¥·Å
8.1ÊôÐÔ:
animateFirstView:µÚÒ»¸öViewÊÇ·ñʹÓö¯»
inAnimation:ÏÔʾʱʹÓö¯»
loopViews:Ñ»·µ½×îºóÒ»¸öʱ,ÊÇ·ñ×Ô¶¯×ªÍ·µ½µÚÒ»¸ö
outAanimation:ÉèÖÃ×é¼þÒþ²ØÊ±Ê¹Óõ͝»
autoStrat()ÊÇ·ñ×Ô¶¯²¥·Å
flipInterval():ÉèÖÃ×Ô¶¯²¥·ÅµÄʱ¼ä
8.2×Ô¶¯²¥·ÅͼƬ

²¼¾ÖÎļþ
<?xml version="1.0" encoding="utf-8"?>
<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.itheima.adapterviewflipper.MainActivity">
<!--
flipInterval 5ÃëÏÔʾʱ¼ä
layout_alignParentTopÓ븸´°Ìå¶¥²¿¶ÔÆë
-->
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:flipInterval="5000"/>
<Button
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="prev"
android:text="ÉÏÒ»¸ö"/>
<Button
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="auto"
android:text="×Ô¶¯²¥·Å"/>
<Button
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="next"
android:text="ÏÂÒ»¸ö"/>
</RelativeLayout>
AdapterÌî³äµÄ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="match_parent">
<ImageView
android:id="@+id/iv_display"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
</LinearLayout> |
ActivityʵÏÖ
* AdapterViewFlipper
* ²½Öè1.»ñÈ¡flipper
* 2.´´½¨×ÊÔ´ÓëÊÊÅäÆ÷
* 3.ΪflipperÉèÖÃÊÊÅäÆ÷
public class MainActivity extends AppCompatActivity
{
flipper
private AdapterViewFlipper flipper;
×ÊԴͼƬ
private int[] imageIds = new int[]
{
R.drawable.shuangzi, R.drawable.shuangyu,
R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu,
R.drawable.mojie};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flipper = (AdapterViewFlipper) findViewById(R.id.flipper);
//ÉùÃ÷ÊÊÅäÆ÷
BaseAdapter baseAdapter = new BaseAdapter()
{
@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View view;
if (convertView == null) {
view = View.inflate(MainActivity.this, R.layout.images,
null);
} else {
view = convertView;
}
ImageView display = (ImageView) view.findViewById(R.id.iv_display);
//ÉèÖÃͼƬ×ÊÔ´
display.setImageResource(imageIds[position]);
//ÉèÖÃImageViewµÄËõ·ÅÀàÐÍ
display.setScaleType(ImageView.ScaleType.FIT_XY);
return view;
}
};
//ÉèÖÃÊÊÅäÆ÷
flipper.setAdapter(baseAdapter);
}
* ÉÏÒ»ÕÅ
public void prev(View v) {
flipper.showPrevious();
flipper.stopFlipping();
}
* ×Ô¶¯²¥·Å
* @param v
*/
public void auto(View v) {
flipper.startFlipping();
}
* ÏÂÒ»ÕÅ
* @param v
public void next(View v) {
flipper.showNext();
flipper.stopFlipping();
}
} |
9.StackView¹¦ÄÜÓëÓ÷¨

StackViewÒ²ÊÇAdapterViewAnimatorµÄ×ÓÀà
ËüÒ²ÊÇÓÃÓÚÏÔʾAdapterÌṩµÄһϵÁÐViewµÄ.StackView½«»áÐĶѵþStackµÄ·½Ê½À´ÏÔʾ¶à¸öÁбíÏî
ÍÏ×ßStackViewÖд¦ÓÚ¶¥¶ËµÄView,ÏÂÒ»View½«»áÏÔʾ³öÀ´,½«ÉÏÒ»¸öViewÍϽøStackView,½«½øÏÔʾ
³öÀ´
showNext(),show()Previous()¿ØÖÆÏÔʾÏÂÒ»¸öÓëÉÏÒ»¸ö
9.1²¼¾Ö
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal"
tools:context="com.itheima.stackview.MainActivity">
<StackView
android:id="@+id/stackView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:loopViews="true">
</StackView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:onClick="prev"
android:text="ÉÏÒ»¸ö"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:onClick="next"
android:text="ÏÂÒ»¸ö"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout> |
Ìî³ä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="match_parent">
<ImageView
android:id="@+id/iv_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout> |
9.2 ʵÏÖ
* StackView
* ²½Öè
* 1.»ñÈ¡stackview
* 2.½¨Á¢ÊÊÅäÆ÷Óë×ÊÔ´
* 3.ÉèÖÃÊÊÅäÆ÷
public class MainActivity extends AppCompatActivity
{
private StackView stackView;
private int[] imageIds = new int[]
{
R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7
, R.drawable.bomb8, R.drawable.bomb9, R.drawable.bomb10
, R.drawable.bomb11, R.drawable.bomb12, R.drawable.bomb13
, R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stackView = (StackView) findViewById(R.id.stackView);
Êý¾Ý¼¯ºÏ
List<Map<String, Object>> listItem
= new ArrayList<Map<String, Object>>();
for (int i = 0; i < imageIds.length; i++)
{
Map<String, Object> map = new HashMap<String,
Object>();
map.put("image", imageIds[i]);
listItem.add(map);
}
SimpleAdapter simpleAdapter =
new SimpleAdapter(this, listItem, R.layout.images,
new String[]{"image"}, new int[]{R.id.iv_display});
//ÉèÖÃÊÊÅäÆ÷
stackView.setAdapter(simpleAdapter);
}
* ÉÏÒ»¸ö
public void prev(View v){
stackView.showPrevious();
}
* ÏÂÒ»¸ö
public void next(View v){
stackView.showNext();
}
} |
|