Android 高仿QQ 好友分組列表實現的效果如下:
用ExpandableListView實現,
先看Activity的代碼:
- public class BuddyActivity extends Activity {
- ExpandableListView expandablelistview;
- //群組名稱
- private String[] group = new String[] { "在線好友", "我的好友", "我的同事"};
- //好友名稱
- private String[][] buddy = new String[][] {
- { "元芳", "雷丶小賤", "狄大人"},
- {"高太後", "士兵甲", "士兵乙", "士兵丙" },
- { "藝術家", "叫獸", "攻城師", "職業玩家" }};
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_buddy);
-
- expandablelistview= (ExpandableListView) findViewById(R.id.buddy_expandablelistview);
- ExpandableListAdapter adapter=new BuddyAdapter(this,group,buddy);
- expandablelistview.setAdapter(adapter);
- //分組展開
- expandablelistview.setOnGroupExpandListener(new OnGroupExpandListener(){
- public void onGroupExpand(int groupPosition) {
- }
- });
- //分組關閉
- expandablelistview.setOnGroupCollapseListener(new OnGroupCollapseListener(){
- public void onGroupCollapse(int groupPosition) {
- }
- });
- //子項單擊
- expandablelistview.setOnChildClickListener(new OnChildClickListener(){
- public boolean onChildClick(ExpandableListView arg0, View arg1,
- int groupPosition, int childPosition, long arg4) {
- Toast.makeText(BuddyActivity.this,
- group[groupPosition]+" : "+buddy[groupPosition][childPosition],
- Toast.LENGTH_SHORT).show();
- return false;
- }
- });
- }
- }