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);
}
&nb
补充:移动开发 , Android ,