您的位置:首页 > 移动开发 > Android开发

Android输入法弹出时控制Listview

2016-08-11 18:41 441 查看
问题描述:类似微信聊天界面,输入法或者表情输入框显示时,listview自动显示最新一条消息

解决方法:

1、在AndroidManifest.xml文件的Activity中设置android:windowSoftInputMode=”adjustResize|stateAlwaysHidden”

2、重写了一个LinearLayout布局:

代码如下:

package com.example.listviewtest;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
* Created by Administrator on 2016/8/11.
*/
public class ResizeLayout extends LinearLayout {
private OnResizeListener mListener;

public interface OnResizeListener {
void OnResize();
}

public void setOnResizeListener(OnResizeListener l) {
mListener = l;
}

public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

if (mListener != null) {
mListener.OnResize();
}
}

}


3.xml布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<com.example.listviewtest.ResizeLayout
android:id="@+id/rslayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<ListView android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>
</com.example.listviewtest.ResizeLayout>
<Button
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="more" />
<EditText
android:id="@+id/edt_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>

</LinearLayout>


4.在MainActivity中添加代码:

ResizeLayout rslayout;
rslayout = (ResizeLayout) findViewById(R.id.rslayout);
rslayout.setOnResizeListener(new ResizeLayout.OnResizeListener() {
@Override
public void OnResize() {
listview.postDelayed(new Runnable() {
@Override
public void run() {
listview.setSelection(listview.getCount());
listview.smoothScrollToPosition(listview.getCount());
}
}, 100);
}
});


具体代码请参考附件:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息