編輯:關於Android編程
1.簡介
基於基於BaseExpandableListAdapter擴展的ExpandableList用法,現在網上流行的主要有兩種:第一種是向BaseExpandableListAdapter傳入兩個數組,第一個是表示Group(目錄頭)信息的一維數組,第二個是表示Child(目錄子項)的二維數組數組;第二種是構建兩個類,一個是表示目錄信息的GroupInfo類,另一個是表示子項信息的ChildInfo類,然後傳入BaseExpandableListAdapter。通過對比發現,第一種方法由於數組是固定的,而實際項目中往往需要動態變化的目錄和子項,因此用處不大,第二種方法文件太多,實現復雜。這裡提供一種方法,傳遞兩個個動態的二維數組來實現目錄結構.
2.代碼
package com.devin; import java.util.ArrayList; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class PadTestActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList3.groupList = new ArrayList (); for (int i = 0; i < 3; i++) { groupList.add("title"); } ArrayList itemList1 = new ArrayList (); itemList1.add("Item1"); itemList1.add("Item2"); ArrayList itemList2 = new ArrayList (); itemList2.add("Item1"); itemList2.add("Item21"); itemList2.add("Item3"); ArrayList itemList3 = new ArrayList (); itemList3.add("Item1"); itemList3.add("Item2"); itemList3.add("Item3"); itemList3.add("Item4"); ArrayList> childList = new ArrayList>(); childList.add(itemList1); childList.add(itemList2); childList.add(itemList3); ExpandableListView list = new ExpandableListView(this); ExpandableListAdapter mAdapter = new MyExpandableListAdapter(groupList, childList); list.setAdapter(mAdapter); list.setCacheColorHint(0x00000000); list.setSelector(new ColorDrawable(Color.TRANSPARENT)); list.setGroupIndicator(null); for (int i = 0; i < mAdapter.getGroupCount(); i++) { list.expandGroup(i); } setContentView(list); } private class MyExpandableListAdapter extends BaseExpandableListAdapter { private ArrayList groupList; private ArrayList> childList; MyExpandableListAdapter(ArrayList groupList, ArrayList> childList) { this.groupList = groupList; this.childList = childList; } public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } private int selectedGroupPosition = -1; private int selectedChildPosition = -1; public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) { this.selectedGroupPosition = selectedGroupPosition; this.selectedChildPosition = selectedChildPosition; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = null; if (convertView == null) { textView = new TextView(PadTestActivity.this); textView.setPadding(32, 10, 0, 10); convertView = textView; } else { textView = (TextView) convertView; } textView.setText(getChild(groupPosition, childPosition).toString()); if (groupPosition == selectedGroupPosition) { if (childPosition == selectedChildPosition) { textView.setBackgroundColor(0xffb6ddee); } else { textView.setBackgroundColor(Color.TRANSPARENT); } } textView.setOnClickListener(new OnClickListener() { public void onClick(View v) { setSelectedPosition(groupPosition, childPosition); notifyDataSetChanged(); } }); return textView; } public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } public int getGroupCount() { return groupList.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout cotain = new LinearLayout(PadTestActivity.this); cotain.setPadding(0, 10, 0, 10); cotain.setGravity(Gravity.CENTER_VERTICAL); ImageView imgIndicator = new ImageView(PadTestActivity.this); TextView textView = new TextView(PadTestActivity.this); textView.setText(getGroup(groupPosition).toString()); textView.setPadding(5, 0, 0, 0); if (isExpanded) { imgIndicator.setBackgroundResource(R.drawable.macro_minus); } else { imgIndicator.setBackgroundResource(R.drawable.macro_plus); } cotain.addView(imgIndicator); cotain.addView(textView); return cotain; } public boolean hasStableIds() { return true; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }
應用層面如何獲得已經安裝應用的大小?網上找了一下有兩種方法:1、直接拿到data目錄下對應的包,然後用File.length()方法獲得。然後會發現和設置裡顯示的大小不同
前言在學習Android內存性能優化時,發現需要對Android系統的內存概況得有個概況了解,便有了此篇文章。這篇文章僅僅介紹相關於內存的,即輔助理解 官方文檔 How
在網上看了比較多的關於Tab的教程,發現都很雜亂。比較多的用法是用TitlePagerTabStrip和ViewPaper。不過TitlePagerTabStrip有個很
這一次我們將會實現一個完整純粹的自定義控件,而不是像之前的組合控件一樣,拿系統的控件來實現;計劃分為三部分:自定義控件的基本部分,自定義控件的觸摸事件的處理和自定義控件的