简体   繁体   中英

Android OnTouchListener

I want to implement OnTouchListener in my app. What I want:

  • when I touch the screen , I need to show or hide some action: here when I touch ImageView , show the LinearLayout which handle actions

My Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_af"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">

<ImageView
    android:id="@+id/ivaf"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_centerInParent="true" />

<LinearLayout
    android:id="@+id/llaffbtn"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000"
        android:src="@drawable/ic_file_download_white_24dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000"
        android:src="@drawable/ic_share_white_24dp" />

</LinearLayout>

and what i tried in my java class:

    img = (ImageView) findViewById(R.id.ivaf);
    rl = (RelativeLayout) findViewById(R.id.rl_af);
    affBtn = (LinearLayout) findViewById(R.id.llaffbtn);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                affBtn.setVisibility(View.VISIBLE);
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                affBtn.setVisibility(View.INVISIBLE);
            }

            return false;
        }
    });

instead of View.INVISIBLE ==> View.Gone

Update

Try it without MotionEvent.ACTION_DOWN) {... like :

    img = (ImageView) findViewById(R.id.ivaf);
    rl= (RelativeLayout) findViewById(R.id.rl_af);
    affBtn = (LinearLayout) findViewById(R.id.llaffbtn);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            affBtn.setVisibility(affBtn.getVisibility()== View.VISIBLE ? View.GONE : View.VISIBLE);
            return false;
        }
    });

this code working on my devise. hope it helps

Using invisible will still have the object take up space in layout. Using Gone instead will hide the object and remove the space.

affBtn.setVisibility(View.GONE);

Are you sure you don't really want an OnClickListener ?

rl.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        affBtn.setVisibility(affBtn.getVisibility == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
        }
    }
});

This will make your view toggle between visible and invisible on every click.

If you want the view to disappear along with the space that it takes up, change View.INVISIBLE in the code snippet above to View.GONE

Try returning "True" after ACTION_DOWN. Android will then listen to ACTION_UP events and call it when it happens. I had a similar issue in an earlier project of mine.

rl.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            affBtn.setVisibility(View.VISIBLE);
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            affBtn.setVisibility(View.INVISIBLE);
        }

        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