【问题标题】:Swipe-touch interface for a GridView of Buttons按钮 GridView 的滑动式触摸界面
【发布时间】:2016-02-24 18:07:31
【问题描述】:

是否有人知道(或认识其他人)如何为 Android 可显示小部件(即按钮、ImageView 图像等)的 GridView 实现滑动触摸界面,而不仅仅是一个 GridView,比如一个字符串数组?一个很好的例子是 Bejeweled 或 Candy Crush,用户可以通过滑动来交换图块。

Here's my post in more detail.

因为,我目前正在开发一款基于经典益智游戏 Puzzle-15 的应用程序,并且我正在使用方向按钮(上、下、左、右)来交换编号瓷砖(按钮对象)在板上(GridView)...

Here's my app.

非常感谢!

【问题讨论】:

  • 问题是事件没有进入你的 gridview ?你可以试试这个解决方案:stackoverflow.com/questions/6374592/…。如果它不起作用,您必须找到一种方法来捕捉事件。您可以尝试添加一个透明视图并在此视图中声明 ontouchlistner。如果我不明白,请告诉我大声笑
  • @user3549047 非常感谢您的回复;尽管帖子很乏味,但这对我来说真的很重要......我有时间会去看看
  • 没问题 ;) 如果您需要帮助,请告诉我
  • 对不起,我不明白你的问题。当您触摸甚至在屏幕之外时,它会滑动吗?或者是因为当你触摸它时还是会滑动?我认为你需要修复一个限制。例如,用户需要从至少 10 个像素开始滚动,因此如果差异 > 10 则为滑动,否则他只是触摸了屏幕。我说的是 10,但也许触摸会从 10 移动,所以放 30,你必须测试。
  • 我编辑了我的答案(在 onTouch 方法中)让我知道。

标签: java android button gridview swipe


【解决方案1】:

好的,对于透明视图,这是我要做的:

xml 布局(我将自定义视图放在按钮上):

<?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:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test Button"/>

<com.example.trist_000.teststack.CustomView
    android:id="@+id/customv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

您需要一个自定义视图来处理滑动:

public class CustomView extends View implements View.OnTouchListener {


float save_x = 0;
float save_y = 0;

@Override
public boolean onTouch(View v, MotionEvent event) {


    if (mSwipeInterface == null)
        return false;

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        save_x = event.getX();
        save_y = event.getY();
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        float difx = event.getX() - save_x;
        float dify = event.getY() - save_y;

        if (IsBigger(difx, dify)) {
            if (difx > -30 && difx < 30)return false;
            if (difx > 0){
                mSwipeInterface.swipe(SwipeEnum.RIGHT);
            }
            else{
                mSwipeInterface.swipe(SwipeEnum.LEFT);
            }
        }
        else{
            if (dify > -30 && dify < 30)return false;
            if (dify > 0){
                mSwipeInterface.swipe(SwipeEnum.DOWN);
            }
            else{
                mSwipeInterface.swipe(SwipeEnum.TOP);
            }
        }


    }
    return true;
}

private boolean IsBigger(float difx, float dify) {
    if (difx < 0) {
        difx *= -1;
    }
    if (dify < 0) {
        dify *= -1;
    }

    if (difx > dify){
        return true;
    }
    return false;
}

public enum SwipeEnum {
    LEFT,
    DOWN,
    RIGHT,
    TOP;
}

interface swipeInterface {
    public void swipe(SwipeEnum swipeEnum);
}

private swipeInterface mSwipeInterface = null;

public void setSwipeListener(swipeInterface swipe) {
    mSwipeInterface = swipe;
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
}

public CustomView(Context context) {
    super(context);
}
}

在主要活动中:

public class MainActivity extends Activity {


Button mButton;
CustomView mCustomView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mButton = (Button)findViewById(R.id.button);
    mCustomView = (CustomView)findViewById(R.id.customv);

    mCustomView.setSwipeListener(new CustomView.swipeInterface() {
        @Override
        public void swipe(CustomView.SwipeEnum swipeEnum) {
            if (swipeEnum == CustomView.SwipeEnum.DOWN){
                mButton.setY(200);
            }
            if (swipeEnum == CustomView.SwipeEnum.TOP){
                mButton.setY(0);
            }
            if (swipeEnum == CustomView.SwipeEnum.RIGHT){
                mButton.setX(200);
            }
            if (swipeEnum == CustomView.SwipeEnum.LEFT){
                mButton.setX(0);
            }
        }
    });
}
}

我只是放了一个按钮,但只是放了你的 gridview。并用您自己的逻辑替换我在 MainActivity 中的不同“if”中输入的内容。

如果您对代码有任何疑问,请立即告诉我。

PS:Algos 可能不是最佳的,哈哈,但我测试过,它可以工作,你可以试试 ;)

【讨论】:

  • 正是我要找的...非常感谢,特里斯特!你是最好的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
相关资源
最近更新 更多