ExpendableListView(可展開的列表組件)筆記

廢話少說,直接上代碼:

public class ExpendableListViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //創建一個BaseExpandableListAdapter對象,可擴展列表視圖適配器
      ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
          //用數組盛放相應圖片地址
          int [] logos=new int[]{R.drawable.p,
                  R.drawable.z,R.drawable.t};
          //父選項標題
          private String[] armTypes=new String[]{
                  "天堂","地獄","人間"
          };
          //子選項下的內容
          private String[][] arms=new String[][]{
                  {"小A","小B","小C","小D"},{"中A","中B","中D","中D"},{"大A","大B","大C"}
          };
        @Override
        //判斷子列表是否被選中,在相應的圖表上可以綁定相應的事件
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }
        
        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }
        
        @Override
        //得到父框架視圖
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LinearLayout ll=new LinearLayout(ExpendableListViewActivity.this);
            ll.setOrientation(0);
            ImageView  logo=new ImageView(ExpendableListViewActivity.this);
            logo.setImageResource(logos[groupPosition]);
            ll.addView(logo);
            TextView textView=getTextView();
            textView.setText(getGroup(groupPosition).toString());
            ll.addView(textView);
            return ll;
        }
        
        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }
        
        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return armTypes.length;
        }
        
        @Override
        //返回父組件
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return  armTypes[groupPosition] ;
        }
        
        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return arms[groupPosition].length;
        }
        private TextView getTextView(){
            AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
            TextView textView=new TextView(ExpendableListViewActivity.this);
             textView.setLayoutParams(lp);
             textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
             textView.setPadding(36, 0, 0, 0);
             textView.setTextSize(20);
            return textView;
            
        }
        
        @Override
        //G該方法決定每個子選項 的外觀
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView textView=getTextView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }
        //獲取子列表的ID
        @Override
    
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return  childPosition;
        }
        
        @Override
        
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return arms[groupPosition][childPosition];
        }
    };
    ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list);
        expandableListView.setAdapter(adapter);
    }
}
ExpandableListView思路分:一、父選項的getGroupView 、getGroupId、getGroupCount、getGroup 二,子選項的getChildView、getChile、getChild、getChildrenCount
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章