我在 Fragment 活动中有一个 Viewpager,它有一个带有编辑文本和发送按钮的底部框架。在片段布局中,我有一个 Listview 并在片段中附加了一个适配器。现在我正在从片段内的父级实现发送按钮,单击时我试图通过适配器将编辑文本中的文本(再次来自父级)添加到列表(在片段中)。但是这里的问题是当我输入一些文本并单击发送时,将文本添加到寻呼机中的当前视图而不是下一个,当我转到最后一个项目时,它有时会添加到同一个视图(这是预期的)。有时它是随机的,而不是下一个项目的顺序。片段活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/form" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="@+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="2dp" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btnSend"
android:drawableRight="@drawable/edit_keyboard"
android:inputType="textMultiLine"
android:maxLines="3" />
<ImageButton
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/send" />
</RelativeLayout>
</RelativeLayout>
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/transparent"
android:listSelector="@android:color/transparent"
android:scrollbars="none" />
</RelativeLayout>
以下是实现发送 ImageButton 的方法:
ImageButton sendBtn = (ImageButton) getActivity().findViewById(R.id.btnSend);
sendBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String msg = msgBox.getText().toString();
if (msg.length() > 0) {
long date = new Date().getTime();
// TODO Auto-generated method stub
commentAdapter.add(new OneComment(false, msg, "Me",
String.valueOf(date), "0"));
msgBox.setText("");
} else
Toast.makeText(getActivity(), "type in some text..", Toast.LENGTH_SHORT).show();
}
});