引出:最近项目中ListView Item中有个回复的按钮,点击后会从屏幕底部弹出回复的输入框+软键盘,同时被点击item的底部与输入框顶部对齐。
////////////////////////////////////////////////////////////////////////////////////////////////////
预览图:
///////////////////////////////////////////////////////////////////////////////////////////////////
高能代码区:
主布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#cccccc"
android:gravity="center"
android:text="Title Bar" />
<com.qiyi.guianxiang.testlistview.CustomListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title" />
<LinearLayout
android:id="@+id/input_layer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#555555"
android:orientation="horizontal">
<EditText
android:id="@+id/comment_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="please input..." />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="send" />
</LinearLayout>
</RelativeLayout>
自定义ListView,监听布局尺寸变化
package com.qiyi.guianxiang.testlistview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* Created by guianxiang on 16-9-26.
*/
public class CustomListView extends ListView {
public void setmListener(OnResizeListener mListener) {
this.mListener = mListener;
}
private OnResizeListener mListener;
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mListener.OnResize(w, h, oldw, oldh);
}
}
adapter中计算变化前后需要滚动的距离
可以使用scrollListBy(>=API 19) 或者 setSelectionFromTop(>=API 21)
package com.qiyi.guianxiang.testlistview;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by guianxiang on 16-9-23.
*/
public class myAdapter extends BaseAdapter {
private Context mContext;
private View commentLayer;
private CustomListView mlistView;
private int[] datas;
private int clickbottom = 0;
private int offset = 0;
private int clickPosition;
private int clickItemHeight;
private int newH = 0;
@Override
public int getCount() {
return datas.length;
}
@Override
public Object getItem(int i) {
return datas[i];
}
@Override
public long getItemId(int i) {
return 0;
}
public myAdapter(Context context, CustomListView listView, int[] datas) {
mContext = context;
mlistView = listView;
this.datas = datas;
initInputBar();
mlistView.setmListener(new OnResizeListener() {
@Override
public void OnResize(int w, int h, int oldw, int oldh) {
Log.e("test", "listOnResize: " + w + "," + h + "," + oldw + "," + oldh);
//键盘弹出
if (oldh > h) {
//(clicked item's bottom to the listview's bottom after keyboard appear) - (clicked item's bottom to the listview's bottom before keyboard appear)
offset = (oldh - h + commentLayer.getHeight()) - (oldh - clickbottom);
newH = h;
mlistView.postDelayed(new Runnable() {
@Override
public void run() {
mlistView.scrollListBy(offset);
//following is another way to realize but require a higher API version
// mlistView.setSelectionFromTop(clickPosition, newH - commentLayer.getHeight() - clickItemHeight);
}
}, 20);
}
}
});
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
final int position = i;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.list_item, viewGroup, false);
holder = new ViewHolder();
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.name = (TextView) view.findViewById(R.id.name);
holder.reply = (TextView) view.findViewById(R.id.reply);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText("android:" + datas[i]);
holder.reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInputBar();
//attention: getChildAt()is to get the visible item in ListView area
clickbottom = mlistView.getChildAt(position - mlistView.getFirstVisiblePosition()).getBottom();
clickItemHeight = mlistView.getChildAt(position - mlistView.getFirstVisiblePosition()).getHeight();
clickPosition = position;
}
});
return view;
}
public static class ViewHolder {
public ImageView icon;
public TextView name;
public TextView reply;
}
private void showInputBar() {
commentLayer.setVisibility(View.VISIBLE);
EditText comment_et = (EditText) commentLayer.findViewById(R.id.comment_et);
keyboardUtils.openSoftKeyboard(comment_et);
}
private void initInputBar() {
commentLayer = ((Activity) (mContext)).findViewById(R.id.input_layer);
commentLayer.setVisibility(View.GONE);
}
}
最后附上github链接:https://github.com/tobevan/Keyboard-align-ListView-item-bottom
存在的问题:最后一条Item会被输入框挡住一部分,目前没有解决
拓展:如果ListView有header,记得在adapter中listview.getchildat(xxx+mlistview.getHeaderViewCounts())