ExpandableListView 里面嵌套GridView实现高度自适应

很早之前做过一个商城的app 也是第一次做安卓。

实现的效果如下:



 

因为一开始做安卓,很多写的代码都不规范,在下面上代码的地方,还请高手指点(勿喷,楼主是自尊心很强的屌丝)

这个效果要解决2个大问题,

第一个是ExpandableListView 如何放置gridview  ,这个比较好做 思路就是adapter里实现

第二个是在ExpandableListView里面展开后,GirdView如何能充满ExpandableListView的item, 就是楼上的效果

好,先来解决第一个。如何放置gridview,

在这里使用的adapter 是BaseExpandableListAdapter  并且实现点击girdview里面item的事件

Java代码  收藏代码
  1. //适配器  
  2.     class madapter extends BaseExpandableListAdapter implements OnItemClickListener {  
  3.   
  4.         @Override  
  5.         public int getGroupCount() {  
  6.             // TODO Auto-generated method stub  
  7.             return alllist.size();  
  8.         }  
  9.   
  10.         @Override  
  11.         public int getChildrenCount(int groupPosition) {  
  12.             return 1;  
  13.         }  
  14.   
  15.         @Override  
  16.         public Object getGroup(int groupPosition) {  
  17.             // TODO Auto-generated method stub  
  18.             return alllist.get(groupPosition).parentnode;  
  19.         }  
  20.   
  21.         @Override  
  22.         public Object getChild(int groupPosition, int childPosition) {  
  23.             // TODO Auto-generated method stub  
  24.             return null;  
  25.         }  
  26.   
  27.         @Override  
  28.         public long getGroupId(int groupPosition) {  
  29.             // TODO Auto-generated method stub  
  30.             return groupPosition;  
  31.         }  
  32.   
  33.         @Override  
  34.         public long getChildId(int groupPosition, int childPosition) {  
  35.             // TODO Auto-generated method stub  
  36.             return childPosition;  
  37.         }  
  38.   
  39.         @Override  
  40.         public boolean hasStableIds() {  
  41.             // TODO Auto-generated method stub  
  42.             return true;  
  43.         }  
  44.   
  45.         @Override  
  46.         public View getGroupView(int groupPosition, boolean isExpanded,  
  47.                 View convertView, ViewGroup parent) {  
  48.             TextView text = createView(getGroup(groupPosition).toString(),getContext());  
  49.   
  50.             return text;  
  51.         }  
  52.           
  53.         @Override  
  54.         public View getChildView(int groupPosition, int childPosition,  
  55.                 boolean isLastChild, View convertView, ViewGroup parent) {  
  56.             layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  57.             ViewGroup item = (ViewGroup)layoutInflater.inflate(R.layout.shops_allshops_type_grid, null); //载入gridview布局  
  58.             grid  = (ShowAllShopsType_list_grid) item.findViewById(R.id.shopstypegridview);// 获取girdview的节点  
  59.             grid.setNumColumns(4);// 设置每行列数  
  60.             grid.setGravity(Gravity.CENTER);// 位置居中  
  61.             grid.setVerticalSpacing(10);  
  62.             grid.setAdapter(gridAdapter(parent.getContext(),(int)getGroupId(groupPosition)));  
  63.             grid.setOnItemClickListener(this);  
  64.             grid.setVisibility(View.VISIBLE);  
  65.               
  66.             return item;  
  67.         }  
  68.           
  69.       
  70.         @Override  
  71.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  72.             // TODO Auto-generated method stub  
  73.             return true;  
  74.         }  
  75.   
  76.         @Override  
  77.         public void onItemClick(AdapterView<?> parent, View view, int position,  
  78.                 long id) {  
  79.               
  80.             Intent intent =new Intent(getContext(),ShowAllShops.class);  
  81.             intent.putExtra("text", ((TextView)view).getText());  
  82.             //getContext().startActivity(intent);  
  83.             shopactivty.setResult(1, intent);  
  84.             shopactivty.finish();  
  85.               
  86. }  
  87.     }  

代码只是提供思路的,详细的哪里不懂可以评论发

第二个问题是gridview的自适应

Java代码  收藏代码
  1. public class ShowAllShopsType_list_grid extends GridView{  
  2.   
  3.     public ShowAllShopsType_list_grid(Context context, AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.     }  
  6.     /** 
  7.      * 设置不滚动 
  8.      */  
  9.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  10.     {  
  11.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  12.                 MeasureSpec.AT_MOST);  
  13.         super.onMeasure(widthMeasureSpec, expandSpec);  
  14.   
  15.     }  

 这里只是重写了onMeasure 方法,这样的重写 在ScrollView里放置ListView的冲突上面一样可以解决。

原文地址:https://www.cnblogs.com/wangfeng520/p/5728333.html