我想更改 PagerAdapter 中图像按钮的可见性。似乎我不能通过 id 引用 instanceiteItem 中的部分布局,我只能引用完整布局。不过这很奇怪,因为似乎我仍然可以通过 id 引用我的 webviews。
我该如何处理?这就是我想要做的,但我在下面显示的最后一行得到一个空指针异常:
public Object instantiateItem(View collection, int position) {
ImageButton btnRight = (ImageButton) findViewById(R.id.buttonright);
ImageButton btnLeft = (ImageButton) findViewById(R.id.buttonleft);
if (position == 0) {
resLayout = R.layout.the_webview;
urltoload = theUrls[position];
resId = R.id.webview0;
btnLeft.setVisibility(View.INVISIBLE);
View view = inflater.inflate(resLayout, null);
...
if (position != 2) {
mainWebView = (WebView) view.findViewById(resId);
mainWebView.getSettings().setJavaScriptEnabled(true);
mainWebView.loadUrl(urltoload);
}
((ViewPager) collection).addView(view, 0);
return view;
}
我需要相应地设置可见性的位置,所以我不能在 onCreate 中执行此操作。我知道对于普通适配器,我可以使用 getView,但不能将它用于 PagerAdapter。
我还读到我可以通过处理程序创建自己的 UI 更新线程,但在这种情况下我该怎么做呢?
谢谢。