我目前正在制作一本小书,例如出于教育目的的游戏,我正在使用页面查看器将我的 Strings.xml 中的信息放在每个页面上。这是一本相当小的书,所以我是这样做的。我显示了所有页面,您可以毫无问题地滑动浏览。当我只想显示一定间隔的页面时,我的问题就开始了。游戏中包含三个类(class)。第一课有 2 页 (1-2),第二课有 (3-9),第三课有 (10-15)。共有 15 页信息。
有没有办法让页面查看器只显示特定间隔的页面?
我让用户选择一个按钮,这会向另一个名为 BookActivity 的类发送一个包含所选类(class)的 Intent 。从那里我想弄清楚点击了哪一节课,并希望基于此显示某些页面。这是 bookActivity 类。
public class BookActivity extends Activity
{
private String getLessonName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
// getting object's properties from LoginActivity class.
getLessonName = intent.getStringExtra("nameOfLesson");
MyPageAdapter adapter = new MyPageAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
// Button addBasicQuestionsForFirstLesson = (Button)findViewById(R.id.addBasicQuestionsButton);
myPager.setAdapter(adapter);
if(getLessonName.equals("one"))
{
myPager.setCurrentItem(0);
if(myPager.getCurrentItem() == 1)
{
myPager.setCurrentItem(1);
}
}
else if(getLessonName.equals("two"))
{
myPager.setCurrentItem(2);
if(myPager.getCurrentItem() == 8)
{
myPager.setCurrentItem(8);
}
}
else if(getLessonName.equals("three"))
{
myPager.setCurrentItem(9);
if(myPager.getCurrentItem()==14)
{
myPager.setCurrentItem(14);
}
}
}
}
非常感谢您帮我看这个。如果有人知道如何做这样的事情,我将非常感激。如果有更好的方法,我不想为单独的页面创建多个 Activity ....
class MyPageAdapter extends PagerAdapter
{
public int getCount()
{
return 15;
}
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.activity_page_one;
break;
case 1:
// second page with button to add questions!
resId = R.layout.activity_page_two;
View view = inflater.inflate(resId, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.linearbasic);
Button addButton = (Button) layout
.findViewById(R.id.addBasicQuestionsButton);
((ViewPager) collection).addView(view, 0);
addButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("clicked", "clclcl");
LessonActivity.
importQuestions("lessononebasicquestions");
// LessonActivity.clicked = true;
// Toast.makeText(getApplicationContext(), "yay",
// Toast.LENGTH_LONG).show();
}
});
return view;
// break;
case 2:
resId = R.layout.activity_page_three;
break;
case 3:
resId = R.layout.activity_page_four;
break;
case 4:
resId = R.layout.activity_page_five;
break;
case 5:
resId = R.layout.activity_page_six;
break;
case 6:
resId = R.layout.activity_page_seven;
break;
case 7:
resId = R.layout.activity_page_eight;
break;
case 8:
resId = R.layout.activity_page_nine;
View view2 = inflater.inflate(resId, null);
LinearLayout layout2 = (LinearLayout) view2
.findViewById(R.id.linearlife);
Button lifeCycleButton = (Button) layout2
.findViewById(R.id.addLifeCycleButton);
((ViewPager) collection).addView(view2, 0);
lifeCycleButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("clicked", "clclcl");
LessonActivity
.importQuestions("lessontwolifecyclequestions");
// LessonActivity.clicked = true;
// Toast.makeText(getApplicationContext(), "yay",
// Toast.LENGTH_LONG).show();
}
});
return view2;
case 9:
resId = R.layout.activity_page_ten;
break;
case 10:
resId = R.layout.activity_page_eleven;
break;
case 11:
resId = R.layout.activity_page_twelve;
break;
case 12:
resId = R.layout.activity_page_thirteen;
break;
case 13:
resId = R.layout.activity_page_fourteen;
break;
case 14:
resId = R.layout.activity_page_fifteen;
View view3 = inflater.inflate(resId, null);
LinearLayout layout3 = (LinearLayout) view3
.findViewById(R.id.lineartools);
Button toolsButton = (Button) layout3
.findViewById(R.id.addToolsQuestionsButton);
((ViewPager) collection).addView(view3, 0);
toolsButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("clicked", "clclcl");
LessonActivity.
importQuestions("lessonthreetoolsquestions");
// LessonActivity.clicked = true;
// Toast.makeText(getApplicationContext(), "yay",
// Toast.LENGTH_LONG).show();
}
});
return view3;
}
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 Parcelable saveState()
{
return null;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
再次感谢。这是我添加的代码!
最佳答案
您可以为页面更改使用监听器,然后在用户超出您的范围时强制用户转到特定页面。使用 onPageSelected()
,您可以检测越界页面,然后强制用户返回上一页。
这是听众:http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html
不过,我认为更好的解决方案是根据用户选择的类(class)只将某些页面添加到您的 ViewPager。如果您有一个带有按钮的主 Activity 来选择类(class),那么当单击它们时,它们可以启动不同的 Activity,这些 Activity 的 ViewPagers 中填充了您想要的页面。所以它看起来像:
MainActivity
- Button1
-Activity with ViewPager for pages 1-2
- Button2
-Activity with ViewPager for pages 3-9
- Button3
-Activity with ViewPager for pages 10-15
我认为这样的结构更有条理。
关于android - 如何通过页面查看器显示页面间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885770/