ぎじゅつめもブログ

主にアプリ開発の技術メモを残していきます。

【Android】ExpandableListViewの文字色を変える

これからはAndroidの開発メモも残していきます(次の案件がAndroidなので。。)

AndroidSDK付属のレイアウト(android.R.layout.simple_expandable_list_item_1, android.R.layout.simple_expandable_list_item_2)を使ってExpandableListViewをつくる際に、文字色を変える方法です。
(環境:Android4.2.2)

SimpleExpandableListAdapterをオーバーライドします。

※ExpandableListViewの基本的な使い方は、ここを参考にしました。

SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
    getApplicationContext(),
    groupData,
    android.R.layout.simple_expandable_list_item_1,
    new String[] {"group"},
    new int[] {android.R.id.text1},
    childData,
    android.R.layout.simple_expandable_list_item_2,
    new String[] {"name", "group"},
    new int[] {android.R.id.text1, android.R.id.text2}) {
        @Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
	    final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
            final TextView tv1 = (TextView)itemRenderer.findViewById(android.R.id.text1);
            final TextView tv2 = (TextView)itemRenderer.findViewById(android.R.id.text2);
            tv1.setTextColor(0xff000000); // 子リストのタイトルは黒に設定
            tv2.setTextColor(0xff0000ff); // 子リストのサブタイトルは青に設定
            return itemRenderer;
        }
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
	    final View itemRenderer = super.getGroupView(groupPosition, isExpanded, convertView, parent);
            final TextView tv1 = (TextView)itemRenderer.findViewById(android.R.id.text1);
            tv1.setTextColor(0xff0000ff); // 親リストのタイトルは青に設定
            return itemRenderer;				
	}
};
		
ExpandableListView listView = (ExpandableListView)findViewById(R.id.hogelistView);
listView.setAdapter(adapter);

以上です。

※参考
http://stackoverflow.com/questions/16588659/android-changing-color-of-textview-in-simpleexpandablelistadapter