我的目標是能夠滑過3種不同的佈局,並且能夠點擊每個佈局上的項目。目前滑動運行良好,所有3種佈局均可查看。Android Viewpager項目訪問
活動:
public class FetchMenu extends Fetch {
protected ImageView block;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//pagerviwer
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
//icon on layout 1
fbicon = (ImageView)findViewById(R.id.facebookicon);
fbicon.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/pages/Fetch-Training/174358202669554"));
startActivity(browserIntent);
// TODO Auto-generated method stub
return false;
}
});
PageAdapter:
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.training_topics;
break;
case 1:
resId = R.layout.behaviour_topics;
break;
case 2:
resId = R.layout.tricks_topics;
break;
}
View v = inflater.inflate(resId, null);
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
問題: 的每個打通刷卡布局,包含了一些ImageViews的,如果我在我的活動設置代碼那些傾聽他們對圖像的觸動,我得到了一個關閉力的錯誤。我猜測活動中的代碼不知道ImageView存儲在哪個佈局中?我已閱讀關於片段是我需要的嗎?
謝謝你們一直愛幫助