0

我想更改 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 更新线程,但在这种情况下我该怎么做呢?

谢谢。

4

3 回答 3

1

关于在线程上更新 UI 的 Q,请在此处查看 runOnUiThread

于 2012-01-31T21:04:40.570 回答
0

试试这个,其中“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;
于 2012-01-31T20:55:21.593 回答
0

答案就在这里:http: //mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizo​​ntal -view-paging/comment-page-1/#comment-14065

显然,您必须参考按钮所在的相对或线性布局,否则 findViewById 将无法找到您所指的内容。

于 2012-02-01T04:28:44.720 回答