Parent Activity's code getting executed, but t

2019-03-04 01:36发布

问题:

I want to have a sliding menu throughout my application which will be accessible for every activity in my application. For this, I created a parent activity to which all other activities extend as suggested in this answer. So that, only one activity will implement that sliding menu functionality and all other activities extending that activity will have that implementation. But my problem is, when I press UP button (app icon) the sliding drawer should show up and the whole layout should shift to the right side. This works fine for the BaseActivity, but for the activities extending that class, its not working. Neither the drawer is getting shown, nor the layout is shifting towards right. I tried putting toasts in the BaseActivity's appropriate method, the Toast is getting shown but the drawer is not showing up. What can be the problem?

The layout xml is as follows,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_root"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >

<RelativeLayout
    android:id="@+id/ll_lhs_menu"
    android:layout_width="300dip"
    android:layout_height="fill_parent"
    android:background="@color/menu_bg"
    android:orientation="vertical"
    android:visibility="visible" >

    .......
    Sliding drawer goes here.
    .......

</RelativeLayout>


<!-- Following is the actual layout where in the layout for other activities
     will be put in using the java code below. -->
<RelativeLayout
    android:id="@+id/rl_right"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:background="@drawable/capture_port"
    android:scrollbars="none" >

</RelativeLayout>

</RelativeLayout>

And the overridden setContentView() method is as follows:

    @Override
public void setContentView(int layoutResID) {
    _completeLayout= (RelativeLayout) getLayoutInflater().inflate(R.layout.menu, null);
    _activityLayout= (RelativeLayout) _completeLayout.findViewById(R.id.rl_right);
    getLayoutInflater().inflate(layoutResID, _activityLayout, true);
    setContentView(_completeLayout);
}

On click of UP button on action bar, following code is executed in BaseActivity

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
    _leftLayout.setVisibility(View.VISIBLE);
    int width = _leftLayout.getWidth();
    if (width == 0) {
        width = 300;
    }

    if (!isMenuVisible()) {
        Toast.makeText(this, "Executed successfully...", Toast.LENGTH_LONG).show();
        _leftLayout.setVisibility(View.VISIBLE);

        _rightLayoutParams = new LayoutParams(
            _rightLayout.getWidth(), _rightLayout.getHeight());
        _rightLayoutParams.setMargins(width, 0, -width, 0);
        _rightLayout.setLayoutParams(_rightLayoutParams);

        Animation slideRight = setRightSlidingAnimation();
        _leftLayout.startAnimation(slideRight);
        _rightLayout.startAnimation(slideRight);
    } else {
        Animation slideLeft = setLeftSlidingAnimation();
        _rightLayout.startAnimation(slideLeft);
        _leftLayout.startAnimation(slideLeft);
    }

    setMenuVisible(!isMenuVisible());
    break;
    }
    return false;
}

回答1:

Problem solved, just had to initialize the view once again in onOptionsItemSelected since the right layout has changed now. The method now looks like:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:

    _rootLayout = (RelativeLayout) findViewById(R.id.rl_root);
    _leftLayout = (RelativeLayout) findViewById(R.id.ll_lhs_menu);
    _rightLayout = (RelativeLayout) findViewById(R.id.rl_right);

    _leftLayout.setVisibility(View.VISIBLE);
    int width = _leftLayout.getWidth();
    if (width == 0) {
        width = 300;
    }

if (!isMenuVisible()) {
    Toast.makeText(this, "Executed successfully...", Toast.LENGTH_LONG).show();
    _leftLayout.setVisibility(View.VISIBLE);

    _rightLayoutParams = new LayoutParams(
        _rightLayout.getWidth(), _rightLayout.getHeight());
    _rightLayoutParams.setMargins(width, 0, -width, 0);
    _rightLayout.setLayoutParams(_rightLayoutParams);

    Animation slideRight = setRightSlidingAnimation();
    _leftLayout.startAnimation(slideRight);
    _rightLayout.startAnimation(slideRight);
} else {
    Animation slideLeft = setLeftSlidingAnimation();
    _rightLayout.startAnimation(slideLeft);
    _leftLayout.startAnimation(slideLeft);
}

setMenuVisible(!isMenuVisible());
break;
}
return false;
}