Android ListViewヘッダーフッター効果


ListViewのaddFooterView()メソッドを呼び出してリストにフッターを追加すると、リストの内容が多く、画面サイズを超えるとフッターが見えなくなりますが、一般的には下図のように、ListViewの内容が画面を超えた場合、フッターは画面の下部にあります.ここでは、上図に示すListView効果について説明するとともに、androidで上図のヘッダーフッターの背景色のようにグラデーション効果を実現する方法について説明します.上記の効果を実現するには主にいくつかのRelativeLayoutラベルとListViewの組み合わせを使用すればよい.コードは以下の通りである.
<?xml
 version
="1.0"
 encoding
="utf-8"
?>


<RelativeLayout
 xmlns:android
="http://schemas.android.com/apk/res/android"


	android:orientation
="vertical"
 android:layout_width
="fill_parent"


	android:layout_height
="fill_parent"
>


	<RelativeLayout
 android:id
="@+id/listHeader"


		android:background
="@drawable/jbshape"
 android:layout_alignParentTop
="true"


		android:gravity
="center_horizontal"
 android:layout_width
="fill_parent"


		android:layout_height
="wrap_content"
>


		<TextView
 android:text
="IdeasAndroid     "
 android:textColor
="#000000"


			android:textSize
="18dip"
 android:layout_width
="wrap_content"


			android:layout_height
="wrap_content"
>
</TextView> 


	</RelativeLayout> 


	<RelativeLayout
 android:id
="@+id/listFooter"


		android:background
="@drawable/jbshape"
 android:gravity
="center_horizontal"


		android:layout_alignParentBottom
="true"
 android:layout_width
="fill_parent"


		android:layout_height
="wrap_content"
>


		<Button
 android:id
="@+id/prePage"
 android:layout_width
="wrap_content"


			android:layout_height
="wrap_content"
 android:text
="   "


			android:layout_alignParentLeft
="true"
>
</Button> 


		<Button
 android:layout_width
="wrap_content"


			android:layout_gravity
="right"
 android:layout_height
="wrap_content"


			android:text
="   "
 android:layout_toRightOf
="@id/prePage"
>
</Button> 


	</RelativeLayout> 


	<ListView
 android:id
="@+id/myListView"
 android:layout_width
="fill_parent"


		android:layout_height
="fill_parent"
 android:layout_below
="@id/listHeader"


		android:layout_above
="@id/listFooter"
>


	</ListView> 


</RelativeLayout> 


いくつかのキー:1、ヘッダー(idはlistHeader)で属性androidを使用する:layout_alignParentTop="true"は、ヘッダー部分が親ビューの上部に位置することを宣言します.2、フッター(idはlistFooter)で属性androidを使う:layout_alignParentBottom="true"は、親ビューの下部に位置することを宣言します.3、ListViewで属性androidを使う:layout_below=”@id/listHeader” android:layout_above='@id/listFooter'ListViewはlistHeaderの下、listFooterの上にあることを宣言します.
これにより、ヘッダーフッターの効果が実現します.
どうやってグラデーションを実現したのか見てみましょう.
res/drawableディレクトリの下にjbshapeという新しいディレクトリを作成しました.xmlのファイルです.内容は次のとおりです.
<?xml
 version
="1.0"
 encoding
="utf-8"
?>


<shape
 xmlns:android
="http://schemas.android.com/apk/res/android"


	android:shape
="rectangle"
>


	<gradient
 android:startColor
="#509245"
 android:centerColor
="#3e8532"


		android:endColor
="#509245"
 android:type
="linear"
 android:angle
="90"


		android:centerX
="0.5"
 android:centerY
="0.5"
 />


	<padding
 android:left
="7dp"
 android:top
="7dp"
 android:right
="7dp"


		android:bottom
="7dp"
 />


	<corners
 android:radius
="4dp"
 />


</shape> 


ここではあまり話さないで、あなたが見るとわかると信じて、android:shapeは図形の形式を配置して、主に方形、円形などを含んで、本例では方形です.gradientノードは主に始点色、終点色、中間点の座標、中間点の色、グラデーション角度(90度は上下グラデーション、0は左右グラデーション)を配置し、paddingノードは主に上下左右の余白を配置し、cornersノードは4周の園角の半径を配置する.詳細については、http://www.ideasandroid.com/android/sdk/docs/guide/topics/resources/drawable-resource.html .
グラデーションを使用するのは簡単ですが、最初の部分コードに示すように、android:background="@drawable/jbshape"で背景を先ほど構成したグラデーショングラフィックに設定すればいいです.
では、ここで紹介します.まだわからないところがあれば下のコードをダウンロードして、ローカルでテストしてください.
コードのダウンロード:http://www.ideasandroid.com/android/demo/HeaderBottomListDemo.rar