在ViewPager中检测触摸/点击事件 [英] Detecting touch/tap events within a viewpager

查看:90
本文介绍了在ViewPager中检测触摸/点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android开发非常陌生,目前正在使用Viewpager,试图了解如何处理触摸/点击事件.我整个上午都在寻找答案,但似乎找不到任何可以帮助我的东西.

I'm quite new to android development and at the moment I'm working with a viewpager, trying to understand how I can handle touch/tap events. I've spent all morning searching for answers to this and I can't seem to find anything that helps me.

基本上,我有一个viewpager,其中有5个片段都使用相同的布局.我要做的就是能够检测到固定视图上是否有轻按/触摸(我不希望在拖动视图时进行任何检测).

Basically I have a viewpager with 5 fragments that all use the same layout. All I want to do is be able to detect when there's a tap/touch on the stationary view (I don't want any detection to happen when a view is being dragged).

从我收集到的信息来看,默认情况下,ViewPager似乎在拦截所有触摸事件方面存在问题.我相信可以使用以下方法解决此问题:

From what I gather, there seems to be an issue with the ViewPager intercepting all touch events by default. I believe this can be dealt with by using:

myPager.requestDisallowInterceptTouchEvent(true);

起初,我涉猎OnTouchListener并尝试记录onTouch方法是否曾经被击中-看来没有.我也已经看到可以使用GestureDetector类作为解决此问题的方法,但坦率地说,我感到不知所措,并且不确定在这里应使用哪些类/接口.

At first I dabbled with OnTouchListener and tried to log whether the onTouch method was ever hit - it appeared not. I've also seen the GestureDetector class thrown about a lot as a solution to this problem but frankly I feel overwhelmed and I'm not sure which classes/interfaces I should be using here.

我要做的就是在使用ViewPager时拾取简单的触摸/点击事件.

All I'd like to do is pick up simple touch/tap events while using a ViewPager.

目前,我正在使用android开发者网站上的动画教程代码的修改版本.这是我的 MainActivity.java 文件中的内容:

At the moment I'm using a modified version of the Animation tutorial code from the android developer site. This is what I have in my MainActivity.java file:

package com.example.pbinteractive;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
    /**
     * The number of pages (wizard steps) to show in this demo.
     */
    private static final int NUM_PAGES = 5;

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);       
    }   

    /**
     * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }        

        @Override
        public Fragment getItem(int position) {
            return MyPageFragment.create(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }   
}

这就是我的 MyPageFragment.java 文件中的内容:

And this is what I have in my MyPageFragment.java file:

package com.example.pbinteractive;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

public class MyPageFragment extends Fragment implements OnTouchListener {
    /**
     * The argument key for the page number this fragment represents.
     */
    public static final String ARG_PAGE = "page";

    /**
     * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
     */
    private int mPageNumber;

    /**
     * Factory method for this fragment class. Constructs a new fragment for the given page number.
     */
    public static MyPageFragment create(int pageNumber) {
        MyPageFragment fragment = new MyPageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public MyPageFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout containing a title and body text.
        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_slide, container, false);

        // Set the title view to show the page number.
        //((TextView) rootView.findViewById(android.R.id.text1)).setText(
        //        getString(R.string.title_template_step, mPageNumber + 1));

        return rootView;
    }

    /**
     * Returns the page number represented by this fragment object.
     */
    public int getPageNumber() {
        return mPageNumber;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }   
}

仅供参考,我已经检查了这两个资源,因为它们经常被认为是解决我所面临问题的最佳方法(据我所知):

FYI I've checked both of these resources, as they often seem to be cited as the best solution to the problem I'm facing (from what I can gather):

android:ViewPager和Horizo​​ntalScrollVIew

在网格布局上显示手势手势

但是我无法从他们那里得到很多帮助.就像我说的那样,我刚开始使用android,所以我真的很想熟悉基础知识!

But I haven't been able to get much help from them. Like I said I'm quite new to working with android so I'd really like to just get familiar with the basics!

谢谢

为澄清起见,我想在每个片段中获取触摸事件,因为一旦我越过了这一障碍,我希望某些片段可以使触摸事件正常工作,而其他片段只会显示无响应的内容.

Just to clarify, I'd like to pick up the touch events within each fragment, since once I get past this hurdle I'd like some fragments to have touch events working while others will just display unresponsive content.

推荐答案

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_slide, container, false);
LinearLayout layout = (LinearLayout)rootView.findViewById(R.id.layout)// get your root  layout
layout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.v(null, "TOUCH EVENT"); // handle your fragment number here
            return false;
        }
    });
      return rootView;
    }

这篇关于在ViewPager中检测触摸/点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆