我想在ViewPager(使用PagerAdapter)中使用三个先前创建的活动,以便用户可以水平平滑地滚动它们.
我按照了一个很好的教程.问题出在他们使用TextViews演示的教程中.我已经完成了活动(布局在 XML文件中).我想现在在滑块中使用这些活动,但看起来我只能使用Views.
我无法弄清楚如何更改类的代码(从“implements Activity”到“extends View”),我可以在滑块中使用它. @H_502_4@我当前的代码如下所示:
我按照了一个很好的教程.问题出在他们使用TextViews演示的教程中.我已经完成了活动(布局在 XML文件中).我想现在在滑块中使用这些活动,但看起来我只能使用Views.
我无法弄清楚如何更改类的代码(从“implements Activity”到“extends View”),我可以在滑块中使用它. @H_502_4@我当前的代码如下所示:
@H_502_4@然后使用PageAdapter的内部类:
- public class HorizontalSliderBeispielActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- cxt = this;
- awesomeAdapter = new AwesomePagerAdapter();
- awesomePager = (ViewPager) findViewById(R.id.awesomepager);
- awesomePager.setAdapter(awesomeAdapter);
- }
- ...
@H_502_4@而不是TextView“tv”我想使用Activities(即SubmitCheatInstructions).以前这个班看起来像:
- ...
- private class AwesomePagerAdapter extends PagerAdapter {
- public Object instantiateItem(View collection,int position) {
- TextView tv = new TextView(cxt);
- tv.setText("Bonjour PAUG " + position);
- tv.setTextColor(Color.WHITE);
- tv.setTextSize(30);
- view_01 = new SubmitCheatInstructions(cxt);
- ((ViewPager) collection).addView(tv,0);
- ((ViewPager) collection).addView(view_01,1);
- return tv;
- }
- }
@H_502_4@} @H_502_4@但据我所知,我必须改变它
- public class SubmitCheatInstructions implements Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.submitcheat_instructions);
- }
@H_502_4@为了能够将它用于ViewPager. @H_502_4@我现在的问题是我想将布局xml文件(submitcheat_instructions.xml)中的布局加载到该视图中,而不是在代码中执行所有操作.我无法弄清楚我该怎么做. @H_502_4@谢谢你的帮助.
- public class SubmitCheatInstructions extends View {
- ????
- }
解决方法
我按照相同的教程和你一样,我最初无法实现这一点,但经过一些修补和试错后,下面的代码就像一个魅力:
@H_502_4@希望这有帮助,祝你好运谢里夫·马祖克
- /**
- * Create the page for the given position. The adapter is responsible
- * for adding the view to the container given here,although it only
- * must ensure this is done by the time it returns from
- * {@link #finishUpdate()}.
- *
- * @param container
- * The containing View in which the page will be shown.
- * @param position
- * The page position to be instantiated.
- * @return Returns an Object representing the new page. This does not
- * need to be a View,but can be some other container of the
- * page.
- */
- @Override
- public Object instantiateItem(View collection,int position) {
- View v = new View(PatientView.this.getApplicationContext());
- final LayoutInflater inflater = (LayoutInflater) PatientView.this
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- switch (position) {
- case 0:
- v = inflater.inflate(R.layout.patientp1,(ViewGroup) null,false);
- ((Button) v.findViewById(R.id.pp1btnbck)).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- finish();
- }
- });
- break;
- case 1:
- v = inflater.inflate(R.layout.patientp2,null,false
- );
- break;
- default:
- TextView tv = new TextView(PatientView.this.context);
- tv.setText("Page " + position);
- tv.setTextColor(Color.WHITE);
- tv.setTextSize(30);
- v = tv;
- break;
- }
- ((ViewPager) collection).addView(v,0);
- return v;
- }