Issue
I have created a custom view to draw a line on the screen. This view is included in a fragment's xml layout and retrieved like the following in the fragment's onCreateView
method:
MyCustomView mMyCustomView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate view
View v = inflater.inflate(R.layout.fragment, container, false);
mMyCustomView = (MyCustomView) v.findViewById(R.id.my_custom_view);
...
}
When I pass the mMyCustomView
variable to a custom listener in the fragment's onCreateView
method and call something like mMyCustomView.drawLine
in the listener class everything works fine.
When I call mMyCustomView.drawLine
in the fragment's onResume()
method, however, nothing happens, though it is the same variable and method.
The only reason I could think of is that the listener calls the method when the user interacts with the fragment, which is even later than the onResume()
is called, as far as the lifecycle is concerned. However, within the fragment I cannot call the method any later than in onResume()
AFAIK.
EDIT 1:
This is what my custom view looks like:
public class ConnectionLinesView extends View {
// Class variables
Paint mPaint = new Paint(); // Paint to apply to lines
ArrayList<float[]> mLines = new ArrayList<float[]>(); // Array to store the lines
public ConnectionLinesView(Context context) {
super(context);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(2);
}
public ConnectionLinesView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(2);
}
public ConnectionLinesView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(2);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// If lines were already added, draw them
if(!mLines.isEmpty()){
for(float[] line : mLines){
canvas.drawLine(line[0], line[1], line[2], line[3], mPaint);
}
}
}
// Method to add a line to the view
public void addLine(View v1, View v2) {
float[] line = new float[4];
line[0] = v1.getX() + v1.getWidth()/2;
line[1] = v1.getY() + v1.getHeight()/2;
line[2] = v2.getX() + v2.getWidth()/2;
line[3] = v2.getY() + v2.getHeight()/2;
mLines.add(line);
this.invalidate();
}
public void removeLines() {
mLines.clear();
this.invalidate();
}
}
When I call addLine(...) in onResume()
, the line is not drawn, even though the inside of the for loop in the onDraw()
method is reached. When I add another line later in the listener class (which responds to some user interaction), the both lines are drawn on the canvas. Somehow the canvas.drawLine()
doesn't work in the onResume()
of the view's parent fragment.
EDIT 2:
I have added a handler, that calls the custom view's invalidate
method repeatedly after the fragment has been added to the layout of the parent activity. The line still doesn't get drawn!
Solution
In the end I solved the problem by creating a handler, which calls the addLine
method after 50 milliseconds after the fragment has been added. A pretty dirty solution, but it works...
Answered By - Schnodahipfe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.