Pregunta

Tengo un diseño que podría ser un poco inusual. La estructura es la siguiente:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <org.CustomSurfaceView
        android:id="@+id/page_flip"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dip" />

    <org.customRelativeLayout
        android:id="@+id/note"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone" />
</FrameLayout>

Mi customRelativeLayout en la parte inferior del XML tiene un diseño demasiado XML:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/my_background"
    android:id="@+id/note_layout">
    <TextView android:id="@+id/note"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:singleLine="false"
        android:layout_margin="5dp"
        android:textColor="#000000" />
</LinearLayout>

Mi CustomRelativeLayout:

public class CustomRelativeLayout extends RelativeLayout implements OverlayView {

    private TextView mNoteText;
    private LinearLayout mLinearLayout;
    public int mX = 0;
    public int mY = 0;
    public boolean mShow;

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.note, this);
        mNoteText = (TextView) findViewById(R.id.note);
        mLinearLayout = (LinearLayout) findViewById(R.id.note_layout);

        mLinearLayout.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e("touched");
                mX = (int) event.getX() - 100;
                mY = (int) event.getY() - 100;
                invalidate();
                return false;
            }
        });
    }

    @Override
    public void hide() {
        mShow = false;
        setVisibility(View.GONE);
    }

    @Override
    public boolean isVisible() {
        return isVisible();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.e("Drawing the postit");
        Matrix matrix = new Matrix();
        matrix.postTranslate(mX, mY);
        canvas.setMatrix(matrix);
        super.onDraw(canvas);
    }

    @Override
    public void setContent(Object content) {
        if (content != null && content instanceof Content) {
            Content noteContent = (Content) content;
            if (noteContent.getText() != null) {
                mNoteText.setText(noteContent.getText());
            } else {
                mNoteText.setText("");
            }
        }
        mNoteText.invalidate();
        mLinearLayout.invalidate();
    }

    @Override
    public void show() {
        mShow = true;
        setVisibility(View.VISIBLE);
    }
}

Lo que quiero hacer: Quiero ser capaz de cambiar la posición de mi CustomRelativeLayout. Debe ser siguen mi toque. Su pequeño que el SurfaceView y debe "deslizante" por encima después de mi contacto ...

Hay dos problemas:

  1. El onTouchListener unen a mLinearLayout (en CustomRelativeLayout) solamente se activa una vez y sólo para ACTION_DOWN. Veo el resultado del registro de una sola vez
  2. La nota nunca cambia la posición, nunca se vuelve a dibujar con la matriz de cambio de ... Veo la salida se indica en onDraw Nunca sucedió después del contacto.

En primer lugar podría ser debido a que el SurfaceView también gestiona los eventos táctiles. Pero pensé que si las asas CustomRelativeLayout en primer lugar, que debería funcionar.

Algunas ideas?

Editar para solución

Gracias a Xil3 que fue capaz de eliminar la pared me encuentro con ... aquí está mi solución:

mi nota XML:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">
    <LinearLayout android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/postit"
        android:id="@+id/textnote_layout">
        <TextView android:id="@+id/textnote_content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:singleLine="false"
            android:layout_margin="5dp"
            android:textColor="#000000" />
    </LinearLayout>
</LinearLayout>

Y aquí está mi constructor:

public TextNoteOverlay(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    li.inflate(R.layout.textnote, this);
    mNoteText = (TextView) findViewById(R.id.textnote_content);
    mLinearLayout = (LinearLayout) findViewById(R.id.textnote_layout);

    setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mLinearLayout.getHitRect(mNoteRect);
            if (mNoteRect.contains((int) event.getX(), (int) event.getY())) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mStartX = (int) event.getX() - mLinearLayout.getLeft();
                    mStartY = (int) event.getY() - mLinearLayout.getTop();
                    return true;
                }
                mX = (int) event.getX() - mStartX;
                mY = (int) event.getY() - mStartY;
                mLinearLayout.layout(mX, mY, mX + mLinearLayout.getWidth(), mY + mLinearLayout.getHeight());
                return true;
            }
            return false;
        }
    });
}

También se encontró un bug / función / cuestión, que es absolutamente nuevo para mí: Cuando quito el fondo en mi elemento raíz nota (que actualmente es transparente) la nota sólo es visible dentro de la 200dp anchura / altura Tengo conjunto en el LinearLayout interior. Así que no es fill_parent, su wrap_content así como yo lo puso a fill_parent. Así layout_width y layout_height solamente usan la fill_parent dado cuando un fondo se establece ... weired ...

¿Fue útil?

Solución

El problema es que usted está volviendo falsa en el caso onTouch -. Así que, básicamente estás diciéndole que usted no está interesado en cualesquiera acontecimientos posteriores

Un cambio que devuelva true.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top