Android ExpandableListView 樣式自定義
<ExpandableListView android:id="@+id/widget30" android:layout_width="317px" android:layout_height="321px" android:layout_x="2px" android:layout_y="70px" android:drawSelectorOnTop="false"> </ExpandableListView>
public class ExpandableAdapter extends BaseExpandableListAdapter { Activity activity; private List<String> group; private List<List<String>> child; public ExpandableAdapter(Activity a) { activity = a; initialData(); //初始化顯示的數據 } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return child.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override //設置子選項樣式 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub String string = child.get(groupPosition).get(childPosition); return getGenericView(string); } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return group.get(groupPosition); } @Override public int getGroupCount() { // TODO Auto-generated method stub return group.size(); } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override //使用LayoutInflater解析定制的XML文件,然后設置相應的樣式 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub String string = group.get(groupPosition); Context mContext = activity; //LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE); //View layout = inflater.inflate(R.layout.main_listview,null); LayoutInflater inflater = LayoutInflater.from(mContext); View layout = inflater.inflate(R.layout.main_listview,null); TextView my_text = (TextView)layout.findViewById(R.id.ItemTitle); my_text.setText(string); //my_text.set ImageView my_image = (ImageView)layout.findViewById(R.id.ItemImage); my_image.setImageResource(R.drawable.icon_online); return layout; //return getGenericView(string); } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } public void initialData() { group = new ArrayList<String>(); child = new ArrayList<List<String>>(); addInfo("Jerry 18718672108", new String[]{"呼叫"}); addInfo("Kim 18718672108",new String[]{"呼叫"}); addInfo("Landy 18718672108",new String[]{"呼叫"}); } public void addInfo(String p,String[] c) { group.add(p); List<String> item = new ArrayList<String>(); for(int i=0;i<c.length;i++){ item.add(c[i]); } child.add(item); } //View stub to create Group/Children 's View public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setText(s); return text; } public String GetGroupData(int pos) { return group.get(pos); } }
其中關鍵是使用LayoutInflater解析定制的XML文件,實現了用XML來定制ExpandableListView的組樣式。另外,重寫了BaseExpandableListAdapter類,實現了自己的Adapter.
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!