-2

I know the LayoutInflater can load View dynamically.Can I jump to other View with LayoutInflater? I have write some code,but it doesn't work.

setContentView(R.layout.activity_viewpager);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LayoutInflater lf = (LayoutInflater) VierPagerTestActivity.this
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                LinearLayout linear = (LinearLayout)lf.inflate(R.layout.view_item, null);
            }
        });
Logic
  • 2,230
  • 2
  • 24
  • 41
CoolEgos
  • 47
  • 1
  • 10

2 Answers2

1

Tricks:

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LayoutInflater lf = (LayoutInflater) VierPagerTestActivity.this
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            LinearLayout linear = (LinearLayout)lf.inflate(R.layout.view_item, null);
            setContentView(linear);
        }
SilentKnight
  • 13,761
  • 19
  • 49
  • 78
0

As I understand, you are trying to get reference to layout. For that you should call

setContentView(R.layout.view_item);
findViewById(R.id.viewId);

Method findViewById gets reference to view inside content view. In this case you should set id @+id/viewId for the view you want to get.

Ircover
  • 2,406
  • 2
  • 22
  • 42