java.lang.Object | ||||||
↳ | android.view.View | |||||
|
↳ | android.view.ViewGroup | ||||
|
|
↳ | android.widget.AdapterView<android.widget.ListAdapter> | |||
|
|
|
↳ | android.widget.AbsListView | ||
|
|
|
|
↳ | android.widget.ListView | |
|
|
|
|
|
↳ | android.widget.ExpandableListView |
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.Toast; /** * last-modified :下午4:23:21 * @author jack chen */ public class MainActivity extends Activity { private ExpandableListView expandList; private String[][] data = new String[][]{{"title1","close"},{"title1","close"},{"title1","close"}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandList = (ExpandableListView) findViewById(android.R.id.list); expandList.setAdapter(new ExpandListAdapter(MainActivity.this,data,expandList)); expandList.setGroupIndicator(null); //more comfortable expandList.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { Toast.makeText(MainActivity.this, "Group position:"+groupPosition, 2000).show(); //unused_code return true; } }); expandList.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, "Child position:"+childPosition, 2000).show(); return true; } }); //open all for(int i=0;i<data.length;i++){ expandList.expandGroup(i,true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
/** * */ package com.example.test; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.ExpandableListView; import android.widget.TextView; /** * last-modified :下午4:23:21 * @author jack chen */ public class ExpandListAdapter extends BaseExpandableListAdapter { private Context context; private LayoutInflater layoutInflater; //inflate layout private ExpandableListView expandListView; private String[][] data; public ExpandListAdapter(Context context,String[][] data,ExpandableListView expandListView){ this.data = data; layoutInflater = LayoutInflater.from(context); this.expandListView = expandListView; } @Override public int getGroupCount() { return data.length; } @Override public int getChildrenCount(int groupPosition) { return data[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return data[groupPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public Object getChild(int groupPosition, int childPosition) { return data[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) { final GroupHolder holder; if(convertView == null){ holder = new GroupHolder(); convertView = layoutInflater.inflate(R.layout.group_item, null); holder.title = (TextView) convertView.findViewById(R.id.group_item_title); holder.close = (Button) convertView.findViewById(R.id.group_item_close); convertView.setTag(holder); }else{ holder = (GroupHolder) convertView.getTag(); } String[] groupStrs = data[groupPosition]; holder.title.setText(groupStrs[0]); holder.close.setText(groupStrs[1]); // holder.close.setVisibility(View.GONE); holder.close.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(isExpanded){ expandListView.collapseGroup(groupPosition); }else{ expandListView.expandGroup(groupPosition,true); } } }); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildHolder holder; if(convertView == null){ convertView = layoutInflater.inflate(R.layout.child_item, null); holder = new ChildHolder(); holder.title = (TextView) convertView.findViewById(R.id.child_item_title); convertView.setTag(holder); }else{ holder = (ChildHolder) convertView.getTag(); } String childrenStr = data[groupPosition][childPosition]; holder.title.setText(childrenStr); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } private class GroupHolder{ TextView title; Button close; } private class ChildHolder{ TextView title; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/groupItem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/group_item_title" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:text="hello" android:textSize="18sp" android:layout_weight="1"/> <Button android:id="@+id/group_item_close" android:layout_width="0dp" android:layout_height="wrap_content" android:text="world" android:layout_weight="1"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/groupItem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/child_item_title" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:text="hello" android:textSize="18sp" android:layout_weight="1"/> </LinearLayout>
Group因为布局中有了按钮,所以点击Group事件会被子控件监听事件阻挡从而不会被触发,设置按钮为View.GONE即可。看起来和ListView差不多。刚刚看起来觉得很难,后来稍微研究下觉得也没什么了。
之后在使用的时候发现Group布局加载7次,搞不懂,后来才发现listView的布局文件的高度设置为wrap_content,so,会不断的去重新计算,重新的布局。stackflower
if (oldBean.getStatus() == Constant.STATUS_ORDERITEM_CANCELITEM) { String remark = oldBean.getRemark(); if (TextUtils.isEmpty(remark)) { remark = resources.getString(R.string.lbVoid); } else { remark += resources.getString(R.string.lbVoid); } holder.valRemark.setText(remark); holder.valRemark.setVisibility(View.VISIBLE); } else { holder.valRemark.setVisibility(View.GONE); }
加载次数过多需要谨慎,特别是Visible和Gone这种。小错误,找半天。
遇到的问题:
我是菜鸟,我在路上。