如何阻止OnSwipeListener捕获OnClickListener的事件?

时间:2017-09-09 12:15:59

标签: android onclicklistener android-gridview ontouchlistener

我有一个gridView,我在其中存储图片。现在我想在用户滑动时更改gridView的内容(我使用gridView.invalidateViews()),但是当用户点击图片时我还需要启动一个新活动。 不幸的是,OnSwipeListener捕获了一些ClickEvents,因此ClickListener不再正常工作。这是代码:

final GridView gridView = (GridView) findViewById(R.id.gridView);
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //PULL UP THE HERO STATS ACTIVITY HERE
            //setContentView(R.layout.activity_herostats);

            Intent startHeroStats = new Intent (typeHeroes.getContext(), HeroStats.class);
            int heropath = ActiveHeroGridInt[position];
            //int heropath = getResources().getIdentifier(Agility[POSITION], "drawable", getPackageName());
            startHeroStats.putExtra("heropathkey", heropath);
            startActivity(startHeroStats);
        }
    });

    //SET UP THE SWIPE LISTENER
    gridView.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
        /*public void onSwipeTop() {
            Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
        }*/
        public void onSwipeRight() {
            Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();

        }
        public void onSwipeLeft() {
            Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
            if (ActiveHeroPage<2) {
                ActiveHeroPage+=1;
            }
            ActiveHeroGrid=StrengthInteger;
            ActiveHeroGridInt=Strengthint;
            gridView.invalidateViews();
        }

        /*public void onSwipeBottom() {
            Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
        }*/

    });

我该如何解决这个问题?非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

您不能同时使用print(Ci.pitch) onClickListener

答案 1 :(得分:0)

您可以尝试使用拦截触摸事件并返回true以阻止它为允许它。使用该方法,您可以对垂直/水平滑动进行区分,阻止点击并传递滑动,反之亦然。