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

liguofeng29’s blog

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

AndroidのActivity - LauncherActivityとExpandableActivity

①LauncherActivity継承クラスをつくる

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

import android.app.ExpandableListActivity;
import android.app.LauncherActivity;
import android.content.Intent;
import android.preference.PreferenceActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MainActivity extends LauncherActivity {

String[] activityNames = {"ExpandableListのActivity表示①",
"ExpandableListのActivity表示②"};
// Activityクラス
Class<?>[] clazzs = {ExpandableListActivityTest.class,ExpandableListActivityTest.class};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, activityNames);

setListAdapter(adapter); // Launcherのリスト
}

@Override
protected Intent intentForPosition(int position) {
return new Intent(MainActivity.this, clazzs[position]);
}
}


②ExpandableActivity継承したクラスつくる(BaseExpandableListAdapter使用)
MainActivity.java
 
package com.example.liguofeng.launcheractivitytest;

import android.app.ExpandableListActivity;
import android.graphics.Color;
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 ExpandableListActivityTest extends ExpandableListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// 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(ExpandableListActivityTest.this);
layout.setOrientation(LinearLayout.HORIZONTAL);

ImageView logo = new ImageView(ExpandableListActivityTest.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(ExpandableListActivityTest.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;
}
};
// Adapter設定
setListAdapter(adapter);
}
}


③AndroidManifeist.xml
 
AndroidManifest.xml
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxxxx.launcheractivitytest" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ExpandableListActivityTest" >
</activity>
</application>
</manifest>