PagerAdapter中的InstantiateItem(ViewGroup,int)和ViewPager中的AddView混淆 [英] instantiateItem(ViewGroup, int) in PagerAdapter and AddView in ViewPager confusion
问题描述
到目前为止,我一直在整理示例,以整体了解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个TextViews膨胀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重写该方法时,必须在对addView()
的调用中包含对addView()
的调用,该调用是从ViewPager
作为参数传递的.
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混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!