android – ViewPager使用xml文件作为视图

前端之家收集整理的这篇文章主要介绍了android – ViewPager使用xml文件作为视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在ViewPager(使用PagerAdapter)中使用三个先前创建的活动,以便用户可以水平平滑地滚动它们.
我按照了一个很好的教程.问题出在他们使用TextViews演示的教程中.我已经完成了活动(布局在 XML文件中).我想现在在滑块中使用这些活动,但看起来我只能使用Views.
我无法弄清楚如何更改类的代码(从“implements Activity”到“extends View”),我可以在滑块中使用它. @H_502_4@我当前的代码如下所示:

  1. public class HorizontalSliderBeispielActivity extends Activity {
  2.  
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main);
  6.  
  7. cxt = this;
  8. awesomeAdapter = new AwesomePagerAdapter();
  9. awesomePager = (ViewPager) findViewById(R.id.awesomepager);
  10. awesomePager.setAdapter(awesomeAdapter);
  11. }
  12. ...
@H_502_4@然后使用PageAdapter的内部类:

  1. ...
  2. private class AwesomePagerAdapter extends PagerAdapter {
  3.  
  4. public Object instantiateItem(View collection,int position) {
  5. TextView tv = new TextView(cxt);
  6. tv.setText("Bonjour PAUG " + position);
  7. tv.setTextColor(Color.WHITE);
  8. tv.setTextSize(30);
  9.  
  10. view_01 = new SubmitCheatInstructions(cxt);
  11.  
  12. ((ViewPager) collection).addView(tv,0);
  13. ((ViewPager) collection).addView(view_01,1);
  14.  
  15. return tv;
  16. }
  17. }
@H_502_4@而不是TextView“tv”我想使用Activities(即SubmitCheatInstructions).以前这个班看起来像:

  1. public class SubmitCheatInstructions implements Activity {
  2.  
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.submitcheat_instructions);
  6. }
@H_502_4@}

@H_502_4@但据我所知,我必须改变它

  1. public class SubmitCheatInstructions extends View {
  2. ????
  3. }
@H_502_4@为了能够将它用于ViewPager.

@H_502_4@我现在的问题是我想将布局xml文件(submitcheat_instructions.xml)中的布局加载到该视图中,而不是在代码中执行所有操作.我无法弄清楚我该怎么做.

@H_502_4@谢谢你的帮助.

解决方法

我按照相同的教程和你一样,我最初无法实现这一点,但经过一些修补和试错后,下面的代码就像一个魅力:
  1. /**
  2. * Create the page for the given position. The adapter is responsible
  3. * for adding the view to the container given here,although it only
  4. * must ensure this is done by the time it returns from
  5. * {@link #finishUpdate()}.
  6. *
  7. * @param container
  8. * The containing View in which the page will be shown.
  9. * @param position
  10. * The page position to be instantiated.
  11. * @return Returns an Object representing the new page. This does not
  12. * need to be a View,but can be some other container of the
  13. * page.
  14. */
  15. @Override
  16. public Object instantiateItem(View collection,int position) {
  17. View v = new View(PatientView.this.getApplicationContext());
  18. final LayoutInflater inflater = (LayoutInflater) PatientView.this
  19. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  20.  
  21. switch (position) {
  22. case 0:
  23. v = inflater.inflate(R.layout.patientp1,(ViewGroup) null,false);
  24. ((Button) v.findViewById(R.id.pp1btnbck)).setOnClickListener(new OnClickListener() {
  25.  
  26. @Override
  27. public void onClick(View arg0) {
  28. finish();
  29. }
  30. });
  31.  
  32. break;
  33. case 1:
  34. v = inflater.inflate(R.layout.patientp2,null,false
  35. );
  36. break;
  37. default:
  38.  
  39. TextView tv = new TextView(PatientView.this.context);
  40. tv.setText("Page " + position);
  41. tv.setTextColor(Color.WHITE);
  42. tv.setTextSize(30);
  43. v = tv;
  44. break;
  45. }
  46. ((ViewPager) collection).addView(v,0);
  47.  
  48. return v;
  49. }
@H_502_4@希望这有帮助,祝你好运谢里夫·马祖克

猜你在找的Android相关文章