1. 程式人生 > >解決android expandablelistview 裡面嵌入gridview行資料重複問題

解決android expandablelistview 裡面嵌入gridview行資料重複問題

最近做了一個“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 與我交流