android UI笔记.docx
《android UI笔记.docx》由会员分享,可在线阅读,更多相关《android UI笔记.docx(34页珍藏版)》请在冰豆网上搜索。
androidUI笔记
ListView
ListView根据Adapter的不同分为:
ArrayAdapter
SimpleAdapter
SimpleCursorAdapter
CustomAdapter自定义Adapter
ArrayAdapter
publicclassMainActivityextendsActivity{
privatestaticfinalString[]songArray={"稳稳的幸福.mp3","稳稳的幸福0.mp3",
"稳稳的幸福1.mp3"};
privateListViewmListView;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView=(ListView)findViewById(R.id.mListView);
ArrayAdaptermAdapter=newArrayAdapter(this,
android.R.layout.simple_list_item_1,songArray);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(newOnItemClickListener(){
@Override
publicvoidonItemClick(AdapterView
>adapterView,Viewview,intindex,
longid){
Toast.makeText(MainActivity.this,songArray[index],Toast.LENGTH_SHORT).show();
}
});
}
}
SimpleAdapter
publicclassMainActivityextendsActivity{
privateArrayListcompanyList;
privateHashMapcompanyMap;
privateListViewmListView;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
companyList=newArrayList();
companyMap=newHashMap();
mListView=(ListView)findViewById(R.id.mListView);
companyMap.put("image",R.drawable.ic_launcher);
companyMap.put("company","中国人力资源网");
companyMap.put("product","HR人力资源管理软件");
companyList.add(companyMap);
companyMap=newHashMap();
companyMap.put("image",R.drawable.ic_launcher);
companyMap.put("company","深圳华为软件有限公司");
companyMap.put("product","华为CC软件和移动应用软件");
companyList.add(companyMap);
companyMap=newHashMap();
companyMap.put("image",R.drawable.ic_launcher);
companyMap.put("company","中兴通讯南研所");
companyMap.put("product","ZsmartBOSS业务运营管理系统");
companyList.add(companyMap);
SimpleAdaptercompanyAdapter=newSimpleAdapter(this,companyList,
R.layout.item,
newString[]{"image","company","product"},newint[]{
R.id.iconImageView,RpanyTextView,
R.id.productTextView});
mListView.setAdapter(companyAdapter);
//ListView列表选择事件
mListView.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener(){
publicvoidonItemSelected(AdapterView
>parent,Viewview,
intposition,longid){
MapinfoMap=(Map)parent.getSelectedItem();
Toast.makeText(MainActivity.this,
"Java语言开发:
"+infoMap.get("product"),
Toast.LENGTH_SHORT).show();
}
publicvoidonNothingSelected(AdapterView
>parent){
}
});
//ListView列表单击事件
mListView.setOnItemClickListener(newAdapterView.OnItemClickListener(){
publicvoidonItemClick(AdapterView
>parent,Viewview,
intposition,longid){
MapinfoMap=(Map)companyList.get(position);
Toast.makeText(
MainActivity.this,
(String)infoMap.get("company")+"开发"
+(String)infoMap.get("product"),
Toast.LENGTH_SHORT).show();
}
});
}
}
SimpleCursorAdapter
自定义Adapter
1.定义item布局
xmlversion="1.0"encoding="utf-8"?
>
android="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
android:
orientation="horizontal">
android:
id="@+id/iconImageView"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_weight="0"/>
android:
id="@+id/companyTextView"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_margin="5dip"
android:
layout_weight="1"
android:
singleLine="true"/>
android:
id="@+id/productTextView"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_margin="5dip"
android:
layout_weight="2"
android:
singleLine="true"/>