我正在研究android中的聊天应用程序。我在我的应用程序中使用this代码来实现“拉动刷新”功能。我的问题是当我点击Editext写入消息时,软键盘出现,如果pulltoRefresh List有1或2个元素,列表会向上滚动到顶部,但如果pulltoRefresh列表有足够的项目覆盖整个屏幕,那么列表正常工作(它不会向上滚动到顶部。)
以下是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout android:id="@+id/buttonsFrame" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="6dp" android:background="@drawable/routecover_bg">
<LinearLayout android:id="@+id/messages_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical|center" android:padding="2dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_headermessageselect" android:id="@+id/msg_icon"/>
</LinearLayout>
<LinearLayout android:id="@+id/locations_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical|center" android:padding="2dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_headerlocation" android:id="@+id/location_icon"/>
</LinearLayout>
<LinearLayout android:id="@+id/members_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical|center" android:padding="2dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_headermember" android:id="@+id/member_icon"/>
</LinearLayout>
</LinearLayout>
<ChatWindow android:id="@+id/messages_window"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"/>
聊天窗口的代码:
public class ChatWindow extends FrameLayout{
private PullToRefreshListView listView;
public ChatWindow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(context);
}
public ChatWindow(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ChatWindow(Context context) {
super(context);
init(context);
}
@Override
public void init(Context context){
setupview(context);
initialiseList(context);
}
@Override
public void setupview(Context context) {
LayoutInflater mInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = mInflator.inflate(R.layout.chat_window, null);
listView = (PullToRefreshListView)contentView.findViewById(R.id.chatlist);
listView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
@Override
public void onRefresh(){
}
});
this.addView(contentView);
}
}
chat_window布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical">
<PullToRefreshListView
android:id="@+id/chatlist"
android:dividerHeight="2dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:smoothScrollbar="true"
android:cacheColorHint="#00000000"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
android:scrollbars="none"
android:divider="@color/white"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentcontainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="@color/bg_menubar">
<EditText android:id="@+id/name" android:hint="Compose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:maxLines="3"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send_button"
android:background="@drawable/bg_sendbutton"
android:layout_marginRight="8dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>