vendredi 9 février 2018

Android Dinamicaly add view in position xy in a custom view

I'm having some troubless on adding views at a determined position inside my custom view. All the views, buttons in this case, are added at position (0,0) and not at random positions as expected. Searching on internet I found out that the problem may came from onMeasure() or onLayout() but I dont know how to fix it.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private int count=0;
    private Random rn = new Random();
    private ScrollingLayout2 zoomableView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        zoomableView=(ScrollingLayout2) findViewById(R.id.scrollingLayout);

        Button buttonBottom = new Button(MainActivity.this);
        buttonBottom.setText("Button "+count);
        buttonBottom.setTag("First button");
        buttonBottom.setOnClickListener(this);
        addButtonLayout(buttonBottom, 0,0, 0, 0);
    }

    private void addButtonLayout(Button button, int marginLeft, int marginTop, int marginRight, int marginBottom) {

        // Defining the layout parameters of the Button
        RelativeLayout.LayoutParams buttonLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        // Add Margin to the LayoutParameters
        buttonLayoutParameters.setMargins(marginLeft, marginTop, marginRight, marginBottom);

        // Setting the parameters on the Button
        button.setLayoutParams(buttonLayoutParameters);

        zoomableView.addElement(button);
    }

    @Override
    public void onClick(View view) {
        count++;

        Button buttonBottom = new Button(MainActivity.this);
        buttonBottom.setText("Button "+count);
        buttonBottom.setTag("Button "+count);
        buttonBottom.setOnClickListener(this);

        addButtonLayout(buttonBottom, rn.nextInt(1000 - 0 + 1) +0, rn.nextInt(1000 - 0 + 1) +0, 0, 0);
    }
}

and the custom view that extends a relativeLayout

public class ScrollingLayout2 extends RelativeLayout {

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

    public ScrollingLayout2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

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

    private void init(Context context){
        setBackgroundResource(R.drawable.back);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                child.layout(child.getLeft(), child.getTop(), child.getLeft() + child.getMeasuredWidth(), child.getTop() + child.getMeasuredHeight());
                //child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
            }
        }
    }

    public void addElement(View view){

        addView(view);

    }

}




Aucun commentaire:

Enregistrer un commentaire