android中GridView的Adapter的getView()

 近期些項目,發現一個現象。在重寫Adapter中getView的時候,發現兩種方式,導致按下的效果不同。
        常用的方式:
      
[java] 
@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        if (SystemUtils.list.size() <= 0) { 
            return null; 
        } 
        final HistoryItemInfo info = SystemUtils.list.get(position); 
        if (convertView == null) { 
            convertView = LayoutInflater.from(context).inflate( 
                    R.layout.gv_item, null); 
        } 
        ImageView imgView = (ImageView) convertView.findViewById(R.id.imgTitle); 
        try { 
            int resourceId = Integer.parseInt(info.picPath); 
            imgView.setImageResource(resourceId); 
        } catch (Exception e) { 
            imgView.setImageDrawable(BitmapDrawable.createFromPath(info.picPath)); 
        } 
        TextView txtTitle = (TextView)convertView.findViewById(R.id.txtTitle); 
        txtTitle.setText(info.title); 
        return convertView; 
    } 
        這種寫法,點擊時候整個區域全被顏色籠罩。
      
       另外一種:
    
[java] 
@Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            final int index = position; 
            RelativeLayout relativeLayout = null; 
            SpringImageView imageview = null; 
            TextView view = null; 
            if (convertView == null) { 
                relativeLayout = new RelativeLayout(mContext); 
                relativeLayout.setLayoutParams(new GridView.LayoutParams( 
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
                imageview = new SpringImageView(mContext); 
                imageview.setId(111); 
                imageview.setScaleType(ImageView.ScaleType.FIT_XY); 
                view = new TextView(mContext); 
                view.setTextColor(Color.BLACK); 
                view.setGravity(Gravity.CENTER_HORIZONTAL); 
                view.setTextSize(20); 
                RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams( 
                        150, ViewGroup.LayoutParams.WRAP_CONTENT); 
                lp1.addRule(RelativeLayout.BELOW, imageview.getId()); 
                lp1.addRule(RelativeLayout.CENTER_HORIZONTAL); 
                relativeLayout.addView(view, lp1); 
                RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( 
                        ViewGroup.LayoutParams.WRAP_CONTENT, 
                        ViewGroup.LayoutParams.WRAP_CONTENT); 
                lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
                lp2.addRule(RelativeLayout.CENTER_HORIZONTAL); 
                relativeLayout.addView(imageview, lp2); 
            } else { 
 
                relativeLayout = (RelativeLayout) convertView; 
                view = (TextView) relativeLayout.getChildAt(0); 
                imageview = (SpringImageView) relativeLayout.getChildAt(1); 
            } 
            imageview.setImageDrawable(getDrawable(gridList.get(position))); 
            imageview.setOnClickListener(new OnClickListener() { 
                public void onClick(View v) { 
                    if (onItemClickLisenter != null) { 
                        onItemClickLisenter.onItemClick(gridList, index,page); 
                    } 
                } 
            }); 
            view.setText(gridList.get(position).title); 
            return relativeLayout; 
        } 

         這種寫法,點擊的時候,隻在ImageView起作用,相當於,OnItemClick事件在ImageView起瞭作用。
         點擊前後對比:
       
         寫下文章方便記憶,大傢有用的住的地方,謝謝借鑒。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。