最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字。
很简单一个功能,先看效果图:
结果发现一个问题,就是每个分类里的数据存在重复插入好几次的现象;
ExpertGridAdapter.java gridview的adapter
- public class ExpertGridAdapter extends BaseAdapter {
- private LayoutInflater inflater;
- private CsBlogger[] datals;
- private Context mContext;
- private String TAG="ExpertGridAdapter";
- public ExpertGridAdapter(Context context,List<CsBlogger> childs) {
- this.inflater = LayoutInflater.from(context);
- datals=new CsBlogger[childs.size()];
- childs.toArray(datals);
- CsLog.d("maomao", "childs.size:"+childs.size());
- mContext=context;
- }
- @Override
- public int getCount() {
- return datals.length;
- }
- @Override
- public Object getItem(int position) {
- return datals[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView==null) {
- holder=new ViewHolder();
- convertView=this.inflater.inflate(R.layout.csblog_expert_item_menu, null);
- holder.iv=(ImageView) convertView.findViewById(R.id.item_image);
- holder.tv=(TextView) convertView.findViewById(R.id.item_text);
- convertView.setTag(holder);
- }
- else {
- holder=(ViewHolder) convertView.getTag();
- }
- CsBlogger expert=datals[position];
- DisplayImageOptions options; // 显示图像设置
- ImageLoader imageLoader = ImageLoader.getInstance();// 得到图片加载器
- // 图片加载器初始化
- imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
- // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
- options = new DisplayImageOptions.Builder()
- .showStubImage(R.drawable.blogger) // 设置图片下载期间显示的图片
- .showImageForEmptyUri(R.drawable.blogger) // 设置图片Uri为空或是错误的时候显示的图片
- .showImageOnFail(R.drawable.blogger) // 设置图片加载或解码过程中发生错误显示的图片
- .cacheInMemory() // 设置下载的图片是否缓存在内存中
- .cacheOnDisc() // 设置下载的图片是否缓存在SD卡中
- .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
- .build(); // 创建配置过得DisplayImageOption对象
- // 异步加载图片 专家头像
- imageLoader
- .displayImage(expert.getUserface(), holder.iv, options);
- holder.tv.setText(expert.getNickname());
- return convertView;
- }
- private class ViewHolder{
- ImageView iv;
- TextView tv;
- }
- }
ExpertsListViewAdapter.java 可扩展的listview
- public class ExpertsListViewAdapter extends BaseExpandableListAdapter implements
- OnItemClickListener
- {
- public static final int ItemHeight = 136;// 每项的高度
- public static final int PaddingLeft = 96;// 每项的高度
- private int myPaddingLeft = 0;
- private List<TreeNode> treeNodes = new ArrayList<TreeNode>();
- private Context parentContext;
- private LayoutInflater layoutInflater;
- public ExpertsListViewAdapter(Context view, int myPaddingLeft,List<TreeNode> treeNodes)
- {
- parentContext = view;
- this.myPaddingLeft = myPaddingLeft;
- this.treeNodes=treeNodes;
- }
- public void RemoveAll()
- {
- treeNodes.clear();
- }
- public CsBlogger getChild(int groupPosition, int childPosition)
- {
- return treeNodes.get(groupPosition).childs.get(childPosition);
- }
- @Override
- public int getChildrenCount(int groupPosition)
- {
- //return treeNodes.get(groupPosition).childs.size();
- // 返回值必须为1,否则会重复数据
- return 1;
- }
- public TextView getTextView(Context context)
- {
- AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, ItemHeight);
- TextView textView = new TextView(context);
- textView.setLayoutParams(lp);
- textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- return textView;
- }
- class ExpandableListHolder {
- CsGridView grdiview;
- }
- private ExpertGridAdapter adatper;
- /**
- * 可自定义ExpandableListView
- */
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent)
- {
- CsLog.d("maomao", "getChildView, groupPosition:"+groupPosition+",childs size:"+treeNodes.get(groupPosition).childs.size());
- //if (convertView == null)
- {
- CsLog.d("maomao", "converView is null");
- layoutInflater = (LayoutInflater) parentContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = layoutInflater.inflate(R.layout.csblog_expert_view, null);
- CsGridView toolbarGrid = (CsGridView) convertView
- .findViewById(R.id.GridView_toolbar);
- toolbarGrid.setNumColumns(4);// 设置每行列数
- toolbarGrid.setGravity(Gravity.CENTER);// 位置居中
- toolbarGrid.setHorizontalSpacing(10);// 水平间隔
- adatper=new ExpertGridAdapter(parentContext,treeNodes.get(groupPosition).childs);
- toolbarGrid.setAdapter(adatper);// 设置菜单Adapter
- toolbarGrid.setOnItemClickListener(this);
- adatper.notifyDataSetChanged();
- }
- //else{
- // CsLog.d("maomao", "convertView!=null");
- //}
- return convertView;
- }
- /**
- * 可自定义list
- */
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent)
- {
- TextView textView = getTextView(this.parentContext);
- String gname=getGroup(groupPosition).toString()+"("+treeNodes.get(groupPosition).childs.size()+")";
- textView.setText(gname);
- textView.setPadding(myPaddingLeft + PaddingLeft, 0, 0, 0);
- return textView;
- }
- public long getChildId(int groupPosition, int childPosition)
- {
- return childPosition;
- }
- public Object getGroup(int groupPosition)
- {
- return treeNodes.get(groupPosition).parent;
- }
- public int getGroupCount()
- {
- return treeNodes.size();
- }
- public long getGroupId(int groupPosition)
- {
- return groupPosition;
- }
- public boolean isChildSelectable(int groupPosition, int childPosition)
- {
- return true;
- }
- public boolean hasStableIds() {
- return true;
- }
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id)
- {
- Toast.makeText(parentContext, "当前选中的是:" + position, Toast.LENGTH_SHORT)
- .show();
- }
- }
大家注意这个方法,知道这两种写法的区别吗? 当然 return1 ,就可以得到上面截图的效果。
- @Override
- public int getChildrenCount(int groupPosition)
- {
- //return treeNodes.get(groupPosition).childs.size();
- // 返回值必须为1,否则会重复数据
- return 1;
- }
- 加入CSDN技术群:221057495 与我交流