scrollview gridview
package com.fangdamai.salewinner.ui.customer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.fangdamai.salewinner.R;
import com.fangdamai.salewinner.ui.base.BaseActivity;
import com.fangdamai.salewinner.utils.LogUtil;
import com.fangdamai.salewinner.widget.MyGridView;
public class CustomerEditActivity extends BaseActivity {
ExpandableListView expandListview;
int[] childHeight = new int[7];
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_customer_edit);
getSupportActionBar().setTitle("基本信息编辑");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
expandListview = (ExpandableListView) findViewById(R.id.expandableListView1);
MyExpandListAdapter adapter = new MyExpandListAdapter(mContext);
expandListview.setAdapter(adapter);
setExpandableListViewHeight();
expandListview
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int arg0) {
ViewGroup.LayoutParams params = expandListview
.getLayoutParams();
int orignHeigth = params.height;
// 获取将要展开的子view的高度
int gridviewHeight = childHeight[arg0];
LogUtil.i("oh:" + orignHeigth + " gh:"
+ gridviewHeight);
params.height = orignHeigth - gridviewHeight;
expandListview.setLayoutParams(params);
expandListview.requestLayout();
}
});
expandListview.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int arg0) {
ViewGroup.LayoutParams params = expandListview
.getLayoutParams();
int orignHeigth = params.height;
// 获取将要展开的子view的高度
int gridviewHeight = childHeight[arg0];
LogUtil.i("oh:" + orignHeigth + " gh:"
+ gridviewHeight);
params.height = orignHeigth + gridviewHeight;
expandListview.setLayoutParams(params);
expandListview.requestLayout();
}
});
// for (int i = 0; i < adapter.getGroupCount(); i++) {
// expandListview.expandGroup(i);
// }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void setExpandableListViewHeight() {
ListAdapter listAdapter = expandListview.getAdapter();
int totalHeight = 0;
LogUtil.i("count:" + listAdapter.getCount());
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, expandListview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
LogUtil.i("height:" + totalHeight);
}
ViewGroup.LayoutParams params = expandListview.getLayoutParams();
params.height = totalHeight
+ (expandListview.getDividerHeight() * (listAdapter.getCount() - 1));
LogUtil.i("exheight:" +params.height);
expandListview.setLayoutParams(params);
expandListview.requestLayout();
}
private int getGridViewHeight(GridView gv) {
ListAdapter listAdapter = gv.getAdapter();
int totalHeight = 0;
LogUtil.i("count:" + listAdapter.getCount());
int count = listAdapter.getCount();
int columns = gv.getNumColumns();
int rows = count / columns + count % columns == 0 ? 0 : 1;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, gv);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
LogUtil.i("height:" + totalHeight);
}
int height = totalHeight + (gv.getVerticalSpacing() * (rows - 1));
return height;
}
private void setGridViewHeight(GridView gridview, int height) {
ViewGroup.LayoutParams params = gridview.getLayoutParams();
params.height = height;
gridview.setLayoutParams(params);
gridview.requestLayout();
}
class MyExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private String[] groups = { "购房区域", "购房面积", "房源户型/类型", "购房预算", "置业动机",
"急迫程度", "能否决策" };
private String[][] childs = {
{ "不 限", "园区", "新区", "相城区", "姑苏区", "吴中区" },
getResources().getStringArray(R.array.buid_area),
getResources().getStringArray(R.array.buid_type_room),
getResources().getStringArray(R.array.buy_budget),
getResources().getStringArray(R.array.buy_reason),
getResources().getStringArray(R.array.urgent_degree),
getResources().getStringArray(R.array.canconfirm) };
public MyExpandListAdapter(Context context) {
this.context = context;
}
@Override
public Object getChild(int arg0, int arg1) {
return null;
}
@Override
public long getChildId(int arg0, int arg1) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_section_content, null);
GridView gridview = (GridView) convertView
.findViewById(R.id.gridView1);
gridview.setAdapter(new MyGridviewAdatper(childs[groupPosition]));
int height = getGridViewHeight(gridview);
childHeight[groupPosition] = height;
LogUtil.i("position:" + groupPosition + " h:" + height);
setGridViewHeight(gridview, height);
return convertView;
}
@Override
public int getChildrenCount(int arg0) {
return 1;
}
@Override
public Object getGroup(int arg0) {
return groups[arg0];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int arg0) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_section_header, null);
TextView txtContent = (TextView) convertView
.findViewById(R.id.txt_header);
txtContent.setText(getGroup(groupPosition).toString());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
class MyGridviewAdatper extends BaseAdapter {
private String[] items;
public MyGridviewAdatper(String[] items) {
this.items = items;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int arg0) {
return items[arg0];
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(CustomerEditActivity.this)
.inflate(R.layout.item_grid_content, null);
Button txtContent = (Button) convertView.findViewById(R.id.button1);
txtContent.setText(getItem(position).toString());
return convertView;
}
}
}