2012-11-27 34 views
9

我對android開發很陌生,目前我正在使用viewpager,試圖瞭解如何處理觸摸/點擊事件。我花了整整一個早上尋找答案,我似乎無法找到任何可以幫助我的東西。在viewpager中檢測觸摸/點按事件

基本上我有一個viewpager與5片段,所有使用相同的佈局。我想要做的就是能夠檢測靜止視圖上的點擊/觸摸(當拖動視圖時,我不希望發生任何檢測)。

從我收集的信息來看,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; 
    } 
} 

FYI我檢查了兩個這些資源,因爲它們經常被認爲是我面臨的問題的最佳解決方案(從我能收集的內容中):

android: ViewPager and HorizontalScrollVIew

Fling gesture detection on grid layout

但是我一直沒能從他們那裏得到很多幫助。就像我說的,我對android的工作很陌生,所以我真的很想熟悉這些基礎知識!

感謝

編輯:只是爲了澄清,我想拿起每片段內的觸摸事件,因爲一旦我得到過去的這個障礙,我想一些片斷有工作時,觸摸事件其他人只會顯示無響應的內容。

+0

你想觸摸viewpager嗎?或裏面的碎片? – Yahor10

+0

剛剛編輯:-) – alimac83

回答

8
@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; 
    } 
+0

謝謝Yahor!它現在正在接觸觸摸事件,但它也會在滑動中進行拾取。是否可以在視圖被滑動時忽略觸摸,但在視圖靜止時檢測它? – alimac83

+4

處理MotionEvent代碼if(event == MotionEvent.ACTION_DOWN)//處理if(event == MotionEvent.MOVE)//跳過 – Yahor10

+0

aha,我明白了。非常感謝! – alimac83