如何更改 PagerAdapter 中 ImageButton 的可见性?
我想更改 PagerAdapter 中图像按钮的可见性。我似乎无法通过 id 引用 instantiateItem 中的部分布局,我只能引用完整的布局。但这很奇怪,因为似乎我仍然可以通过 id 引用我的网络视图。
我该如何处理这个问题?这就是我想要做的,但我在最后一行遇到空指针异常,如下所示:
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 更新线程,但在这种情况下我该怎么做?
谢谢。
I want to change visibility of my image buttons within my PagerAdapter. It doesn't seem like I can refer to parts of a layout in instantiateItem through id, I can only refer to the full layout. This is strange though, because it seems that I can still refer to my webviews through id.
How do I approach this? This is what I'm trying to do, but I'm getting a null pointer exception at the last line shown below:
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;
}
I need the position to set visibility accordingly, so I cannot do this in onCreate. I know for a normal adapter, I can use getView, but I cannot use it for PagerAdapter.
I have also read that I can just create my own UI update thread through handlers, but how do I do that in this case?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于更新线程上的 UI 的问题,请查看 runOnUiThread 这里
Regarding your Q on updating the UI on a thread, check out runOnUiThread here
试试这个,其中“itemLayout”是您的布局:
Try this, where "itemLayout" is your layout :
答案就在这里: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/comment-page-1/#comment-14065
显然你必须引用按钮所在的相对或线性布局,否则 findViewById 将无法找到您所引用的内容。
The answer to this is right here: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/comment-page-1/#comment-14065
Apparently you must refer to the Relative or Linear layout that your button resides in or else findViewById will not be able to find what you are referring to.