项目中遇到个问题,点击listview的条目的评论按钮,显示输入法,这个时候listView条目内容会被输入法遮挡,这就需要将listView上移一段距离。一下是解决方案:
1、输入法弹出后必然会导致View重绘,可设置一个重绘完成的监听器
interface OnLayoutChangeAfterListener
{
void OnChangeAfter(int keyboard);//定义方法声明,用以传参
}
监听器的set方法
public void setListener(OnLayoutChangeAfterListener listener)
{
this.mListener = listener;
}
private OnLayoutChangeAfterListener mListener;
2、将类实现View.OnLayoutChangeListener,监听重绘
复写onLayoutChange方法
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
if(oldBottom != 0 && bottom != 0 && ( oldBottom-bottom>100 ))//输入法弹出,计算输入法高度,也即要移动的距离
{
mKeyBoard = oldBottom-bottom;
if(mListener != null)
{
mListener.OnChangeAfter(mKeyBoard);//传参
}
isOpenKeyBoard = true;
mLlComment.setVisibility(View.VISIBLE);
}else if(oldBottom != 0 && bottom != 0 && ( bottom-oldBottom>100 ))
{
isOpenKeyBoard = false;
mLlComment.setVisibility(View.GONE);
}
}
1、计算移动距离
private void caculateScroll(View ll_comment_content)
{
int[] location = new int[2];
ll_comment_content.getLocationOnScreen(location);
final int y = location[1];//计算view在屏幕上的Y轴位置
setListener(new OnLayoutChangeAfterListener()
{
//复写onChangeAfter方法
@Override
public void OnChangeAfter(int keyboard)
{
int distance = y-keyboard-(int)mDensity*40;
// mLvNotice.getRefreshableView().scrollListBy(distance);//scrollListBy()方法至少要求API19,故弃用
scrollVertical(mLvNotice.getRefreshableView(), mContext, distance);
Log.e(TAG, "y:::::"+y+"------"+"keyboard:::::"+keyboard);
}
});
}
将listView移动到指定位置
private void scrollVertical(final ListView listView, Activity act, final int distance)
{
if(listView == null)
{
return;
}
act.runOnUiThread(new Runnable()
{ //执行自动化测试的时候模拟滑动需要进入UI线程操作
@Override
public void run()
{
invokeMethod(listView, "trackMotionScroll", new Object[]{-distance, -distance}, new Class[]{int.class, int.class});
}
});
}
利用反射找到要改写的方法,注意此处方法名trackMotionScroll// 垂直滑动指定距离
private void scrollVertical(final ListView listView, Activity act, final int distance)
{
if(listView == null)
{
return;
}
act.runOnUiThread(new Runnable()
{ //执行自动化测试的时候模拟滑动需要进入UI线程操作
@Override
public void run()
{
invokeMethod(listView, "trackMotionScroll", new Object[]{-distance, -distance}, new Class[]{int.class, int.class});
}
});
}