Frage

Ich habe ein Layout, das etwas ungewöhnlich sein könnte. Die Struktur ist die folgende:

<?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>

Meine customRelativeLayout am unteren Rande des XML hat ein XML-Layout auch:

<?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>

Meine 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);
    }
}

Was ich tun möchte: Ich möchte die Position meiner CustomRelativeLayout zu der Lage sein, zu ändern. Es sollte meine Berührung sein folgen. Sein kleiner als die Surface und soll „slide“ oben nach meiner Berührung ...

Es gibt zwei Probleme:

  1. Die onTouchListener binden an mLinearLayout (in CustomRelativeLayout) wird nur dann ausgelöst, wenn und nur für ACTION_DOWN. Ich sehe die Log-Ausgabe nur einmal
  2. Die Notiz ändert sich nie die Position, die er nie mit der veränderten Matrix neu gezeichnet wird ... ich die Ausgabe in onDraw angegeben sehen nie nach der Berührung passiert ist.

Zuerst könnte sein, weil die Surface auch Touch-Ereignisse behandelt. Aber ich dachte, wenn die CustomRelativeLayout Griffe es zuerst, sollte es funktionieren.

Einige Ideen?

Bearbeiten für Lösung

Dank Xil3 konnte ich die Wand entfernen, ich laufe in ... hier ist meine Lösung:

meine Anmerkung 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>

Und hier ist mein Konstruktor:

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;
        }
    });
}

Ich fand auch einen Bug / Feature / Ausgabe, die für mich absolut neu ist: Wenn ich den Hintergrund in meiner Anmerkung Wurzelelement entfernen (die zur Zeit transparent ist) ist die Note nur sichtbar innerhalb der 200dp Breite / Höhe I gesetzt haben in dem inneren Linearlayout. So ist es nicht fill_parent, seine wrap_content wie ich setzen Sie ihn auf fill_parent. So layout_width und layout_height nur den gegebenen fill_parent verwenden, wenn ein Hintergrund gesetzt ... weired ...

War es hilfreich?

Lösung

Das Problem ist, dass Sie falsch in der OnTouch Ereignis wieder zuzuführen -. Sie sind also im Grunde ist es zu sagen, dass Sie in allen nachfolgenden Ereignisse nicht interessiert sind

Ändern Sie diesen true zurück.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top