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

android – ViewPager使用xml文件作为视图,第1张

概述我想在ViewPager(使用PagerAdapter)中使用三个先前创建的活动,以便用户可以水平平滑地滚动它们. 我按照了一个很好的教程.问题出在他们使用TextViews演示的教程中.我已经完成了活动(布局在 XML文件中).我想现在在滑块中使用这些活动,但看起来我只能使用Views. 我无法弄清楚如何更改类的代码(从“implements Activity”到“extends View”), 我想在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 – ViewPager使用xml文件作为视图全部内容,希望文章能够帮你解决android – ViewPager使用xml文件作为视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1135645.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存