AndroidのExpandableListViewの例のチュートリアルへようこそ。このチュートリアルでは、カテゴリごとにリストデータをグループ化するためにExpandableListViewを実装します。これは、メニューとサブメニューのようなAndroid ListViewです。
Android ExpandableListView
Android ExpandableListViewは、垂直にスクロールする2レベルのリストでアイテムを表示するビューです。これは、ListViewとは異なり、簡単に展開および折りたたむことができるグループとそれに対応する子アイテムを持つことができます。ExpandableListViewAdapterは、このビューに関連付けられたアイテムにデータをロードします。このクラスで使用されるいくつかの重要なメソッドは以下の通りです:
- setChildIndicator(Drawable):現在の状態を表す各アイテムの横にインジケータを表示するために使用されます。子がグループの最後の子である場合、状態
state_last
が設定されます。 - setGroupIndicator(Drawable):グループの横に状態(展開または折りたたみ)を表すインジケータが描画されます。グループが空の場合、状態
state_empty
が設定されます。グループが展開されている場合、状態state_expanded
が設定されます。 - getGroupView():リストグループのヘッダーのビューを返します。
- getChildView():リストの子アイテムの表示を返します。
このクラスで実装されている注目すべきインターフェースは以下の通りです:
- ExpandableListView.OnChildClickListener:展開されたリストの子アイテムがクリックされた時に呼び出されるコールバックメソッドを実装するためにオーバーライドされます。
- ExpandableListView.OnGroupClickListener:展開されたリストのグループヘッダーがクリックされた時に呼び出されるコールバックメソッドを実装するためにオーバーライドされます。
- ExpandableListView.OnGroupCollapseListener:グループが折りたたまれた時に通知するために使用されます。
- ExpandableListView.OnGroupExpandListener:グループが展開された時に通知するために使用されます。
Android ExpandableListView プロジェクトの構造
- A MainActivity that shows the layout with the ExpandableListView
- ExpandableListDataPumpは、List内のランダムなデータを表し、HashMapを使用して子アイテムのデータを対応するグループヘッダーにマッピングします。
- A CustomExpandableListAdapter which provides the MainActivity with the data from the ExpandableListDataPump class/li>
Android ExpandableListViewのコード
activity_main.xmlレイアウトは、RelativeLayout内のExpandableListViewで構成されています。以下に示します:activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="@android:color/darker_gray"
android:dividerHeight="0.5dp" />
</RelativeLayout>
android:indicatorLeftは、アイテムのインジケータの左端です。 注意:XMLのExpandableListView
のandroid:layout_height属性にwrap_contentの値を使用することはできません。親のサイズが厳密に指定されている場合にのみ使用できます。各個別リストのグループヘッダーのレイアウトは以下のようになります:list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/listTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="@android:color/black"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>
子アイテムのレイアウト行は以下のようになります:list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/expandedListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>
ExpandableListDataPump
クラスは以下のように定義されています:
package com.journaldev.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
List<String> cricket = new ArrayList<String>();
cricket.add("India");
cricket.add("Pakistan");
cricket.add("Australia");
cricket.add("England");
cricket.add("South Africa");
List<String> football = new ArrayList<String>();
football.add("Brazil");
football.add("Spain");
football.add("Germany");
football.add("Netherlands");
football.add("Italy");
List<String> basketball = new ArrayList<String>();
basketball.add("United States");
basketball.add("Spain");
basketball.add("Argentina");
basketball.add("France");
basketball.add("Russia");
expandableListDetail.put("CRICKET TEAMS", cricket);
expandableListDetail.put("FOOTBALL TEAMS", football);
expandableListDetail.put("BASKETBALL TEAMS", basketball);
return expandableListDetail;
}
}
上記のコードでは、expandableListDetail
オブジェクトは、グループヘッダーの文字列をそれぞれの子に対応させるために、StringのArrayListを使用してマッピングされます。CustomExpandableListAdapter.java
package com.journaldev.expandablelistview;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
このクラスはBaseExpandableListAdapterを拡張し、基本クラスのメソッドをオーバーライドしてExpandableListViewのビューを提供します。getView()は、指定されたインデックスのアイテムビューにデータを埋め込みます。MainActivity.java
package com.journaldev.expandablelistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListDetail = ExpandableListDataPump.getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Expanded.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
expandableListTitle.get(groupPosition)
+ " -> "
+ expandableListDetail.get(
expandableListTitle.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
).show();
return false;
}
});
}
}
上記のコードでは、前述のすべてのインターフェースを実装しました。簡単にするために、クリックごとにアイテムの名前またはグループの状態を持つToastだけを表示します。しかし、これらは簡単に他の操作に変更することができます。以下は、Androidの展開可能なリストビューを使用したアプリの動作です。 注意:ExpandableListViewsはデフォルトでスクロール可能です。これにより、AndroidのExpandableListViewチュートリアルは終了します。最終的なAndroid ExpandableListViewプロジェクトは以下のリンクからダウンロードできます。
Source:
https://www.digitalocean.com/community/tutorials/android-expandablelistview-example-tutorial