如何更改 PagerAdapter 中 ImageButton 的可见性?

发布于 2025-01-01 03:22:17 字数 1149 浏览 1 评论 0原文

我想更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒几许 2025-01-08 03:22:17

关于更新线程上的 UI 的问题,请查看 runOnUiThread 这里

Regarding your Q on updating the UI on a thread, check out runOnUiThread here

夜唯美灬不弃 2025-01-08 03:22:17

试试这个,其中“itemLayout”是您的布局:

View thisLayout = inflater.inflate(R.layout.itemLayout, null);

btnLeft = (ImageButton) thisLayout.findViewById(R.id.buttonleft);

<do stuff with btnLeft>

((ViewPager) collection).addView(thisLayout);

return thisLayout;

Try this, where "itemLayout" is your layout :

View thisLayout = inflater.inflate(R.layout.itemLayout, null);

btnLeft = (ImageButton) thisLayout.findViewById(R.id.buttonleft);

<do stuff with btnLeft>

((ViewPager) collection).addView(thisLayout);

return thisLayout;
埖埖迣鎅 2025-01-08 03:22:17

答案就在这里: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizo​​ntal-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文