0

I have a linear-layout "draggableBallsContainer" that includes 10 vertically-aligned balls which can be dragged and dropped. After dragging and dropping some of the balls into another layout, I want to re-initialize "draggableBallsContainer" by clicking on a button.

After clicking on the button, the balls are not correctly aligned, and some of them are even not visible inside "draggableBallsContainer" anymore.

here is the xml:

    <LinearLayout
        android:id="@+id/draggableBallsContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:layout_weight="2"
        android:background="@drawable/background_right_layout"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/ball_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:contentDescription=""
            android:src="@mipmap/ball" />

        .
        .
        /* balls removed for readability purposes */
        .
        .

        <ImageView
            android:id="@+id/ball_10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:contentDescription=""
            android:src="@mipmap/ball" />
    </LinearLayout>

OnClick will run this code:

    draggableBallsContainer.removeAllViews();
    //int i = 0;
    LinearLayout.LayoutParams ballLayoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
    ballLayoutParams.gravity = Gravity.CENTER;
    for (ImageView ball : balls) {
        draggableBallsContainer.addView(ball, ballLayoutParams);
    }

By running this code, my expectation is to get the exact same initial state ( 10 Balls vertically aligned). However, only some of the balls will be displayed, and "randomly" aligned.

I tried to call invalidate as answers on some similar topics suggested, but in vain.

I would appreciate your help and thanks in advance.

hba
  • 13
  • 3
  • Did you try calling `requestLayout`? – lionscribe Feb 01 '19 at 06:33
  • @lionscribe yes I did, it didn't help. – hba Feb 01 '19 at 14:44
  • I imagine during dragging and dropping, you changed the margins, padding, or the x y values of the ball. Some of these values stayed put when you reattach them, thus you are seeing them in different places. – lionscribe Feb 03 '19 at 03:26
  • @lionscribe that was a good hint ! It was the reason why the balls were out of layout. Thanks :) – hba Feb 05 '19 at 21:41

1 Answers1

0

My suggestion for you is to change to RecyclerView for this kind of work. It will help you easier to extend your list in the future. https://developer.android.com/guide/topics/ui/layout/recyclerview

Bach Vu
  • 2,298
  • 1
  • 15
  • 19