How to Hide and Show Buttons on Alternate Touch Events

☆樱花仙子☆ 提交于 2021-02-07 10:59:27

问题


My activity displays images and i want to display two buttons when the user touch the screen, and to disappear these button on next touch.

My activity file ImageViewPager.java is as follows:

package com.pankajvatsa.testfeet;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageViewPager extends Activity {
// Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.activity_image_view_pager);

    mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);
    mainLay.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                bWallpaperButton.setVisibility(View.VISIBLE); 
                bDownloadButton.setVisibility(View.VISIBLE);
                return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                bWallpaperButton.setVisibility(View.INVISIBLE); 
                bDownloadButton.setVisibility(View.INVISIBLE);
                return true;
            }
            return true;
        }
    });

    bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
    bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);

    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapter imageAdapter = new ImageAdapter(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        images.add(imageView);
    }

    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
    ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
    viewpager.setAdapter(pageradapter);
    // Show images following the position
    viewpager.setCurrentItem(position);
}

}

And my xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >

<android.support.v4.view.ViewPager
    android:id="@+id/image_pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" >
</android.support.v4.view.ViewPager>

<Button
    android:id="@+id/bSetWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/set_wallpaper"
    android:visibility="gone" />

<Button
    android:id="@+id/bSaveToGallery"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/save_local"
    android:visibility="gone" />
</RelativeLayout>

回答1:


Here is an example using LinearLayout but should work fine with RelativeLayout as well:

  Button btn1;
    boolean gone = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.button1);

        LinearLayout ln = (LinearLayout)findViewById(R.id.LinearLayout01);

        ln.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    if(!gone){
                        btn1.setVisibility(View.GONE);
                        gone = true;
                    }else{
                        btn1.setVisibility(View.VISIBLE);
                        gone = false;

                    }


                }
                return true;
            }
        });

    }



回答2:


Well, it somehow worked on removing the setOnTouchListener() from layout and adding it on the ViewPager. My Updated code(with other changes for differentiating tap and swipe) is:

package com.pankajvatsa.testfeet;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageViewPager extends Activity {
    // Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;
boolean gone = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.activity_image_view_pager);

    bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
    bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);

    mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);

    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapter imageAdapter = new ImageAdapter(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        images.add(imageView);
    }

    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
    ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
    viewpager.setAdapter(pageradapter);
    // Show images following the position
    viewpager.setCurrentItem(position);

    viewpager.setOnTouchListener(new View.OnTouchListener() {


        private float pointX;
        private float pointY;
        private int tolerance = 50;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE: 
                return false;
            case MotionEvent.ACTION_DOWN:
                pointX = event.getX();
                pointY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
                boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
                if(sameX && sameY){
                        //The user "clicked" certain point in the screen or just returned to the same position an raised the finger

                    if(gone == false){
                        bWallpaperButton.setVisibility(View.GONE);
                        bDownloadButton.setVisibility(View.GONE);
                        gone = true;
                    }else{
                        bWallpaperButton.setVisibility(View.VISIBLE);
                        bDownloadButton.setVisibility(View.VISIBLE);
                        gone = false;
                    }
                }
            }
            return false;

        }
    });


}

}


来源:https://stackoverflow.com/questions/17474384/how-to-hide-and-show-buttons-on-alternate-touch-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!