伸展的listview

1 直接繼承ExpandableListActivity
package demo.com.myapplication;

import android.app.ExpandableListActivity;
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.TextView;
import android.widget.Toast;

public class MainActivity extends ExpandableListActivity {
private TextView textView;
//聲明adapter
// 父列表數據
private String[] groups = {“吉林省”, “遼寧省”, “黑龍江省”, “山東省”};
// 子列表數據
private String[][] children = {{ “長春市” }, { “瀋陽市”, “鐵嶺市” }, { “哈爾濱市”, “齊齊哈爾市”, “牡丹江市” }, { “濟南市”, “青島市”, “淄博市”, “濰坊市” }};

private ExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //實例化adapter
    mAdapter = new MyExpandableListAdapter();
    //爲列表設置adapter  取消mExpListView的系統箭頭MainActivity.this.getExpandableListView().setGroupIndicator(null);
    setListAdapter(mAdapter);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    Toast.makeText(this, " 組: " + groups[groupPosition] + " 子: " +children[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

@Override
public void onGroupExpand(int groupPosition) {
    Toast.makeText(this, " 組: " +  groups[groupPosition], Toast.LENGTH_SHORT).show();
    super.onGroupExpand(groupPosition);
}

//自定義Adapter
public class MyExpandableListAdapter extends BaseExpandableListAdapter {


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }
    // 取子列表中的某一項的 View
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }
    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }
    @Override
    public int getGroupCount() {
        return groups.length;
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    // 取父列表中的某一項的 View
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    // 獲取某一項的 View 的邏輯
    private TextView getGenericView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
        textView = new TextView(MainActivity.this);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 10, 10, 10);
        return textView;
    }
}

}
//2 ExpandableListView
package demo.com.myapplication;
import android.app.Activity;
import android.content.Context;
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.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

ExpandableListView mainlistview = null;
List<String> parent = null;
Map<String, List<String>> map = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainlistview = (ExpandableListView) this.findViewById(R.id.main_expandablelistview);

    initData();
    mainlistview.setAdapter(new MyAdapter());
}

// 初始化數據
public void initData() {
    parent = new ArrayList<String>();
    parent.add("parent1");
    parent.add("parent2");
    parent.add("parent3");

    map = new HashMap<String, List<String>>();

    List<String> list1 = new ArrayList<String>();
    list1.add("child1-1");
    list1.add("child1-2");
    list1.add("child1-3");
    map.put("parent1", list1);

    List<String> list2 = new ArrayList<String>();
    list2.add("child2-1");
    list2.add("child2-2");
    list2.add("child2-3");
    map.put("parent2", list2);

    List<String> list3 = new ArrayList<String>();
    list3.add("child3-1");
    list3.add("child3-2");
    list3.add("child3-3");
    map.put("parent3", list3);

}

class MyAdapter extends BaseExpandableListAdapter {

    //得到子item需要關聯的數據
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = parent.get(groupPosition);
        return (map.get(key).get(childPosition));
    }

    //得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //設置子item的組件
    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        String key = MainActivity.this.parent.get(groupPosition);
        String info = map.get(key).get(childPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) MainActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.layout_children, null);
        }
        TextView tv = (TextView) convertView
                .findViewById(R.id.second_textview);
        tv.setText(info);
        return tv;
    }

    //獲取當前父item下的子item的個數
    @Override
    public int getChildrenCount(int groupPosition) {
        String key = parent.get(groupPosition);
        int size=map.get(key).size();
        return size;
    }
    //獲取當前父item的數據
    @Override
    public Object getGroup(int groupPosition) {
        return parent.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return parent.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    //設置父item組件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) MainActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.layout_parent, null);
        }
        TextView tv = (TextView) convertView
                .findViewById(R.id.parent_textview);
        tv.setText(MainActivity.this.parent.get(groupPosition));
        return tv;
    }

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

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

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章