0

I have a list with Swipe Actions on it using IOnTouchListener, as dragging occurs I am changing the Left of a LinearLayout to reveal a second LinearLayout behind it. That layout has two buttons, one is hidden. If the user does a "quick" swipe then I move the Left of the top view to a specific value and fire some code. This code changes the invisible button to be visible. When the ViewState of that button changes it makes the top view's Left snap back to 0 instead of staying where I place it. Any idea why it would do this or how to work around it.

The XML looks like... (But will change slightly based on the answer to another question I posted)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_vertical"
    android:background="@color/black"
    android:id="@+id/Swipe"
  >
  <LinearLayout
    android:id="@+id/LeftContentView"
    android:layout_width="175dp"
    android:layout_height="match_parent"
    android:background="@color/yellow"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal"
    >
    <Button 
      android:id="@+id/ApproveButton"
      android:layout_width="0dp"
      android:layout_weight=".72"
      android:layout_height="match_parent"
      android:background="#2796C3"
      android:text="Approve"
      android:layout_alignParentLeft="true"
      />
      <Button 
      android:id="@+id/ApproveUndoButton"
      android:layout_width="0dp"
      android:layout_weight=".28"
      android:layout_height="match_parent"
      android:background="#215681"
      android:text="Undo"
      android:layout_toRightOf="@id/ApproveButton"
      />
  </LinearLayout>

  <LinearLayout
  android:layout_alignParentRight="true"
  android:id="@+id/RightContentView"
  android:layout_width="175dp"
  android:layout_height="match_parent"
  android:background="@color/black"
  android:orientation="horizontal"
    >
    <Button
      android:id="@+id/DenyButton"
      android:layout_width="0dp"
      android:layout_weight=".72"
      android:layout_height="match_parent"
      android:background="#FF0000"
      android:text="Deny"
      />
    <Button
      android:id="@+id/DenyUndoButton"
      android:layout_width="0dp"
      android:layout_weight=".28"
      android:layout_height="match_parent"
      android:background="#860000"
      android:text="Undo"
      />
  </LinearLayout>

  <LinearLayout
    android:id="@+id/TopContentView"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1F1F1F">
    <LinearLayout
      android:layout_height="fill_parent"
      android:layout_width="match_parent" >
      <ImageView 
        android:id="@+id/UnreadImage"
        android:layout_height="match_parent" 
        android:layout_width="7dp" 
        android:src="@drawable/vertical_blue_bar"
        android:background="#2796C3"/>  
      <LinearLayout
        android:id="@+id/ListText"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:padding="12dp">
        <TextView
             android:id="@+id/Text_Line1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textSize="13dip"
             />
        <TextView
               android:id="@+id/Text_Line2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textColor="@color/white"
               android:textSize="13dip"
             />
        <TextView
             android:id="@+id/Text_Line3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textSize="11dip"
             />
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
</RelativeLayout>

I am not using Java, I am using Xamarin with C#. Here are the relevant pieces of code...

    public bool OnTouch(View v, MotionEvent e)
    {

        switch (e.Action)
        {
             case MotionEventActions.Down:
                  OriginalTouchX = Math.Ceiling(e.RawX);
                  index = e.ActionIndex;
                  pointerId = e.GetPointerId(index);
                  VelocityTracker.AddMovement(e);
                  break;

             case case MotionEventActions.Move:
                  VelocityTracker.AddMovement(e);
                 ChangeX(newX);
                  VelocityTracker.ComputeCurrentVelocity(1);

                  float velocity = Math.Abs(VelocityTracker.GetXVelocity(pointerId));

                  if (velocity > SlowDrag && Math.Abs(distanceMoved) > (DragThreshold / 3))
                  {
                       QuickApprove();
                  }
                  break;
        }
    }



void QuickApprove()
    {
            ToggleUndoButton(true, true);
            WaitingForUndo = true;
            ChangeX(DragThreshold);
            this.ApproveTimer.Start(true);
    }

    private void ToggleUndoButton(bool ShowUndo, bool LeftSide)
    {
                this.ApproveUndoButton.Visibility = ViewStates.Visible;
                this.ApproveButton.Text = "Approving...";
    }

    private void ChangeX(int newX)
    {
        int width = this.TopContentView.Right - this.TopContentView.Left;
        this.TopContentView.Left = newX;
        this.TopContentView.Right = this.TopContentView.Left + width;
    }

If in the ToggleUndoButton method I comment out the line

this.ApproveUndoButton.Visibility = ViewStates.Visible;

Everything works fine. The Left of TopContentView changes to be equal to my DragThreshold, the text changes to Approving... the timer starts, It stays this way for 2 seconds, then the code in my timer tick fires and the TopContentView is moved back to a Left of 0. If I leave in the visibility change then the TopContentView is immediately moved back to 0 instead of waiting until the timer is done.

Link
  • 143
  • 9
  • similar to your [other question](http://stackoverflow.com/q/39933768/2745495), it would be difficult for people to help you without posting any java and XML codes – Gino Mempin Oct 08 '16 at 16:15
  • After more testing I have found that setting the visibility to Gone is the culprit. So, I tried changing it to use Invisible. Which of course makes it where when it is gone the other button doesn't stretch. So I used LayoutParams to change the weight of the other button to 1.0f, and it stretches it. But then in the ToggleUndoButton code when I try to set the visibility to Visible, and then try to change the LayoutParams to set the other button back to 0.72f, setting the LayoutParams has the same affect as setting the visibility to Gone. It causes the TopContentView to jump back to 0. – Link Oct 08 '16 at 21:01

0 Answers0