ViewPager使用xml文件作为视图

时间:2011-10-09 16:34:16

标签: android xml layout

我想在ViewPager中使用三个先前创建的活动(使用PagerAdapter),以便用户可以水平平滑地滚动它们。 我按照了一个很有效的教程。问题出在他们使用TextViews演示的教程中。我已经完成了活动(布局在XML文件中)。我想现在在滑块中使用这些活动,但看起来我只能使用Views。 我无法弄清楚如何更改类的代码(从“implements Activity”到“extends View”),我可以在滑块中使用它。

我目前的代码如下:

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);
    }
...

然后是带有PageAdapter的内部类:

...
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;
    }
}

而不是TextView“tv”我想使用Activities(即SubmitCheatInstructions)。以前这个班看起来像:

public class SubmitCheatInstructions implements Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.submitcheat_instructions);
}

}

但据我所知,我必须将其更改为

public class SubmitCheatInstructions extends View {
    ????
}

以便能够将其用于ViewPager。

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

感谢您的帮助。

2 个答案:

答案 0 :(得分:6)

我遵循了相同的教程,就像你一开始我无法实现这一点,但经过一些修修补补和试错后,下面的代码就像一个魅力:

        /**
     * 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;
    }

希望这会有所帮助,祝你好运 Shereef Marzouk

答案 1 :(得分:1)

谢谢谢里夫。你的代码非常有用。 我想将数据从我的主要活动传递到ViewPager中的不同布局。我从你的代码中得到了你为按钮设置onClickListener的想法。这有助于我将主要活动中生成的数据传递到不同布局的不同TextView。

 public Object instantiateItem(View collection, int position)
    {
        View v = new View(CalcTimeActivity.this.getApplicationContext());
        collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LayoutInflater inflater = (LayoutInflater) CalcTimeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                switch (position) {
                case 0:
                    v = inflater.inflate(R.layout.N, (ViewGroup) null, false);
                    ((TextView) v.findViewById(R.id.tvA)).setText(strA);
                    ((TextView) v.findViewById(R.id.tvB)).setText(strB);
                    ((TextView) v.findViewById(R.id.tvC)).setText(strC);
                    break;
                case 1:
                    v = inflater.inflate(R.layout.M, null, false);
                    break;
                case 2:
                    v = inflater.inflate(R.layout.F, null, false);
                    break;
                }

                ((ViewPager) collection).addView(v, 0);
                return v;

}