InstantiateItem(ViewGroup, int) in PagerAdapter 和 AddView in ViewPager 混淆 [英] instantiateItem(ViewGroup, int) in PagerAdapter and AddView in ViewPager confusion

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

问题描述

到目前为止,我一直在拼凑示例,以了解 Android SDK 作为一个整体的 API.然而,我陷入了僵局.

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

我正在尝试使用来自 XML 文件的 2 个 TextView 来扩充 LinearLayout.这些将是被分页的视图.我根本无法让它工作并意识到我完全不明白代码发生了什么.

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

查看源代码,我可以看到方法 ViewPager.addNewItem 从提供的适配器调用 InstantiateItem.在 populate() 中调用 addNewItem.populate() 在许多其他地方被调用.

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.

无论如何,在示例中,我遇到过当为 PagerAdapter 重写该方法时,必须包含对从 ViewPager 传递的 ViewPager 集合上的 addView() 的调用 作为参数.

Anyway, in the example, I've experienced that when the method is overridden for PagerAdapter one must include a call to addView() on the ViewPager collection that is passed from the ViewPager as an argument.

如果我想添加多个视图,我认为这将是多次调用 addView() 的情况,但是因为 instantiateItem() 返回一个对象给 ViewPager(在示例中我让它返回它添加的视图)我不知道回什么.我试过返回膨胀的视图和其他一些东西.

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?

非常感谢.

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

推荐答案

我最近实现了这个,这是我的 instantiateItem 方法(为了可读性使它有点小.

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

所以对于显示的页面,我使用位置作为索引从我的 evaluations 列表中获取数据.然后膨胀具有视图的 Layout,我也会添加我的数据.然后我使用 TextView 来设置评估摘要文本.然后将整个Layout 添加到ViewPager.最后还返回了 Layout.

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(ViewGroup, int) in PagerAdapter 和 AddView in ViewPager 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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