移動測試之ExpandableListView

一、ExpandableListView:
(一)、類結構:
java.lang.Object
   ↳  android.view.View
      ↳  android.view.ViewGroup
         ↳  android.widget.AdapterView<T extends android.widget.Adapter>
            ↳  android.widget.AbsListView
               ↳  android.widget.ListView
                  ↳  android.widget.ExpandableListView

(二)、BaseExpandableListAdapter適配器的類結構:
java.lang.Object
   ↳  android.widget.BaseExpandableListAdapter

【備註:】ExpandableListView還可以使用SimpleExpandableListAdapter適配器加載。
java.lang.Object
   ↳  android.widget.BaseExpandableListAdapter
      ↳  android.widget.SimpleExpandableListAdapter

(三)、UI核心代碼:
1、xml佈局文件及解釋:
<ExpandableListView android:id="@+id/list_main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:listSelector="#00000000" > </ExpandableListView>
<!-- android:cacheColorHint="#00000000" 此屬性用來設置:拖動列表的時候防止出現黑色背景 -->
<!-- android:drawSelectOnTop="false" 此屬性用來設置listview上的背景顏色會不會 擋住(覆蓋)內容 , 如果這是爲false就表示不會覆蓋掉 -->

(四)、java核心代碼:
public class MainActivity extends Activity {
private ExpandableListView expandableListView;
// 設置組視圖的圖片
int[] groupImg = new int[] { R.drawable.wei, R.drawable.shu, R.drawable.wu };
// 設置組視圖的顯示文字
private String[] groupData = new String[] { "魏", "蜀", "吳" };

// 設置子視圖圖片
public int[][] childImg = new int[][] {
{ R.drawable.qq, R.drawable.qq, R.drawable.qq, R.drawable.qq,
R.drawable.qq, R.drawable.qq },
{ R.drawable.qq, R.drawable.qq, R.drawable.qq, R.drawable.qq,
R.drawable.qq, R.drawable.qq },
{ R.drawable.qq, R.drawable.qq, R.drawable.qq, R.drawable.qq,
R.drawable.qq } };

// 設置子視圖顯示文字
private String[][] childData = new String[][] {
{ "夏侯惇", "許褚", "郭嘉", "司馬懿", "楊修" },
{ "馬超", "張飛", "劉備", "諸葛亮", "趙雲" },
{ "呂蒙", "陸遜", "孫權", "周瑜", "孫尚香" } };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 以下是一個取消title的樣式設置
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

expandableListView = (ExpandableListView) findViewById(R.id.list_main);
// 給expandableListView設置適配器
expandableListView.setAdapter(new MyAdapter(MainActivity.this));

// 設置item點擊的監聽器
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// 通過groupPosition和childPosition就能獲取到子列表的文字內容
Toast.makeText(MainActivity.this,
"你點擊了:" + childData[groupPosition][childPosition],
Toast.LENGTH_SHORT).show();
return false;
}
});
}

// 以下是自定義了一個可擴展ListView的適配器。BaseExpandableListAdapter是一個直接繼承於object的對象。
class MyAdapter extends BaseExpandableListAdapter {
private Context context;

// 構造方法,目的是讓context對象傳進類中,方便調用
public MyAdapter(Context context) {
this.context = context;
}

@Override
// 獲取組的個數
public int getGroupCount() {
return groupData.length;
}

@Override
// 獲取組中每個item的內容
public Object getGroup(int groupPosition) {
return groupData[groupPosition];
}

@Override
// 獲取組中每個item的id
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
// 獲取每組中每個子item的個數
public int getChildrenCount(int groupPosition) {
return childData[groupPosition].length;
}

@Override
// 獲取子item的內容
public Object getChild(int groupPosition, int childPosition) {
return childData[groupPosition][childPosition];
}

@Override
// 獲取子item的id
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

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

@Override
// 獲取組視圖。就是生成組中每個item視圖
// 通過這個例子希望大家學會在java中定義佈局和控件。並且在java文件中去定義佈局和控件的屬性。
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 構造一個線性佈局
LinearLayout layout = new LinearLayout(context);
// 讓線性佈局的方向爲水平方向。其實默認情況下就是水平方向。這裏是希望同學們學會這個設置方向的方法
layout.setOrientation(0);
// layout.setBackgroundResource(R.drawable.ic_launcher);
// 構造一個ImageView控件
ImageView logo = new ImageView(context);
// 給這個圖片控件設置內容
logo.setImageResource(groupImg[groupPosition]);
// 設置圖片的內填充。setPadding(50, 0, 0, 0)四個參數的方向分別爲左、上、右、下。
logo.setPadding(50, 0, 0, 0);
// 將控件加入到佈局中
layout.addView(logo);
// 構造一個文本控件
TextView textView = new TextView(context);
// 設置文本的顏色
textView.setTextColor(Color.BLUE);
// 設置內填充邊距
textView.setPadding(36, 0, 0, 0);
// 設置文字大小
textView.setTextSize(20);
// 給文本控件設置內容。就要從給定的數據源中獲取數據
textView.setText(getGroup(groupPosition).toString());
// 將文本控件加入到佈局中
layout.addView(textView);
return layout;
}

@Override
// 獲取子視圖。就是生成組中每個子item視圖。該方法中的註釋同上
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(0);
ImageView headpicView = new ImageView(context);
headpicView
.setImageResource(childImg[groupPosition][childPosition]);
// 設置圖片的內填充。setPadding(100, 0, 0, 0)四個參數的方向分別爲左、上、右、下。
headpicView.setPadding(100, 0, 0, 0);
layout.addView(headpicView);
TextView textView = new TextView(context);
textView.setPadding(30, 0, 10, 10);
textView.setTextSize(16);
textView.setText(getChild(groupPosition, childPosition).toString());
layout.addView(textView);
return layout;
}

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

編輯:千鋒軟件測試

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