I have a Viewpager in a Fragment activity and it has a botton frame with the edit text and send button.
In the fragment layout i have a Listview and am attaching an adapter to it in fragment. Now am implementing the send button from the Parent inside the fragment, on click am trying to add the text in the edit text(again from the parent) to the List(in the fragment) via adapter. But issue here is when i enter some text and click send its adding the text not to the current view in the pager but to next, and when i go to the last item some times it adds to the same view(which is expected). Some times its random not in the order of next item.
Fragment Activity layout:
<?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>
The fragment Layout:
<?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>
Here’s how am implementing the send 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();
}
});
You can access to the current fragment (the one with list) doing something like this:
And I think that if you have your button in Activity then you should implement onClick of this button in your Activity, not in your fragment.