0

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.

aimango
  • 1,567
  • 2
  • 15
  • 29

3 Answers3

1

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

Simon
  • 14,407
  • 8
  • 46
  • 61
0

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;
Simon
  • 14,407
  • 8
  • 46
  • 61
  • In my main layout xml, I have a ViewPager and other layout components. This context view is set in my onCreate for my main Activity. In order to load views into the ViewPager, I need to add them through the PagerAdapter. I've updated the code in my question above to show that Im adding webviews to the viewPager. The buttons are declared in the webview, I just don't know how to refer to them w/in instantiateItem, which btw is a public method declared for PagerAdapters: [link](http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html). Are you saying I should replace it? – aimango Jan 31 '12 at 21:11
  • Thanks for the help, I found out my answer from another source which was very similar to your solution. – aimango Jan 31 '12 at 22:09
  • Cool, glad it helped. Did runOnUiThread solve your UI update issue? – Simon Feb 01 '12 at 09:30
  • I didn't end up looking into it :P – aimango Feb 02 '12 at 19:50
0

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.

aimango
  • 1,567
  • 2
  • 15
  • 29