0

PagerAdapter 内の画像ボタンの可視性を変更したいと考えています。id を介して instantiateItem のレイアウトの一部を参照できないようです。完全なレイアウトしか参照できません。これは奇妙ですが、ID を使用して Web ビューを参照できるようです。

どうすればこれにアプローチできますか? これは私がやろうとしていることですが、以下に示す最後の行でヌル ポインター例外が発生しています。

    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

これに対する答えはここにあります: 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 に答える
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 に答える