布局监听
public class MyMeasureLayout extends RelativeLayout {
public static final String TAG = "MyMeasureLayout";
public static final boolean DEBUG = true;
private FragmentSizeObserver mObserver;
private int initBottom = -1;
private int initLeft = -1;
public MyMeasureLayout(Context context){
this(context, null);
}
public MyMeasureLayout(Context context, AttributeSet attrs){
this(context, attrs, 0);
}
public MyMeasureLayout(Context context, AttributeSet attrs,
int defStyleAttr){
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
LogUtils.i( " changed "+changed +" b "+b);
if(initBottom == -1 || initLeft == -1){
initBottom = b;
initLeft = l;
return;
}
if(changed){
LogUtils.i(" height "+b +" width "+r
+" initBottom "+initBottom+" initLeft "+initLeft);
int height = b - initBottom; //高度变化值(弹出输入法,布局变小,则为负值)
int width = r - initLeft; // 当前屏幕宽度(对应输入法而言无影响)
onInputSizeChanged(width, height);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
private void onInputSizeChanged(int widthDiff,int heightDiff) {
if(mObserver != null){
mObserver.onFragmentSizeChanged(widthDiff, heightDiff);
}
}
public void registFragmentSizeObserver(FragmentSizeObserver observer) {
mObserver = observer;
}
public interface FragmentSizeObserver{
public void onFragmentSizeChanged(int wdiff,int hdiff);
}
}
activity 实现
rl.registFragmentSizeObserver(this);
//实现
public void onFragmentSizeChanged(int wdiff, int hdiff) {
LogUtils.i("onFragmentSizeChanged wdiff " + wdiff + " hdiff "
+ hdiff);
// doFragmentSizeChanged(wdiff, hdiff);
if(hdiff < -100){ // show soft input
// 处理输入法弹出事件
}
else if(hdiff == 0){ // hide soft input
// 处理输入法隐藏事件
if(null != rl){
LogUtils.i("--keybord hide soft input--add messageRootListener ");
}
}
}