我开发了一个聊天应用程序。我曾使用片段来显示聊天。当新消息出现时,它将被添加到片段中,它应自动滚动到底部。但那并没有发生。我必须手动滚动才能查看新消息。任何人都可以帮我启用自动向下滚动片段吗?
MessageFragment.java
package com.chaitukadivella.hola;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import com.chaitukadivella.hola.R;
import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MessagesFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG="MessageFragment";
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final DateFormat[] df = new DateFormat[] {
DateFormat.getDateInstance(SimpleDateFormat.MEDIUM, Locale.getDefault()), DateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault())};
private OnFragmentInteractionListener mListener;
private SimpleCursorAdapter adapter;
private Date now;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
now = new Date();
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.chat_list_item,
null,
new String[]{DataProvider.COL_MSG, DataProvider.COL_AT},
new int[]{R.id.text1, R.id.text2},
0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
switch(view.getId()) {
case R.id.text1:
LinearLayout root = (LinearLayout) view.getParent().getParent();
if (cursor.getString(cursor.getColumnIndex(DataProvider.COL_TO)) == null) {
root.setGravity(Gravity.LEFT);
root.setPadding(50, 10, 10, 10);
//root.setBottom(root.getBottom());
} else {
root.setGravity(Gravity.RIGHT);
root.setPadding(10, 10, 50, 10);
//root.setBottom(root.getBottom());
}
break;
case R.id.text2:
TextView tv = (TextView) view;
tv.setText(getDisplayTime(cursor.getString(columnIndex)));
return true;
}
return false;
}
});
setListAdapter(adapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
Bundle args = new Bundle();
args.putString(DataProvider.COL_EMAIL, mListener.getProfileEmail());
getLoaderManager().initLoader(0, args, this);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public String getProfileEmail();
}
private String getDisplayTime(String datetime) {
try {
Log.v(TAG, datetime);
Date dt = sdf.parse(datetime);
long millis = new Date().getTime();
Date dt2 = new Date();
//DateFormat dateFormat2 = android.text.format.DateUtils(getActivity(), 1000, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_SHOW_DATE);
//DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getActivity());
//return dateFormat.format(dt);
if (now.getYear()==dt.getYear() && now.getMonth()==dt.getMonth() && now.getDate()==dt.getDate())
{
//return DateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault()).format(dt);
return dt.toLocaleString();
//return df[1].format(Calendar.getInstance().getTime());
//DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getActivity());
//return dateFormat.format(dt);
}
return DateFormat.getDateInstance(SimpleDateFormat.MEDIUM, Locale.getDefault()).format(dt);
/*
String date = SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM, Locale.getDefault()).format(dt2);
return date;
*/
//return df[0].format(Calendar.getInstance().getTime());
//String time = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault()).format(millis);
//return time;
//Date timeFormat = Calendar.getInstance().getTime();
//return (String)timeFormat;
} catch (ParseException e) {
return datetime;
}
}
public static String formatDateTime (Context context, long millis, int flags)
{
return null;
}
//----------------------------------------------------------------------------
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String profileEmail = args.getString(DataProvider.COL_EMAIL);
CursorLoader loader = new CursorLoader(getActivity(),
DataProvider.CONTENT_URI_MESSAGES,
null,
DataProvider.COL_FROM + " = ? or " + DataProvider.COL_TO + " = ?",
new String[]{profileEmail, profileEmail},
DataProvider.COL_AT + " ASC");
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/darker_gray" >
<fragment
android:name="com.chaitukadivella.hola.MessagesFragment"
android:id="@+id/msg_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<Button
android:id="@+id/send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Send"
android:onClick="onClick" />
<EditText
android:id="@+id/msg_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/send_btn"
android:layout_toLeftOf="@+id/send_btn" >
</EditText>
</RelativeLayout>
</LinearLayout>
chat_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:overScrollMode="always"
android:background="@drawable/box" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>