我正在尝试使用一个用户可以在每个窗格中进行交互的ViewPager。对于这个ViewPager,我创建了一个教程,在这个教程中我向用户展示了水平滑动切换窗格。当用户到达最后一个窗格时,将向用户显示一个按钮,在此按钮将被导航到仪表板。我读过一些文章,试图理解按钮在ViewPager中是如何工作的,但并不是很有意义。
代码应该做的是,当单击ViewPager中的窗格中的按钮时,将启动仪表板活动。
下面是pagerAdapter的完整代码:
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
View v = new View(collection.getContext());
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.tutorial_step1;
break;
case 1:
resId = R.layout.tutorial_step2;
break;
case 2:
resId = R.layout.tutorial_step3;
break;
case 3:
resId = R.layout.tutorial_step4;
break;
case 4:
resId = R.layout.tutorial_complete;
v = inflater.inflate(R.layout.tutorial_complete, null, false);
LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
Button button=(Button)l.findViewById(R.id.bTutorialComplete);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Create an intent that starts a different activity
}
});
break;
}
((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;
}
}从我在文章中看到的情况来看,我应该使用的代码是:
public Object instantiateItem(View collection, int position) {
View v = new View(collection.getContext());
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.tutorial_step1;
break;
case 1:
resId = R.layout.tutorial_step2;
break;
case 2:
resId = R.layout.tutorial_step3;
break;
case 3:
resId = R.layout.tutorial_step4;
break;
case 4:
resId = R.layout.tutorial_complete;
v = inflater.inflate(R.layout.tutorial_complete, null, false);
LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
Button button=(Button)l.findViewById(R.id.bTutorialComplete);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Create an intent that starts a different activity
}
});
break;
}
((ViewPager) collection).addView(v, 0);
return v;
}我使用的只是switch语句中的case 4:,因为它有一个按钮,而其他布局只包含图像和文本,不需要交互。
我已经在这个问题上工作了无数个小时,但仍然找不到解决这个问题的方法。如果你能帮我解决这个问题,我将不胜感激。
发布于 2012-03-15 15:20:54
final Context context = collection.getContext();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context, Dashboard.class));
}
});
break;附注:不能将collection设置为final,因为instantiateItem方法是@Override
提示:如果您想制作不同的页面,可以将代码放在switch(position)中
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
View v = null;
switch (position) {
case 0:
v = inflater.inflate(R.layout.somelayout);
Button btn = (Button) v.findViewById(R.id.somebutton);
btn.setText("btn");
break;
case 1:
v = inflater.inflate(R.layout.someotherlayout);
TextView tv = (Button) v.findViewById(R.id.sometextview);
tv.setText("btn");
break;
}
((ViewPager) collection).addView(v, 0);
return v;发布于 2012-03-15 11:36:15
看起来你想要创建一个意图,然后用它开始一个活动。您可以使用已经访问的“集合”中的上下文。下面是一些可以让你重新开始的代码:
public void onClick(View v) {
Context context = collection.getContext();
Intent intent = new Intent(context, DashboardActivity.class);
context.startActivity(intent);
}请注意,您可能必须使集合成为最终的集合。
https://stackoverflow.com/questions/9695222
复制相似问题