InstantiateItem在PagerAdapter和AddView在ViewPager混乱 [英] InstantiateItem in PagerAdapter and AddView in ViewPager confusion

查看:755
本文介绍了InstantiateItem在PagerAdapter和AddView在ViewPager混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在黑客在一起的例子来感受一下这些API的Anfroid SDK作为一个整体。不过,我已经打了僵局。

我想从XML文件膨胀时2 TextViews一个LinearLayout中。这些都将是那么的分页浏览。我只是canot得到这个工作,并认识到,我完全不明白什么是与code回事。

看着我可以看到,该方法ViewPager.addNewItem调用从提供的适配器InstantiateItem源。而addNewItem被称为填充()。填写被称为在其他一些地方。

反正在这个例子中,我看到,当该方法被重写为PagerAdapter必须包含对AddView上从viewpager作为参数传递的ViewPager总汇。

如果我想补充我想这将是调用addView超过一次的情况多的观点,但作为instantiateItem返回一个对象以ViewPager(在这个例子中我有它返回它添加视图)我不知道返回什么。我试图返回膨胀图和一些其他的东西。

请能有人解释这里发生了什么?

许多AP preciated。

M

  @覆盖
公共对象instantiateItem(查看收集,INT位置){

    布局= inflater.inflate(R.layout.animallayout,NULL);
    TextView的电视=(TextView中)layout.findViewById(R.id.textView1);
    tv.setText(1 ________________>+位置);

    TextView的TV2 =(TextView中)layout.findViewById(R.id.textView2);
    tv.setText(2 ________________>+位置);

    ((ViewPager)集合).addView(布局);
    返回布局;
}
 

解决方案

最近我实现这一点,这是我的 instantiateItem 办法(使它有点最小的可读性

  @覆盖
公共对象instantiateItem(查看收集,INT位置){
    评估评估= evaluations.get(位置);

    查看布局= inflater.inflate(R.layout.layout_evaluation,NULL);

    TextView的evaluationSummary =(TextView中)layout.findViewById(R.id.evaluation_summary);
    evaluationSummary.setText(evaluation.getEvaluationSummary());

    ((ViewPager)集合).addView(布局);

    返回布局;
}

@覆盖
公共无效destroyItem(查看收集,INT位置,对象视图){
     ((ViewPager)集合).removeView((视图));
}

@覆盖
公共布尔isViewFromObject(查看视图,Object对象){
    返回查看==对象;
}
 

因此​​,对于所显示的页面中,我从评估得到的数据使用位置的索引列表。 然后充气布局里面有我将添加我的数据太多的意见。 然后我得到了的TextView 来设置评价摘要文本。 那么整个布局添加到 ViewPager 。 最后的布局也回来了。

如果您仍然无法得到它发布您的code。

So far I've been hacking together examples to get a feel for the APIs the Anfroid SDK as a whole. However I've hit an impasse.

I'm trying to Inflate a LinearLayout with 2 TextViews from an XML file. These will be then Views that are paged. I simply canot get this to work and realise that I totally don't understand what's going on with the code.

Looking at the source I can see that the method ViewPager.addNewItem calls the InstantiateItem from the supplied adapter. And addNewItem is called in populate(). populate is called in a number of other places.

Anyway in the example I've seen when the method is overridden for PagerAdapter you must include a call to AddView on the ViewPager colletion that is passed from the viewpager as an argument.

If I wish to add multiple views I thought this would be a case of calling addView more than once but as instantiateItem returns an Object to ViewPager (in the example I had it returns the view that it added) I don't know what to return. I've tried returning the inflated view and a number of other things.

Please can someone explain what is happening here?

Much appreciated.

M

@Override
public Object instantiateItem(View collection, int position) {

    layout = inflater.inflate(R.layout.animallayout, null);
    TextView tv = (TextView) layout.findViewById(R.id.textView1);
    tv.setText("1________________>" + position);

    TextView tv2 = (TextView) layout.findViewById(R.id.textView2);
    tv.setText("2________________>" + position);

    ((ViewPager) collection).addView(layout);
    return layout;
}

解决方案

I've recently implemented this and this is my instantiateItem method (made it a bit minimal for readability.

@Override
public Object instantiateItem(View collection, int position) {
    Evaluation evaluation = evaluations.get(position);

    View layout = inflater.inflate(R.layout.layout_evaluation, null);

    TextView evaluationSummary = (TextView) layout.findViewById(R.id.evaluation_summary);
    evaluationSummary.setText(evaluation.getEvaluationSummary());

    ((ViewPager) collection).addView(layout);

    return layout;
}

@Override
public void destroyItem(View collection, int position, Object view) {
     ((ViewPager) collection).removeView((View) view);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

So for the page that is displayed, I get the data from my evaluations list using the position as the index. Then inflate the Layout which has the views I will add my data too. Then I get the TextView to set the evaluation summary text on. Then the whole Layout is added to the ViewPager. And finally the Layout is also returned.

If you still can't get it post your code.

这篇关于InstantiateItem在PagerAdapter和AddView在ViewPager混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆