简体   繁体   English

自定义视图中未调用onDraw()

[英]onDraw() not being called in custom View

I've already searched on Stackoverflow for a suitable answer but couldn't find anything that would help me. 我已经在Stackoverflow上搜索了一个合适的答案,但是找不到任何可以帮助我的东西。

I've got a Fragment which contains a ListView. 我有一个包含ListView的Fragment。 This ListView should display an array of custom views. 此ListView应该显示自定义视图的数组。 The problem is, that the custom views' onDraw() method won't be called. 问题在于,自定义视图的onDraw()方法将不会被调用。 They get initialized but not drawn. 它们已初始化但未绘制。 Even forcing them to draw via invalidate() doesn't change anything. 即使强迫他们通过invalidate()进行绘制也不会改变任何内容。 I've already tried to set 我已经尝试设定

this.setWillNotDraw(true);

in the constructor, which didn't help. 在构造函数中,这没有帮助。 Any help would be appreciated. 任何帮助,将不胜感激。

Fragment 分段

public static class CustomFragment extends Fragment {

        private ListView list;

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.fragment, container, false);
            list = (ListView)rootView.findViewById(R.id.fragment_list);
            list.setAdapter(new CustomAdapter(getActivity(), R.layout.list_item, somevalues));
            return rootView;
        }
    }

Custom View 自订检视

    public class CustomView extends View {

    public static final String TAG = "CustomView";

    public CustomView(Context context) {
        super(context);
        init();
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d(TAG, "Measuring and storing dimensions");
    }

    public void init() {
        Log.d(TAG, "Initializing CustomView");
        this.setWillNotDraw(false);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "OnDraw called");
    }
}

Custom Adapter 定制适配器

public static class CustomAdapter extends ArrayAdapter<String> {
        private String[] values;
        private Context context;
        private int resource;

        public CustomAdapter(Context context, int resource, String[] values) {
            super(context, resource, values);
            this.values = values;
            this.context = context;
            this.resource = resource;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(resource, parent, false);
            CustomView cView = (CustomView)rowView.findViewById(R.id.cview);
            cView.setColors(values);
            return rowView;
        }
    }

您应该在init方法中设置setWillNotDraw(false)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM