2017-05-12 84 views
0

,每個圖像上單擊必須做一個動作,從圖像到另一個其不同的作用,圖像viewpager onclicklistener滑動圖像中使用<code>ViewPager</code>問題

我是新的android開發時

所以我儘量添加onClick監聽器ViewPager,但我不能達到它,

請任何建議如何實現OnClickListener所以當我點擊第一個圖像會做的動作,當單擊第二個圖像會做其他動作等所有圖像,我有它在視圖pa ger

在此先感謝。

MainActivity:

public class MainActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
ImageAdapter adapter = new ImageAdapter(this); 
viewPager.setAdapter(adapter); 
    } 


} 

ImageAdapter:

public class ImageAdapter extends PagerAdapter { 
Context context; 
private int[] GalImages = new int[] { 
    R.drawable.a, 
    R.drawable.b, 
    R.drawable.c, 
    R.drawable.d, 
    R.drawable.g, 
    R.drawable.h, 

}; 
ImageAdapter(Context context){ 
    this.context=context; 
} 
@Override 
public int getCount() { 
    return GalImages.length; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((ImageView) object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    ImageView imageView = new ImageView(context); 
    intpadding=context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin); 
    imageView.setPadding(padding, padding, padding, padding); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    imageView.setImageResource(GalImages[position]); 
    ((ViewPager) container).addView(imageView, 0); 
    return imageView; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    ((ViewPager) container).removeView((ImageView) object); 
} 
} 

回答

0

爲了趕上單擊ViewPager事件,您必須重寫onTouchListener,因爲默認實現不言處理點擊次數

你MainActivity應該是這個樣子:

private ViewPager mViewPager; 

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mViewPager= (ViewPager) findViewById(R.id.view_pager); 
     ImageAdapter adapter = new ImageAdapter(this); 
     mViewPager.setAdapter(adapter); 

     final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener()); 
     mViewPager.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       tapGestureDetector.onTouchEvent(event); 
       return false; 
      } 
     }); 
    } 


    private class TapGestureListener extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 

      if (mViewPager.getCurrentItem() == 0) { 
      //do your thing tied with first page 
      } else if (mViewPager.getCurrentItem() == 1) { 
      //do your thing tied with second page 
      } else if (mViewPager.getCurrentItem() == 2) { 
      //do your thing tied with third page 
      } 

      return super.onSingleTapConfirmed(e); 
     } 
    } 
+0

每個頁面,點擊後會運行不同的mp3所以每個頁面的作用會有所不同,如何獲取請,所有MP3存儲在應用程序本身,謝謝 – frozenhill4

+0

@ frozenhill4檢查我的editedAnswer –

+0

我是新來的android,我有兩個紅線錯誤(msend)mSend.setOnTouchListener(新的View.OnTouchListener(){也(mViewPager)中的所有(if&else代碼)),請問如何解決,謝謝 – frozenhill4

-1

試試這個:

@Override 
public Object instantiateItem(View collection, final int pos) { //have 
    to make final so we can see it inside of onClick() 
    LayoutInflater inflater = (LayoutInflater) collection.getContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    View page = inflater.inflate(R.layout.YOUR_PAGE, null); 

    page.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
      //this will log the page number that was click do your action 
    according to position 
      Log.i("TAG", "This page was clicked: " + pos); 
     } 
    }); 


    ((ViewPager) collection).addView(page, 0); 
    return page; 
}