Access layout elements loaded with pager view

1

I have an activity with three layout.

 activity_welcome.xml
 activity_detalle_refaccion.xml
 visor_de_imagenes.xml

What I charge with this code in a ViewPager :

public class DetalleRefaccion extends UtilidadesActividades {

    private Bitmap loadedImage;


    private int[] layouts;
    ViewPager viewPager;
    MyViewPagerAdapter myViewPagerAdapter;

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

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        layouts = new int[]{

                R.layout.activity_detalle_refaccion,
                R.layout.visor_de_imagenes
        };

        myViewPagerAdapter = new MyViewPagerAdapter();
        viewPager.setAdapter(myViewPagerAdapter);

    }

  class MyViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;

        public MyViewPagerAdapter() {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(layouts[position], container, false);
            container.addView(view);

            return view;
        }

        @Override
        public int getCount() {
            return layouts.length;
        }

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


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
        }
    }

This works correctly. The two layouts are displayed within the activity_welcome.xml panel. But I try to access the @id/ of the layout activity_detalle_refaccion with a

TextView codigoInterno = (TextView) findViewById(R.id.codigoInterno);

but I get null and my application closes.

If I modify setContentView(R.layout.activity_welcome); by the layout activity_detalle_refaccion.xml I easily get their ids. So how do you access this TextView in the layout that is not in setContentView ?

    
asked by Angel 09.11.2017 в 21:16
source

1 answer

1

You can not access the elements of a view that is in ViewPager . To date the class ViewPager does not expose any overload where this is possible and I do not believe that it exposes it due to the collision of elements that have the same id.

If you really need that, one solution that worked for me was to expose the view of the element that I need to access. For example in your case you just have to do this:

class MyViewPagerAdapter extends PagerAdapter {
    private LayoutInflater layoutInflater;

    public View refaccionView;

    public MyViewPagerAdapter() {
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = layoutInflater.inflate(layouts[position], container, false);
        container.addView(view);


        // si es el layout refaccion, entonces le indicamos la referencia al campo
        if(layouts[position] == R.layout.activity_detalle_refaccion)
        {
            refaccionView = view;
        }


        return view;
    }

    //...

Then when you go to use it, you only have to access the property refraccionView and look for your element:

  //...

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

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        layouts = new int[]{

                R.layout.activity_detalle_refaccion,
                R.layout.visor_de_imagenes
        };

        myViewPagerAdapter = new MyViewPagerAdapter();
        viewPager.setAdapter(myViewPagerAdapter);

        TextView codigoInterno = (TextView)myViewPagerAdapter.refaccionView.findViewById(R.id.codigoInterno);


    }

    //...

Remember that you must first initialize the ViewPager to then access the refraction view.

    
answered by 09.11.2017 / 21:38
source