如何从右向左滑动文本视图?

时间:2018-12-21 15:30:51

标签: android ontouchlistener

我在coordinatorLayout中有一个textview。我希望能够从左向右和从右向左滑动textview。从左向右滑动可达到预期效果。textview随动作移动值水平移动。

当我尝试从右向左滑动时,textview似乎永远不会结束。我确实看到了textview滑动的事实,但是textview没有尽头。请帮忙。

代码如下:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container"
        android:background="#06ff87"/>

    <TextView
        android:id="@+id/dismiss1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="> Swipe to dismiss > > >"
        android:padding="30dp"
        android:gravity="center"
        android:layout_gravity="center_horizontal|bottom"
        android:background="#3bb0ff"/>

</android.support.design.widget.CoordinatorLayout>




    public class NextActivity extends AppCompatActivity {
    CoordinatorLayout coordinatorLayout;
    TextView dismiss;  
    private int _xDelta;
    private int _yDelta;

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


        dismiss = findViewById(R.id.dismiss1);


        coordinatorLayout = findViewById(R.id.coordinator);

       coordinatorLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        CoordinatorLayout.LayoutParams lParams = (CoordinatorLayout.LayoutParams) dismiss.getLayoutParams();
                        _xDelta = X - lParams.leftMargin;
                        _yDelta = Y - lParams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("next",  String.valueOf(X - _xDelta));

                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) dismiss.getLayoutParams();
                        layoutParams.leftMargin = X - _xDelta;
                        layoutParams.topMargin = Y - _yDelta;

                        dismiss.setLayoutParams(layoutParams);
                        break;
                }


                return true;
            }
        });

    }
}

0 个答案:

没有答案