[android] expandable listview tutorial |
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:id="@+id/ex_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="@drawable/ic_launcher" />
</LinearLayout>
view_list_group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:paddingLeft="10dp"
android:id="@+id/group_item"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
view_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp" >
<TextView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</LinearLayout>
MainActivity.java
package com.example.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<String> mGroup = new ArrayList<String>();
private Map<String, List<String>> mChild = new HashMap<String, List<String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_main );
init();
initView();
}
private void init(){
mGroup.add( "Vegitable" );
mGroup.add( "Fruit" );
List<String> vegList = new ArrayList<String>();
vegList.add( "Potato" );
vegList.add( "Brocoli" );
vegList.add( "Onion" );
vegList.add( "Chilli" );
vegList.add( "Carrot" );
mChild.put( "Vegitable", vegList );
List<String> fruitList = new ArrayList<String>();
fruitList.add( "Strawberry" );
fruitList.add( "Blueberry" );
fruitList.add( "Grapes" );
fruitList.add( "Banana" );
mChild.put( "Fruit", fruitList );
}
private void initView(){
ExpandableListView lv = (ExpandableListView) findViewById( R.id.ex_listview );
ExpandableListAdapter adapter = new ExpandableListAdapter();
lv.setAdapter( adapter );
lv.setOnChildClickListener( new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
return false;
}
});
lv.setOnGroupClickListener( new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return false;
}
});
lv.setOnGroupExpandListener( new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
}
});
lv.setOnGroupCollapseListener( new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
}
private class ExpandableListAdapter extends BaseExpandableListAdapter{
@Override
public Object getChild(int groupPosition, int childPosition) {
return mChild.get( mGroup.get( groupPosition ) ).get( childPosition );
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if ( convertView == null )
convertView = LayoutInflater.from( MainActivity.this ).inflate( R.layout.view_list_item, null );
((TextView)convertView.findViewById( R.id.list_item )).setText( mChild.get( mGroup.get( groupPosition ) ).get( childPosition ) );
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return mChild.get( mGroup.get( groupPosition ) ).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroup.get( groupPosition );
}
@Override
public int getGroupCount() {
return mGroup.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if ( convertView == null )
convertView = LayoutInflater.from( MainActivity.this ).inflate( R.layout.view_list_group, null );
((TextView)convertView.findViewById( R.id.group_item )).setText( mGroup.get( groupPosition ) );
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
android:transcriptMode
새로운 아이템이 추가되었을 때 ( notifyDataSetChanged() ) listview 가 어떤 행동을 할지를 선택한다.
1. disabled : 아무 행동도 하지 않는다.
2. normal : 마지막 아이템이 보이는 상황이면 새로운 아이템이 추가될 때 최하단으로 자동 스크롤한다.
3. alwaysScroll : 어떤 조건이든 상관없이, 새로운 아이템이 추가될 떄 최하단으로 자동 스크롤한다.
indicator_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/indicator_expanded"
android:state_expanded="true" />
<item
android:drawable="@drawable/indicator_collapsed" />
</selector>
'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[android] Bitmap 변환시 exif 정보 유지시키기 (0) | 2015.11.09 |
---|---|
Android Loaders Tutorial (0) | 2015.11.03 |
[android] Google Play Service 는 나쁜놈이었다. (0) | 2015.09.16 |
병렬 프로그램을 위한 어노테이션 (0) | 2015.07.20 |
android 에서 "Read-only file system" 이라는 메세지가 나오며 file operation 이 안 되면... (0) | 2015.06.29 |
댓글