我想将另一个布局扩展到最后一页上有viewpager的活动。它就像一个测验,其中总是回答问题(在问题和答案的集合中,在viewpager中填充的布局中)我想在我的viewpager的最后部分膨胀另一个布局并显示一条消息"感谢您参加测验!",出于调试目的,我尝试在public Object instantiateItem(View container, int position)
方法上写一些条件
if (position == getCount()){ //get count returns the number of pagesize
//do some layout inflating here
}
要调试,我使用Toast消息来显示当前页面,但是如果我在最后一页上刷了viewpager它没有显示什么,修复它的正确方法是什么?
更新代码
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
ViewPager mPager = (ViewPager) survey.findViewById(R.id.pagersurvey);
Toast.makeText(survey, "Page " + mPager.getCurrentItem(), Toast.LENGTH_SHORT).show();
LayoutInflater inflater = (LayoutInflater) survey
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.view_survey, null);
((ViewPager) container).addView(layout, 0);
return layout;
}
我希望每个幻灯片/页面都显示其当前页面的Toast消息但是,viewpager的最后一页不显示任何Toast消息。
答案 0 :(得分:0)
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
Toast.makeText(survey, "Page " + position, Toast.LENGTH_SHORT).show();
LayoutInflater inflater = (LayoutInflater) survey
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = R.layout.normalView;
if(position==getCount)
resID = R.layout.SpecificView;
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
答案 1 :(得分:0)
创建一个监听器并在viewpager上设置它:
/**
* Get the current view position from the ViewPager by
* extending SimpleOnPageChangeListener class and adding your method
*/
public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
private int currentPage;
@Override
public void onPageSelected(int position) {
currentPage = position;
}
public final int getCurrentPage() {
return currentPage;
}
}
或者只是尝试这行代码
mViewPager.getCurrentItem()
来自http://developer.android.com/reference/android/support/v4/view/ViewPager.html#getCurrentItem%28%29 的来源和更多信息