読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - ExpandableListView

 

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="1pt"
android:verticalSpacing="1pt"
android:numColumns="4"
android:gravity="center" />
</LinearLayout>

 
MainActivity.java
package com.example.liguofeng.expandablelistview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
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 MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// BaseExpandableListAdapter生成
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
int[] logos = new int[]{R.drawable.logo1, R.drawable.logo2, R.drawable.logo3};
private String[] type = {"logo1", "logo2", "logo3"};
private String[][] detail = {
{"logo1-1", "logo1-2", "logo1-3",},
{"logo2-1", "logo2-2", "logo2-3",},
{"logo3-1", "logo3-2", "logo3-3",}
};

@Override
public int getGroupCount() {
return type.length;
}

@Override
public int getChildrenCount(int groupPosition) {
return detail[groupPosition].length;
}

@Override
public Object getGroup(int groupPosition) {
return type[groupPosition];
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return detail[groupPosition][childPosition];
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

@Override
public View getGroupView(int groupPosition,
boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.HORIZONTAL);

ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);

layout.addView(logo);

TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
layout.addView(textView);

return layout;
}

private TextView getTextView() {
AbsListView.LayoutParams p =
new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);

TextView textView = new TextView(MainActivity.this);

textView.setLayoutParams(p);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(15);
textView.setTextColor(Color.RED);

return textView;
}
};

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.listView);
// Adapter設定
expandableListView.setAdapter(adapter);

}
}