Displaying multiple xml views w/o fragments

2019-08-01 15:00发布

问题:

In my app the user spins a carousel. On the selected Item I need to display a separate xml view for each item. The carousel is visibile at all time at the bottom with the view visible in the top half. I think that there is a better way than to use fragments. Any ideas? The following code is not functional as of now but I think an inflator may be the way to go but to get it done is troubling me. After case0 there is case1-case5.

 carousel.setOnItemSelectedListener(new OnItemSelectedListener(){

        public void onItemSelected(CarouselAdapter<?> parent, View view,
                int position, long id) {



            switch(position){
            case 0:
                final View firstview;
                LayoutInflater inflater = (LayoutInflater)   getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                firstview = inflater.inflate(R.layout.selected_item, null);
                break;
            case 1:

回答1:

In case anyone else runs into this problem I solved it:

    LayoutInflater inflater = (LayoutInflater) CarouselActivity.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            LinearLayout inflateView = (LinearLayout) findViewById( R.id.InflateParent );
            View nextView = null;

            switch(position){
            case 0:
                System.out.println("Case 0");
                inflateView.removeAllViews();
                nextView = inflater.inflate( R.layout.nextview, null );
                inflateView.addView(nextView);