# ScrollView
一.问题:ScrollView嵌套listView的起始位置不在顶部?
1: mScrollView.scrollTo(0, 0); mScrollView.smoothScrollTo(0,0);无效时,可以用 mScrollView.smoothScrollTo(0,20);设置listview。setFocusable(false);
二:在相对布局下:包含ScrollView、底部EditText布局避免软键盘显示不全?
思想:j就是弹出带有编辑框的PopupWindow
1.xml文件布局先写好,只不过底部不是EditText
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout>
<RelativeLayout
android:id="@+id/layout_comfit"
android:layout_width="match_parent"
android:layout_height="145px"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:paddingLeft="30px"
android:paddingRight="30px" >
<Button
android:id="@+id/btn_comfit"
android:layout_width="90px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_round_blue"
android:text="提交"
android:textColor="@color/white"
android:textSize="30px" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginBottom="50px"
android:layout_marginRight="28px"
android:layout_toLeftOf="@id/btn_comfit"
android:background="@drawable/round_rec"
android:minHeight="60px" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20px"
android:text="回复楼主"
android:textColor="#475257"
android:textSize="30px" />
<TextView
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20px"
android:background="@null"
android:textSize="30px" />
</LinearLayout>
</RelativeLayout>
2:编写带有编辑框的布局,与整体布局底部布局一样,TextView换EdidText
。
3:对点击底部布局添加事件,弹出编辑框的PopupWindow
private void comPopupWindow() {
mRelativeLayout.setVisibility(View.GONE);
View view = (LinearLayout) LayoutInflater.from(
CircleDetailActivity.this).inflate(R.layout.layout_detail_down,
null);
RelativeLayout mTwoLayout = (RelativeLayout) view
.findViewById(R.id.layout_comfit_down);
Button mBtnCif = (Button) view.findViewById(R.id.btn_comfit_down);
final EditText mEdCon = (EditText) view
.findViewById(R.id.et_content_down);
edStr = (EditText) view.findViewById(R.id.et_content_down);
mWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true);
// = new PopupWindow(CircleDetailActivity.this);
mWindow.setBackgroundDrawable(new BitmapDrawable());
mWindow.setFocusable(true);
mWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
//
mWindow.setContentView(view);
mWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
});
// 设置点击窗口外边窗口消失
mWindow.setOutsideTouchable(true);
// 设置弹出窗体需要软键盘
mWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
// 再设置模式,和Activity的一样,覆盖,调整大小。
mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
DisplayMetrics dm = new DisplayMetrics();
mWindow.setWidth(dm.widthPixels);
mWindow.setHeight(LayoutParams.WRAP_CONTENT);
mWindow.update();
popupInputMethodWindow();
// mWindow 的提交按钮监听
mBtnCif.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mEdCon.getText().toString() == null
|| mEdCon.getText().toString().equals("")) {
showToast("请填写内容");
return;
}
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(mEtContent.getWindowToken(), 0);
subCommDate();
}
});
}
这里写代码片
/**
* show soft input
*/
private void popupInputMethodWindow() {
new Thread() {
@Override
public void run() {
super.run();
edStr.setText(mEtContent.getText().toString());
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}.start();
//
}
3:在activity中:
定义全局变量
EditText edStr;
InputMethodManager imm;
private PopupWindow mWindow;
在onCreate中:
imm = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
if (mWindow != null && mWindow.isShowing()) {
mAllView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return imm.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), 0);
}
});
}
SoftKeyBoardListener.setListener(CircleDetailActivity.this,
new OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
// TODO Auto-generated method stub
}
@Override
public void keyBoardHide(int height) {
// TODO Auto-generated method stub
if (mWindow != null) {
mWindow.dismiss();
mRelativeLayout.setVisibility(View.VISIBLE);
mEtContent.setText(edStr.getText().toString());
}
}
});
```4
<div class="se-preview-section-delimiter"></div>
这里写代码片
“`
这里写代码片
/**
* 软键盘 相对布局--- ScrollView---EditText * @ClassName: SoftKeyBoardListener
*
* @Description: TODO
* @date 2016-4-11 上午9:16:27
*
*/
public class SoftKeyBoardListener {
private View rootView;// activity的根视图
int rootViewVisibleHeight;// 纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
// 获取activity的根视图
rootView = activity.getWindow().getDecorView();
// 监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// 获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
// 根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
// 根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener
.keyBoardShow(rootViewVisibleHeight
- visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
// 根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener
.keyBoardHide(visibleHeight
- rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(
OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity,
OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(
activity);
softKeyBoardListener
.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
}