解决ListView自定义Adapter中getView重复调用的问题

最近做项目在getView中写了异步方法,因为getView重复调用的问题致使我的代码逻辑出现了问题,因每一次调用getView方法其实就是在测量子View,通过这个原理我们可以自定义ListView重写onMeasure和onLayout方法,当执行onMeasure不执行我们的代码逻辑,当执行onLayout时执行我们的代码逻辑即可解决问题,代码如下:

自定义ListView

/**
 * Created by XueQing Wang on 2018/5/30.
 */
public class CustomListView extends ListView {

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

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

    public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    boolean isOnMeasure;
    //重新计算高度的具体方法,
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        isOnMeasure = true;
        Log.d("onMeasure", "onMeasure");
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
    //摆放子View
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.d("onLayout", "onLayout");
        isOnMeasure = false;
        super.onLayout(changed, l, t, r, b);
    }

    public boolean isOnMeasure(){
        return isOnMeasure;
    }

}

自定义Adapter的getView方法

/**
 * Created by XueQing Wang on 2018/6/4.
 */
public class MyAdapter extends BaseAdapter {

    private List<SoilElements> list;
    private Context context;
    private String[] stringArray;
    private String showImgUrl;


    public MyAdapter(List<SoilElements> list, Context context) {
        this.list = list;
        this.context = context;
        this.stringArray = context.getResources().getStringArray(R.array.tudi_type_name);
        showImgUrl = context.getResources().getString(R.string.show_photo_url);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.swipe_layout, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //解决多次调用getView方法  利用当OnLayout结束
        if (parent instanceof CustomListView) {
            if (((CustomListView) parent).isOnMeasure()) {
                return convertView;
            }
        }
        //这里写你的代码逻辑

        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.tv_info)
        TextView tvInfo;
        @BindView(R.id.tv_edit)
        TextView tvEdit;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值