@SuppressWarnings("ConstantConditions") public static void setEventListener( Object handler, ViewFinder finder, DoubleKeyValueMap<Object, Annotation, Method> value_annotation_method_map) { for (Object value : value_annotation_method_map.getFirstKeys()) { ConcurrentHashMap<Annotation, Method> annotation_method_map = value_annotation_method_map.get(value); for (Annotation annotation : annotation_method_map.keySet()) { try { Method method = annotation_method_map.get(annotation); if (annotation.annotationType().equals(OnClick.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; view.setOnClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnLongClick.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; view.setOnLongClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnFocusChange.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; view.setOnFocusChangeListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnKey.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; view.setOnKeyListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnTouch.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; view.setOnTouchListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnItemClick.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((AdapterView<?>) view) .setOnItemClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnItemLongClick.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((AdapterView<?>) view) .setOnItemLongClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnChildClick.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((ExpandableListView) view) .setOnChildClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnGroupClick.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((ExpandableListView) view) .setOnGroupClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnGroupCollapse.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((ExpandableListView) view) .setOnGroupCollapseListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnGroupExpand.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((ExpandableListView) view) .setOnGroupExpandListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnCheckedChange.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; if (view instanceof RadioGroup) { ((RadioGroup) view) .setOnCheckedChangeListener(new ViewCommonEventListener(handler, method)); } else if (view instanceof CompoundButton) { ((CompoundButton) view) .setOnCheckedChangeListener(new ViewCommonEventListener(handler, method)); } } else if (annotation.annotationType().equals(OnPreferenceClick.class)) { Preference preference = finder.findPreference(value.toString()); if (preference == null) break; preference.setOnPreferenceClickListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnPreferenceChange.class)) { Preference preference = finder.findPreference(value.toString()); if (preference == null) break; preference.setOnPreferenceChangeListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnTabChange.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; ((TabHost) view).setOnTabChangedListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnScrollChanged.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; view.getViewTreeObserver() .addOnScrollChangedListener(new ViewCommonEventListener(handler, method)); } else if (annotation.annotationType().equals(OnScrollStateChanged.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; Method method0 = null, method1 = null; ConcurrentHashMap<Annotation, Method> a_m_map = value_annotation_method_map.get(value); for (Annotation a : a_m_map.keySet()) { if (a.annotationType().equals(OnScrollStateChanged.class)) { method0 = a_m_map.get(a); } else if (a.annotationType().equals(OnScroll.class)) { method1 = a_m_map.get(a); } } ((AbsListView) view) .setOnScrollListener(new ViewCommonEventListener(handler, method0, method1)); } else if (annotation.annotationType().equals(OnItemSelected.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; Method method0 = null, method1 = null; ConcurrentHashMap<Annotation, Method> a_m_map = value_annotation_method_map.get(value); for (Annotation a : a_m_map.keySet()) { if (a.annotationType().equals(OnItemSelected.class)) { method0 = a_m_map.get(a); } else if (a.annotationType().equals(OnNothingSelected.class)) { method1 = a_m_map.get(a); } } ((AdapterView<?>) view) .setOnItemSelectedListener(new ViewCommonEventListener(handler, method0, method1)); } else if (annotation.annotationType().equals(OnProgressChanged.class)) { View view = finder.findViewById((Integer) value); if (view == null) break; Method method0 = null, method1 = null, method2 = null; ConcurrentHashMap<Annotation, Method> a_m_map = value_annotation_method_map.get(value); for (Annotation a : a_m_map.keySet()) { if (a.annotationType().equals(OnProgressChanged.class)) { method0 = a_m_map.get(a); } else if (a.annotationType().equals(OnStartTrackingTouch.class)) { method1 = a_m_map.get(a); } else if (a.annotationType().equals(OnStopTrackingTouch.class)) { method2 = a_m_map.get(a); } } ((SeekBar) view) .setOnSeekBarChangeListener( new ViewCommonEventListener(handler, method0, method1, method2)); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ExpandableListAdapter adapter = new BaseExpandableListAdapter() { private String[] ArticlesPark = new String[] { "前言", "FormWidget", "Layout", "Picker", }; private String[][] Articles = new String[][] { {"写在前面的话", "一些好的和“好的”品质"}, {"TextView", "EditText", "Button", "Switch"}, {"LinearLayout", "RelativeLayout", "TableLayout", "GridLayout"}, {"TimePicker", "DatePicker", "NumberPicker"}, }; TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_HORIZONTAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } @Override public int getGroupCount() { return ArticlesPark.length; } @Override public Object getGroup(int groupPosition) { return ArticlesPark[groupPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getChildrenCount(int groupPosition) { return Articles[groupPosition].length; } @Override public Object getChild(int groupPosition, int childPosition) { return Articles[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); ImageView logo = new ImageView(MainActivity.this); logo.setPadding(50, 0, 0, 0); ll.addView(logo); TextView textView = getTextView(); textView.setTextColor(Color.BLACK); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); ImageView generallogo = new ImageView(MainActivity.this); ll.addView(generallogo); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); expandableListView.setAdapter(adapter); expandableListView.setOnChildClickListener( new OnChildClickListener() { @Override public boolean onChildClick( ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // Toast.makeText(MainActivity.this,"你点击了" + // adapter.getChild(groupPosition, // childPosition),Toast.LENGTH_SHORT).show(); /* * if(groupPosition==0) { if(childPosition==0) { to = new * Intent( * MainActivity.this,com.geno.tutorial.articles.foreword.Foreword * .class); startActivity(to); } else if(child) { to = new * Intent * (MainActivity.this,com.geno.tutorial.articles.foreword.Goodhabit * .class); startActivity(to); } } */ switch (groupPosition) { case 0: switch (childPosition) { case 0: p = new Intent( MainActivity.this, com.geno.tutorial.articles.foreword.Foreword.class); startActivity(p); break; case 1: p = new Intent( MainActivity.this, com.geno.tutorial.articles.foreword.Goodhabit.class); startActivity(p); break; } break; case 1: switch (childPosition) { case 0: p = new Intent( MainActivity.this, com.geno.tutorial.articles.formwidget.Text.class); startActivity(p); break; case 1: p = new Intent( MainActivity.this, com.geno.tutorial.articles.formwidget.Edit.class); startActivity(p); break; case 2: p = new Intent( MainActivity.this, com.geno.tutorial.articles.formwidget.Buttonn.class); startActivity(p); break; case 3: p = new Intent( MainActivity.this, com.geno.tutorial.articles.formwidget.Switchh.class); startActivity(p); break; } break; case 2: switch (childPosition) { case 0: p = new Intent( MainActivity.this, com.geno.tutorial.articles.layout.Linear.class); startActivity(p); break; case 1: p = new Intent( MainActivity.this, com.geno.tutorial.articles.layout.Relative.class); startActivity(p); break; case 2: p = new Intent( MainActivity.this, com.geno.tutorial.articles.layout.Table.class); startActivity(p); break; case 3: p = new Intent(MainActivity.this, com.geno.tutorial.articles.layout.Grid.class); startActivity(p); break; } break; case 3: switch (childPosition) { case 0: p = new Intent( MainActivity.this, com.geno.tutorial.articles.picker.TimeP.class); startActivity(p); break; case 1: p = new Intent( MainActivity.this, com.geno.tutorial.articles.picker.DateP.class); startActivity(p); break; case 2: p = new Intent( MainActivity.this, com.geno.tutorial.articles.picker.NumberP.class); startActivity(p); break; } break; } return false; } }); }