我有一个使用IOnTouchListener对其进行滑动操作的列表,因为拖动发生我正在更改LinearLayout的左侧以显示其后面的第二个LinearLayout。该布局有两个按钮,一个是隐藏的。如果用户执行“快速”滑动,则将顶部视图的左侧移动到特定值并触发一些代码。此代码将隐藏按钮更改为可见。当该按钮的ViewState发生变化时,它会使顶视图的Left快照回到0,而不是停留在我放置它的位置。不知道为什么会这样做或如何解决它。
XML看起来像......(但会根据我发布的另一个问题的答案略有变化)
<?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>
我不使用Java,我使用Xamarin和C#。以下是相关的代码片段......
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;
}
如果在ToggleUndoButton方法中我注释掉了
行this.ApproveUndoButton.Visibility = ViewStates.Visible;
一切正常。 TopContentView的左边更改为等于我的DragThreshold,文本更改为Approving ...计时器启动,它保持这种方式2秒,然后我的计时器中的代码点击并且TopContentView被移回左边0.如果我在可见性更改中离开,则TopContentView会立即移回0而不是等到计时器完成。