简体   繁体   中英

Recycle view in scroll view for android

i have this code here that i have wrote that should allow you to move objects around the screen.

i seem to be having trouble with this as the object will move but not smothly, it jumps around when you hold your finger down.

please help.

VVVVVVVVVVVVV CODE VVVVVVVVVVVVV

package com.example.alex.alexstouch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

    TextView touchMove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        touchMove = (TextView)findViewById(R.id.pike);
        touchMove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                    case MotionEvent.ACTION_MOVE:
                        touchMove.setTranslationX(event.getX());
                        touchMove.setTranslationY(event.getY());
                        break;

                    case MotionEvent.ACTION_DOWN:
                        Log.d("pike pike", "broken");
                        break;



                }

                return true;
            }
        });
    }

}

If you want move some object on the screen, for example, drag-n-drop button, you should to make your root layout - Relative layout and change object margins in the code (when touch callbacks called).
You can change margins used layoutParams like this

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = layoutParams.rightMargin + x
layoutParams.bottomMargin = layoutParams.bottomMargin + y;
view.setLayoutParams(layoutParams);

My code to floating button (that can be moved by long tap)

private class PlusButtonTouchListener implements View.OnTouchListener {
    private final int MIN_X = 0;
    private final int MIN_Y = 0;
    private float startX;
    private float startY;
    private int MAX_X;
    private int MAX_Y;

    @Override
    public boolean onTouch(View view, MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();

                MAX_X = ((View) mPlusButton.getParent()).getWidth() - mPlusButton.getWidth();
                MAX_Y = ((View) mPlusButton.getParent()).getHeight() - mPlusButton.getHeight();

                mPlusButton.setAlpha(0.7f);
                break;
            case MotionEvent.ACTION_UP:
                mPlusButton.setAlpha(1f);
                savePlusButtonCoordinates();
                isMoved = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isMoved) {
                    float deltaX = startX - event.getX();
                    float deltaY = startY - event.getY();
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    layoutParams.rightMargin = layoutParams.rightMargin + (int) deltaX;
                    layoutParams.bottomMargin = layoutParams.bottomMargin + (int) deltaY;

                    if (layoutParams.rightMargin > MAX_X) {
                        layoutParams.rightMargin = MAX_X;
                    } else if (layoutParams.rightMargin < MIN_X) {
                        layoutParams.rightMargin = MIN_X;
                    }
                    if (layoutParams.bottomMargin > MAX_Y) {
                        layoutParams.bottomMargin = MAX_Y;
                    } else if (layoutParams.bottomMargin < MIN_Y) {
                        layoutParams.bottomMargin = MIN_Y;
                    }

                    view.setLayoutParams(layoutParams);
                }
                break;
        }
        mPlusButton.invalidate();
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM