Android - 收听BaseActivity变量值更改

时间:2016-02-22 19:50:50

标签: java android listener

我的代码有BaseActivity,从Activity延伸。它有一个可扩展的菜单。 其他活动在"内部打开" BaseActivity的框架。我需要检查用户何时展开或折叠菜单,以便在BaseActivity中打开的活动中调整列的大小。

ExpandObserver.java

public class ExpandObserver {

    public interface IExpandListener {
         void resizeIfExpanded(boolean isExpanded);
    }

    private IExpandListener listener = null;

    public void registerListener(IExpandListener listener) {
        this.listener = listener;
    }

    public void listenExpandChange(boolean isExpanded) {
        if (this.listener != null) listener.resizeIfExpanded(isExpanded);
    }
}

BaseActivity.java (相关代码)

public class BaseActivity extends Activity implements ExpandObserver.IExpandListener {

    private ExpandObserver expandObserver = null;

    //onCreate
    expandObserver = new ExpandObserver();
    expandObserver.registerListener(this);
    //... 

    //Inside expand method
    // expand is a BaseActivity private variable used to control if is collapsed or not.
    expandObserver.listenExpandChange(expanded);
}

@Override
public void resizeIfExpanded(boolean isExpanded) {

}

MyListActivity.java

public class MyListActivity extends BaseActivity {

    @Override
    public void resizeIfExpanded(boolean isExpanded) {
        //HERE I NEED TO CHANGE COLUMN SIZE 
    }
}

MyListActivityAdapter.java (我需要调整自定义ListView项目)

public class MyListActivityAdapter extends BaseAdapter {

    @Override
    public void resizeIfExpanded(boolean isExpanded) {
        //HERE I NEED TO CHANGE COLUMN SIZE 
    }
}

关于如何完成任何想法?我尝试了几个代码,但仍然没有工作。 提前谢谢。

0 个答案:

没有答案