解决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);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wangxueqing52/article/details/80655987