解决android expandablelistview 里面嵌入gridview行数据重复问题

最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字。

很简单一个功能,先看效果图:

 

结果发现一个问题,就是每个分类里的数据存在重复插入好几次的现象;

 

ExpertGridAdapter.java  gridview的adapter

 

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. public class ExpertGridAdapter extends BaseAdapter {  
  2.         private LayoutInflater inflater;  
  3.         private CsBlogger[] datals;  
  4.           
  5.         private Context mContext;  
  6.         private String TAG="ExpertGridAdapter";  
  7.        
  8.         public ExpertGridAdapter(Context context,List<CsBlogger> childs) {  
  9.             this.inflater = LayoutInflater.from(context);  
  10.               
  11.             datals=new CsBlogger[childs.size()];  
  12.             childs.toArray(datals);  
  13.        
  14.             CsLog.d("maomao", "childs.size:"+childs.size());  
  15.             mContext=context;  
  16.         }  
  17.        
  18.         @Override  
  19.         public int getCount() {  
  20.             return datals.length;  
  21.         }  
  22.        
  23.         @Override  
  24.         public Object getItem(int position) {  
  25.             return datals[position];  
  26.         }  
  27.        
  28.         @Override  
  29.         public long getItemId(int position) {  
  30.             return position;  
  31.         }  
  32.        
  33.         @Override  
  34.         public View getView(int position, View convertView, ViewGroup parent) {  
  35.             ViewHolder holder;  
  36.             if (convertView==null) {  
  37.                 holder=new ViewHolder();  
  38.                 convertView=this.inflater.inflate(R.layout.csblog_expert_item_menu, null);  
  39.                 holder.iv=(ImageView) convertView.findViewById(R.id.item_image);  
  40.                 holder.tv=(TextView) convertView.findViewById(R.id.item_text);  
  41.                 convertView.setTag(holder);  
  42.             }  
  43.             else {  
  44.                holder=(ViewHolder) convertView.getTag();  
  45.             }  
  46.             CsBlogger expert=datals[position];  
  47.   
  48.               
  49.             DisplayImageOptions options; // 显示图像设置  
  50.             ImageLoader imageLoader = ImageLoader.getInstance();// 得到图片加载器  
  51.             // 图片加载器初始化  
  52.             imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));  
  53.             // 使用DisplayImageOptions.Builder()创建DisplayImageOptions  
  54.             options = new DisplayImageOptions.Builder()  
  55.                     .showStubImage(R.drawable.blogger) // 设置图片下载期间显示的图片  
  56.                     .showImageForEmptyUri(R.drawable.blogger) // 设置图片Uri为空或是错误的时候显示的图片  
  57.                     .showImageOnFail(R.drawable.blogger) // 设置图片加载或解码过程中发生错误显示的图片  
  58.                     .cacheInMemory() // 设置下载的图片是否缓存在内存中  
  59.                     .cacheOnDisc() // 设置下载的图片是否缓存在SD卡中  
  60.                     .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片  
  61.                     .build(); // 创建配置过得DisplayImageOption对象  
  62.               
  63.               
  64.               
  65.   
  66.               
  67.          // 异步加载图片  专家头像  
  68.                     imageLoader  
  69.                             .displayImage(expert.getUserface(),  holder.iv, options);  
  70.               
  71.               
  72.             holder.tv.setText(expert.getNickname());  
  73.             return convertView;  
  74.         }  
  75.         private class ViewHolder{  
  76.             ImageView iv;  
  77.             TextView tv;  
  78.         }  
  79.        
  80.   
  81. }  



 

ExpertsListViewAdapter.java  可扩展的listview

 

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. public class ExpertsListViewAdapter extends BaseExpandableListAdapter implements  
  2.         OnItemClickListener  
  3. {  
  4.     public static final int ItemHeight = 136;// 每项的高度  
  5.     public static final int PaddingLeft = 96;// 每项的高度  
  6.     private int myPaddingLeft = 0;  
  7.   
  8.     private List<TreeNode> treeNodes = new ArrayList<TreeNode>();  
  9.   
  10.     private Context parentContext;  
  11.   
  12.     private LayoutInflater layoutInflater;  
  13.   
  14.   
  15.     public ExpertsListViewAdapter(Context view, int myPaddingLeft,List<TreeNode> treeNodes)  
  16.     {  
  17.         parentContext = view;  
  18.         this.myPaddingLeft = myPaddingLeft;  
  19.         this.treeNodes=treeNodes;  
  20.     }  
  21.   
  22.   
  23.     public void RemoveAll()  
  24.     {  
  25.         treeNodes.clear();  
  26.     }  
  27.   
  28.     public CsBlogger getChild(int groupPosition, int childPosition)  
  29.     {  
  30.         return treeNodes.get(groupPosition).childs.get(childPosition);  
  31.     }  
  32.     @Override  
  33.     public int getChildrenCount(int groupPosition)  
  34.     {  
  35.         //return treeNodes.get(groupPosition).childs.size();  
  36.         //  返回值必须为1,否则会重复数据  
  37.         return 1;  
  38.     }  
  39.   
  40.      public TextView getTextView(Context context)  
  41.     {  
  42.         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  43.                 ViewGroup.LayoutParams.FILL_PARENT, ItemHeight);  
  44.   
  45.         TextView textView = new TextView(context);  
  46.         textView.setLayoutParams(lp);  
  47.         textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  48.         return textView;  
  49.     }  
  50.   
  51.       
  52.        
  53.      class ExpandableListHolder {    
  54.          CsGridView grdiview;    
  55.   
  56.         }    
  57.        
  58.     private ExpertGridAdapter adatper;  
  59.     /** 
  60.      * 可自定义ExpandableListView 
  61.      */  
  62.     public View getChildView(int groupPosition, int childPosition,  
  63.             boolean isLastChild, View convertView, ViewGroup parent)  
  64.     {  
  65.         CsLog.d("maomao", "getChildView, groupPosition:"+groupPosition+",childs size:"+treeNodes.get(groupPosition).childs.size());  
  66.         //if (convertView == null)  
  67.         {     
  68.             CsLog.d("maomao", "converView is null");  
  69.             layoutInflater = (LayoutInflater) parentContext  
  70.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  71.             convertView = layoutInflater.inflate(R.layout.csblog_expert_view, null);  
  72.               
  73.   
  74.               
  75.             CsGridView toolbarGrid = (CsGridView) convertView  
  76.                     .findViewById(R.id.GridView_toolbar);  
  77.             toolbarGrid.setNumColumns(4);// 设置每行列数  
  78.             toolbarGrid.setGravity(Gravity.CENTER);// 位置居中  
  79.             toolbarGrid.setHorizontalSpacing(10);// 水平间隔  
  80.             adatper=new ExpertGridAdapter(parentContext,treeNodes.get(groupPosition).childs);  
  81.             toolbarGrid.setAdapter(adatper);// 设置菜单Adapter  
  82.             toolbarGrid.setOnItemClickListener(this);  
  83.             adatper.notifyDataSetChanged();  
  84.         }  
  85.         //else{  
  86.         //  CsLog.d("maomao", "convertView!=null");  
  87.         //}  
  88.           
  89.         return convertView;  
  90.     }  
  91.   
  92.     /** 
  93.      * 可自定义list 
  94.      */  
  95.     public View getGroupView(int groupPosition, boolean isExpanded,  
  96.             View convertView, ViewGroup parent)  
  97.     {  
  98.         TextView textView = getTextView(this.parentContext);  
  99.         String gname=getGroup(groupPosition).toString()+"("+treeNodes.get(groupPosition).childs.size()+")";  
  100.         textView.setText(gname);  
  101.         textView.setPadding(myPaddingLeft + PaddingLeft, 0, 0, 0);  
  102.         return textView;  
  103.     }  
  104.       
  105.   
  106.   
  107.     public long getChildId(int groupPosition, int childPosition)  
  108.     {  
  109.         return childPosition;  
  110.     }  
  111.   
  112.     public Object getGroup(int groupPosition)  
  113.     {  
  114.         return treeNodes.get(groupPosition).parent;  
  115.     }  
  116.   
  117.     public int getGroupCount()  
  118.     {  
  119.         return treeNodes.size();  
  120.     }  
  121.   
  122.     public long getGroupId(int groupPosition)  
  123.     {  
  124.         return groupPosition;  
  125.     }  
  126.   
  127.     public boolean isChildSelectable(int groupPosition, int childPosition)  
  128.     {  
  129.         return true;  
  130.     }  
  131.   
  132.     public boolean hasStableIds() {    
  133.         return true;    
  134.     }    
  135.     
  136.   
  137.     @Override  
  138.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  139.             long id)  
  140.     {  
  141.         Toast.makeText(parentContext, "当前选中的是:" + position, Toast.LENGTH_SHORT)  
  142.                 .show();  
  143.   
  144.     }  
  145. }  



 

大家注意这个方法,知道这两种写法的区别吗? 当然 return1 ,就可以得到上面截图的效果。

 

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. public int getChildrenCount(int groupPosition)  
  3. {  
  4.     //return treeNodes.get(groupPosition).childs.size();  
  5.     //  返回值必须为1,否则会重复数据  
  6.     return 1;  
  7. }  

 

 

 

    • 加入CSDN技术群:221057495 与我交流
posted @ 2016-07-19 15:22  请叫我码农怪蜀黍  阅读(678)  评论(0编辑  收藏  举报