这几天刚碰到这这样的应用需求,为自己记一下。
1、换item背景,包括默认背景和点击背景
2、Item个别TextView使用自定义字体,这使得必须自定义一个Adapter,动态修改字体。
实现效果:
activity布局,很简单就一个listview
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue2"
tools:context=".stopRealActivity" >
<ListView
android:id="@+id/lv_stop_real"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"
android:focusable="true"
>
</ListView>
</LinearLayout>
activity实现:
public class stopRealActivity extends Activity {
private ListView lvLine;
private ArrayList<HashMap<String, Object>> liLine;
private lineStopAdapter adtLine;
public stopRealActivity() {
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_stop_real_time);
lvLine = (ListView) findViewById(R.id.lv_stop_real);
lvLine.setFocusable(false);
liLine = new ArrayList<HashMap<String, Object>>();
adtLine = new lineStopAdapter(this,liLine,
R.layout.list_stop_real,
new String[] {"BusNo", "finalStop", "itemText0", "itemTextMinites","itemTextStops"},
new int[] {R.id.BusNo,R.id.finalStop,R.id.itemText0,R.id.itemTextMinites,R.id.itemTextStops}
);
lvLine.setAdapter(adtLine);
//add items for test
HashMap<String, Object> map = new HashMap<String, Object>();
// map.put("slItemImage", R.drawable.gray_arraw_right);//图像资源的ID\
map.put("BusNo", "121 ");
map.put("finalStop", "XX终点站");
map.put("itemText0", "约");
map.put("itemTextMinites", "12");
map.put("itemTextStops", "分钟/ 2站");
liLine.add(map);
for(int i=0;i<6;i++){
map = new HashMap<String, Object>();
//map.put("slItemImage", R.drawable.white_arraw_right);//图像资源的ID
map.put("BusNo", "223 ");
map.put("finalStop", "XX终点站");
map.put("itemText0", "约");
map.put("itemTextMinites", "34");
map.put("itemTextStops", "分钟/ 6站");
liLine.add(map);
}
lvLine.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap<String, Object> map = liLine.get(arg2);
}
});
}
}
listView的布局:也就是list_stop_real.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/stop_real_lv_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_back_selector"
>
<LinearLayout
android:id="@+id/black_back_text_layout"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/black_back"
android:orientation="horizontal"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_centerVertical="true"
>
<TextView
android:id="@+id/BusNo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/yellow"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
/>
<ImageView
android:id="@+id/rightArraw"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/yellow_arraw_right"
android:layout_marginLeft="2dp"
/>
<TextView
android:id="@+id/finalStop"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/yellow"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
/>
</LinearLayout>
<TextView
android:id="@+id/itemText0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/white"
android:layout_toRightOf="@+id/black_back_text_layout"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/itemTextMinites"
android:layout_height="wrap_content"
android:layout_width="30dp"
android:textSize="30sp"
android:textColor="@color/yellow"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/itemText0"
android:layout_marginLeft="4dp"
/>
<TextView
android:id="@+id/itemTextStops"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/white"
android:layout_toRightOf="@+id/itemTextMinites"
android:layout_centerVertical="true"
/>
<ImageView
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/white_arraw_right"
/>
</RelativeLayout>
ListView的背景更换selector:也就是drawable/blue_back_selector.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/blue_back1"/>
<item android:state_focused="true" android:drawable="@drawable/blue_back1"/>
<item android:drawable="@drawable/blue_back0"/>
</selector>
Adapter 的继承:我这里主要为了使用自己定义的字体,
public class lineStopAdapter extends BaseAdapter {
private int lvLayoutId;
private ArrayList < HashMap < String , Object > > mContentList;
private LayoutInflater mInflater;
private Context mContext;
private String [] keyString;
private int []viewID;
Typeface typeFace;
public lineStopAdapter( Context c, ArrayList < HashMap < String , Object > > contentList, int layout,
String []keyName , int []resId) {
mContentList = contentList;
mContext = c;
lvLayoutId = layout;
mInflater = ( LayoutInflater) mContext. getSystemService( Context . LAYOUT_INFLATER_SERVICE) ;
keyString = new String [ keyName.length ] ;
viewID = new int [ resId.length ] ;
System.arraycopy( keyName , 0, keyString, 0, keyName.length ) ;
System.arraycopy( resId, 0, viewID, 0, resId.length ) ;
typeFace = Typeface.createFromAsset(mContext.getAssets(),"fonts/LCDD.TTF");
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(mContentList!=null)
return mContentList.size();
else
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
if(mContentList!=null)
return mContentList.get(position);
else
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View []itemsView;
if ( convertView != null ) {
itemsView = ( View []) convertView. getTag ( ) ;
} else {
convertView = mInflater. inflate ( lvLayoutId, null ) ;
itemsView = new View[viewID.length];
for(int i=0;i<viewID.length;i++){
itemsView[i] = convertView. findViewById( viewID[i] ) ;
if(itemsView[i] instanceof TextView ){
if(R.id.itemTextMinites == viewID[i]){
//这里判断是否对应那个要使用自定义字体的TextView,然后设置字体
((TextView)itemsView[i]).setTypeface(typeFace);
}
}
}
convertView.setTag(itemsView);
}
HashMap<String,Object >item = mContentList.get(position);
if ( item != null ) {
for(int i=0;i<viewID.length;i++){
if(itemsView[i] instanceof Button ){
((Button)itemsView[i]).setText((String)item.get(keyString[i]));
}
if(itemsView[i] instanceof TextView ){
String str = (String)item.get(keyString[i]);
if(null!=str){
((TextView)itemsView[i]).setText(str);
}
}
}
}
return convertView;
}
}
ListView的使用,一般就是自定义布局,但是有时候更自由的修改界面,还得继承BaseAdapter去实现。