PagerAdapter 中的 instantiateItem(ViewGroup, int) 和 ViewPager 中的 AddView 混淆

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

问题描述

到目前为止,我一直在整理示例以了解整个 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() 将 Object 返回给 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.

这篇关于PagerAdapter 中的 instantiateItem(ViewGroup, int) 和 ViewPager 中的 AddView 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)