Problems with ViewPager

0

I'm with a problem. I made a ViewPager with 3 screens. Until then everything was fine, the problem came when I wanted to program on one of the screens and I do not understand why I'm throwing an error into something as simple as a button becomes invisible when pressed for example. I tried the same without the ViewPager and it works.

Here I leave the code of what I have, sorry if the question is silly, I learned recently how to use ViewPager and there are still things that I do not understand.

MainActivity (the one that contains the ViewPager):

public class MainActivity extends AppCompatActivity {

private ViewPager viewPager;

private RelativeLayout page1;
private RelativeLayout page2;
private RelativeLayout page3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new MainPageAdapter());
}

class MainPageAdapter extends PagerAdapter
{

    @Override
    public int getCount()
    {
        return 3;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position)
    {
        View page = null;
        switch (position)
        {
            case 0:
                if (page1 == null)
                {
                    page1 = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_pantalla_tienda, null);
                }
                page = page1;
                break;
            case 1:
                if (page2 == null)
                {
                    page2 = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_pantalla_principal, null);
                    viewPager.setCurrentItem(1);
                }
                page = page2;
                break;
            case 2:
                if (page3 == null)
                {
                    page3 = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_pantalla_jardin, null);
                }
                page = page3;
                break;
        }

        ((ViewPager) collection).addView(page, 0);


        return page;
    }

    @Override
    public boolean isViewFromObject(View view, Object object)
    {
        return view == object;
    }

    @Override
    public void destroyItem(View collection, int position, Object view)
    {
        ((ViewPager) collection).removeView((View) view);
    }

}

}

The code on the screen I wanted to work on

public class pantalla_principal extends AppCompatActivity {

Button b3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pantalla_principal);

    b3=(Button)findViewById(R.id.b3);
}

public void invisible(View view){
    b3.setVisibility(View.INVISIBLE);
}

}

    
asked by Zekirak 18.10.2016 в 01:52
source

2 answers

2

The problem is that you try to access the button using findView but Activity . You have to access the button using the current Item of viewPager .

With viewPager.getItem().findViewById should work.

Anyway, I have never used a ViewPager like this, but I have created a fragment on the screen and inside I applied the necessary logic.

Look at the example of this lib that is explained

link

    
answered by 18.10.2016 в 16:33
0

One solution is that the initialization of the button is done within the instantiateItem method so that it refers to the view in which it is found

    
answered by 19.10.2016 в 23:42