10

我对android开发很陌生,目前我正在使用viewpager,试图了解如何处理触摸/点击事件。我整个上午都在寻找这个问题的答案,但我似乎找不到任何对我有帮助的东西。

基本上我有一个带有 5 个片段的 viewpager,它们都使用相同的布局。我想要做的就是能够检测到静止视图上何时有点击/触摸(我不希望在拖动视图时发生任何检测)。

据我所知,ViewPager 默认拦截所有触摸事件似乎存在问题。我相信这可以通过使用来解决:

myPager.requestDisallowInterceptTouchEvent(true);

起初我涉足 OnTouchListener 并尝试记录是否曾经点击过 onTouch 方法 - 它似乎没有。我也看到了 GestureDetector 类作为解决这个问题的方法,但坦率地说,我感到不知所措,我不确定我应该在这里使用哪些类/接口。

我想做的就是在使用 ViewPager 时获取简单的触摸/点击事件。

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

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文件中的内容:

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;
    }   
}

仅供参考,我已经检查了这两种资源,因为它们似乎经常被引用为我所面临问题的最佳解决方案(据我所知):

android: ViewPager 和 Horizo​​ntalScrollVIew

网格布局上的 Fling 手势检测

但我无法从他们那里得到太多帮助。就像我说的那样,我对使用 android 很陌生,所以我真的很想熟悉基础知识!

谢谢

编辑:澄清一下,我想在每个片段中获取触摸事件,因为一旦我克服了这个障碍,我希望一些片段能够使触摸事件工作,而其他片段只会显示无响应的内容。

4

1 回答 1

9
 @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;
    }
于 2012-11-27T12:15:37.487 回答