2010-09-10 12 views
5

Tengo un diseño que podría ser un poco inusual. La estructura es la siguiente:Disposición con posición dinámica

<?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 de XML también:

<?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: Deseo poder cambiar la posición de mi CustomRelativeLayout. Debería seguir mi toque. Su pequeño que el SurfaceView y debe "deslizante" por encima después de mi contacto ...

hay dos problemas:

  1. El unen a onTouchListener mLinearLayout (en CustomRelativeLayout) se activa sólo una vez y sólo para ACTION_DOWN. Veo la salida de registro solo una vez
  2. La nota nunca cambia la posición, nunca se vuelve a dibujar con la matriz modificada ... Veo la salida declarada en onDraw nunca después del toque.

En primer lugar, porque SurfaceView también maneja eventos táctiles. Pero pensé que si CustomRelativeLayout lo maneja primero, debería funcionar.

¿Alguna idea?

Editar para la solución de

Gracias a Xil3 yo era 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 encontré un error/función/problema, que es absolutamente nuevo para mí: cuando elimino el fondo en el elemento raíz de mi nota (que actualmente es transparente), la nota solo es visible dentro de los 200 pb de ancho/alto que he establecido en el interior LinearLayout. Entonces no es fill_parent, es wrap_content incluso cuando lo configuré en fill_parent. Así layout_width y layout_height utilizar sólo lo dado fill_parent cuando un fondo se establece ... ... weired

Respuesta

2

El problema es que usted está volviendo falsa en el caso onTouch - es así, que está básicamente diciéndole que usted' no estoy interesado en ningún evento posterior.

Cambie eso para que sea verdadero.

+0

right ... * smackmyhead * pero el segundo problema persiste ... nunca cambia la posición porque 'onDraw' nunca se llama. Lo arreglé por el momento llamando al 'onDraw' en el mismo hilo que se está ejecutando SurfaceView. ¿No hay una mejor manera de usar 'invalidate()' de alguna manera? – WarrenFaith

+0

hmm, ¿eso es un hilo de interfaz de usuario? Si no, intente postInvalidate() – xil3

+0

Agregué mi solución en función de su respuesta a mi pregunta. ¡Gracias! – WarrenFaith