您好我正在使用本教程http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging製作一個ViewPager應用程序,此處使用的ViewPager很酷的一點是每個頁面都有基於xml的不同佈局。問題是我嘗試了不同的方法來使我在佈局中創建的按鈕能夠工作,但我無法找到放置onClick監聽器的地方。下面是該ViewPager適配器:在視圖尋呼機中實現監聽器
class UniversePagerAdapter extends PagerAdapter {
protected static final String[] CONTENT = new String[] { "One", "Two", "Three", "Four", };
public int getCount() {
return 4;
}
Dashboard activity;
public UniversePagerAdapter(Dashboard activity){
this.activity = activity;
}
public Object instantiateItem(final View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.one;
break;
case 1:
resId = R.layout.two;
break;
case 2:
resId = R.layout.three;
break;
case 3:
resId = R.layout.four;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@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;
}
@Override
public CharSequence getPageTitle(int position) {
return UniversePagerAdapter.CONTENT[position % CONTENT.length];
}
}
這裏的地方它綁定部分:
public class Dashboard extends BaseActivity implements OnClickListener {
Button bNewButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
setAdapter();
}
public void initPagerView(int position, View view) {
bNewButton = (Button) view.findViewById(R.id.bNewUniverse);
bNewButton.setOnClickListener(this);
}
private void setAdapter() {
// TODO Auto-generated method stub
mAdapter = new UniversePagerAdapter(Dashboard.this);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(1);
mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "It Lives!!!!",
Toast.LENGTH_LONG).show();
}
}
我正在初始化,並使用在XML佈局創建的對象,因爲他們都在的問題適配器。 請幫我一下嗎?
測試答案,並會盡快給出反饋:-) –