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

前端之家收集整理的这篇文章主要介绍了android – ViewPager使用xml文件作为视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在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)中的布局加载到该视图中,而不是在代码中执行所有操作.我无法弄清楚我该怎么做.

谢谢你的帮助.

解决方法

我按照相同的教程和你一样,我最初无法实现这一点,但经过一些修补和试错后,下面的代码就像一个魅力:
/**
     * 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;
    }

希望这有帮助,祝你好运谢里夫·马祖克

猜你在找的Android相关文章