Android入門筆記之源碼解析三
<1>簡介
仿愛奇藝客戶端源碼分析。
<2>關鍵步驟
主要有6個Activity,MainActivity爲主Activity。
佈局方面:title是共享的。
AccountActivity:”我的資料”界面。主要是獲取了title的對象,然後修改了title的文件。
ChannelActivity:分類界面。主要是獲取了title的對象,然後修改了title的文件。
MoreActitvity:更多界面。主要是獲取了title的對象,然後修改了title的文件。
HomeActivity:首頁界面。通過一個GalleryAdapter提供數據源實現了一個Gallery,通過一個BaseExpandableListAdapter提供數據源實現了一個ExpandableListView。Tags數組用來標識各個模塊是否被選中。
SearchActivity:搜索界面。通過一個GridAdapter提供數據源來填充從佈局文件中獲取的2個GridView。
MainActivity:主界面,繼承自TabActivity,主要是下方的幾個Tab,整個佈局文件是多重線性佈局嵌套。該類文件中主要是對幾個Tab文件進行了處理,爲mTabHost增加了各個界面的TabSpec,以及相應的跳轉處理。
<3>知識點
一.創建xml文件,位置:drawable/xxx.xml,同目錄下記得要放相關圖片
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 沒有焦點時的背景圖片 -->
<item android:drawable="@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/>
<!-- 觸摸模式按下時的背景圖片 -->
<item android:drawable="@drawable/handle_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/handle_normal" android:state_enabled="true"/>
<!-- 獲得焦點時的圖片背景 -->
<item android:drawable="@drawable/handle_focused" android:state_focused="true"/>
<!--
android:state_selected是選中
android:state_focused是獲得焦點
android:state_pressed是點擊
android:state_enabled是設置是否響應事件,指所有事件
-->
</selector>
二.使用xml文件:
1.方法一:在listview中配置android:listSelector="@drawable/xxx
或者在listview的item中添加屬性android:background="@drawable/xxx"
2.方法二:Drawabledrawable = getResources().getDrawable(R.drawable.xxx);
ListView.setSelector(drawable);但是這樣會出現列表有時候爲黑的情況,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。
相關屬性:
android:state_selected是選中
android:state_focused是獲得焦點
android:state_pressed是點擊
android:state_enabled是設置是否響應事件,指所有事件
根據這些狀態同樣可以設置button的selector效果。也可以設置selector改變button中的文字狀態。
ExpandableListView
1、簡介
android中有一種expandablelistview,可以擴展的listview,就是那種點擊一下可以擴展出子項,再點一下收縮回去的顯示list。
一個視圖顯示項目的垂直滾動的兩級列表。這不同於ListView,允許有兩級列表。
分組能單獨地被擴展出到顯出它的子項目。各子項目來自ExpandableListAdapter相關的View。
2、重要方法
expandGroup(intgroupPos) :在分組列表視圖中展開一組,
setSelectedGroup(intgroupPosition) :設置選擇指定的組。
setSelectedChild(intgroupPosition, int childPosition, boolean shouldExpandGroup) :設置選擇指定的子項。
getPackedPositionGroup(longpackedPosition) :返回所選擇的組
getPackedPositionForChild(intgroupPosition, int childPosition) :返回所選擇的子項
getPackedPositionType(longpackedPosition) :返回所選擇項的類型(Child,Group)
isGroupExpanded(intgroupPosition) :判斷此組是否展開
<4>代碼及解釋
效果截圖:
源代碼地址:
http://download.csdn.net/detail/gmj4850/6301425