listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showInputComment(view);
}
});
/**
* 点击相应item,弹出输入框,item滚动到输入框上方
*
* @param itemView
*/
public void showInputComment(View itemView) {
SoftInputUtils.upSoftInput(mContext, etCommentContent);
int coordinateY = getY(itemView);
int height = itemView.getHeight();
//item 底部y坐标
final int itemBottomY = coordinateY + height;
itemView.postDelayed(new Runnable() {
@Override
public void run() {
int y = getY(llCommentInput);
//滑动 RecyclerView,是对应 item 底部和输入框顶部对齐
listView.smoothScrollBy(itemBottomY - y, 1000);
}
}, 300);
}
/**
* 获取控件左上顶点 y 坐标
*
* @param view
* @return
*/
private int getY(View view) {
int[] rect = new int[2];
view.getLocationOnScreen(rect);
return rect[1];//返回纵坐标
}
public class SoftInputUtils {
//弹出软键盘
public static void upSoftInput(Context mContext, EditText et) {
et.setFocusable(true);
et.setEnabled(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
et.findFocus();
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_FORCED);
}
//收起软键盘
public static void hideSoftInput(Context mContext, EditText et) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0); //强制隐藏键盘
}
}