android中ListView多次刷新重复执行getView的解决方法

1.在android开发中,遇到listview的item中文本输入EditText中如果存在addTextChangedListener(new TextWatcher()的监听,导致多次执行监听代码从而使EditText的焦点多次变化,原本需要的数字输入法也出现快速切换的情况,这显然不是需求所容许的,所以也尝试过很多种网上所说的方法,结果是都没什么作用,最终只能放弃listview,改为LinearLayout动态添加view的方式实现列表,问题顺利解决。


2.出现这一情况的原因是listview的gridview()方法多次执行的原因,后来找到一种方法,自定义listview中加入标识,来去除重复执行的部分,如下:

public class WarmhomeListView extends ListView {

		public boolean isOnMeasure = false;

		public WarmhomeListView(Context context) {
			super(context);
		}

		public WarmhomeListView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			isOnMeasure = true;
			int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
			super.onMeasure(widthMeasureSpec, expandSpec);
		}

		@Override
		protected void onLayout(boolean changed, int l, int t, int r, int b) {
			isOnMeasure = false;
			super.onLayout(changed, l, t, r, b);
		}
	}


在gridview()方法里加入以下代码:

@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.layout_skuslist_catalog, null);
			holder = new ViewHolder(convertView);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if(!(WarmhomeListView)parent.isOnMeasure) {
			return;
		}

		Catalog catalog = list.get(position);
		String selected = catalog.selected;
		if ("1".equals(selected)) {
			holder.tv_name.setBackgroundResource(R.drawable.activity_common_radius_byellow);
			holder.tv_name.setTextColor(mContext.getResources().getColor(R.color.red));
		} else {
			holder.tv_name.setBackgroundResource(R.drawable.activity_common_radius_gray2);
			holder.tv_name.setTextColor(mContext.getResources().getColor(R.color.textColorNormal));
		}

		holder.tv_id.setText(catalog.id);
		holder.tv_name.setText(catalog.name);

		return convertView;
	}


3.如果listview高度计算有误,底部一列无法显示,检查也许是你item里有textview文本类的组件高度是自适应的,那就应当重写组件,让其自己计算高度,如下:
public class MyTextView4TypeFaceAndHeight extends TextView { 

    private Context context;
	
    public MyTextView4TypeFaceAndHeight(Context context) {
        super(context);
        init(context);
    }

    public MyTextView4TypeFaceAndHeight(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyTextView4TypeFaceAndHeight(Context context, AttributeSet attrs, int defSyle) {
        super(context, attrs, defSyle);
        init(context);
    }

    public void init(Context context) { 
    	if (isInEditMode()) {
    		return;
    	}
        this.context = context;
    }
    
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		Layout layout = getLayout();
		if (layout != null) {
			int height = (int) FloatMath.ceil(getMaxLineHeight(this.getText().toString())) + getCompoundPaddingTop()
					+ getCompoundPaddingBottom();
			int width = getMeasuredWidth();
			setMeasuredDimension(width, height);
		}
	}

	private float getMaxLineHeight(String str) {
		float height = 0.0f;
		float screenW = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
		float paddingLeft;
		float paddingReft;
		ViewParent l = this.getParent();
		if (l instanceof RelativeLayout) {
			paddingLeft = ((RelativeLayout) this.getParent()).getPaddingLeft();
			paddingReft = ((RelativeLayout) this.getParent()).getPaddingRight();
		} else {
			paddingLeft = ((LinearLayout) this.getParent()).getPaddingLeft();
			paddingReft = ((LinearLayout) this.getParent()).getPaddingRight();
		}
		
		// 这里具体this.getPaint()要注意使用,要看你的TextView在什么位置,这个是拿TextView父控件的Padding的,为了更准确的算出换行
		int line = (int) Math.ceil((this.getPaint().measureText(str) / (screenW - paddingLeft - paddingReft)));
		height = (this.getPaint().getFontMetrics().descent - this.getPaint().getFontMetrics().ascent) * line;
		return height;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BojunBlue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值