答案 0 :(得分:1)
编译:
compile 'se.emilsjolander:stickylistheaders:2.1.3'
在适配器中
public class ChatAdapter extends BaseAdapter implements StickyListHeadersAdapter {
private Activity mActivity;
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
private SimpleDateFormat dateformat = new SimpleDateFormat("hh:mm a");
private SimpleDateFormat format1 = new SimpleDateFormat("ddMMyyyy");
private SimpleDateFormat headerdateformat = new SimpleDateFormat("dd MMMM yyyy");
private Date date = null;
private String msgDate = "";
private Date headerDate = null;
private String msgHeader = "";
private String msg = "";
private String mName = "";
private int day = 0;
List<GroupChatHistoryData> mChatArrayList = new ArrayList<>();
public ChatAdapter(Activity mActivity,List<GroupChatHistoryData> mChatArrayList) {
this.mActivity = mActivity;
this.mChatArrayList = mChatArrayList;
}
@Override
public int getCount() {
return mChatArrayList.size();
}
@Override
public Object getItem(int position) {
return mChatArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mActivity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
convertView = inflater.inflate(R.layout.chat_details_row_item, null);
holder = new ViewHolder();
holder.receive_message_text = (TextView) convertView.findViewById(R.id.receive_message_text);
holder.receive_message_date = (TextView) convertView.findViewById(R.id.receive_message_date);
holder.receive_member_name = (TextView) convertView.findViewById(R.id.receive_member_name);
holder.sent_message_date = (TextView) convertView.findViewById(R.id.sent_message_date);
holder.sent_message_text = (TextView) convertView.findViewById(R.id.sent_message_text);
holder.chat_receive_layout = (LinearLayout) convertView.findViewById(R.id.chat_receive_layout);
holder.sent_msg_ll = (LinearLayout) convertView.findViewById(R.id.sent_msg_ll);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final GroupChatHistoryData bean = mChatArrayList.get(position);
try {
if (bean.getTime() != null && !bean.getTime().equalsIgnoreCase("")) {
date = format.parse(bean.getTime());
}
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
msgDate = dateformat.format(date);
}
msg = bean.getMessage();
// your rest views
return convertView;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
final HeaderViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(mActivity);
if (convertView == null) {
convertView = inflater.inflate(R.layout.chat_header, parent, false);
holder = new HeaderViewHolder();
holder.headerDate = (TextView) convertView.findViewById(R.id.header_date);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
GroupChatHistoryData bean = mChatArrayList.get(position);
try {
if (bean.getDatetime() != null && !bean.getDatetime().equalsIgnoreCase("")) {
headerDate = format1.parse(bean.getDatetime());
}
} catch (ParseException e) {
e.printStackTrace();
}
if (headerDate != null) {
msgHeader = headerdateformat.format(headerDate);
}
if (show(msgHeader) == 0) {
holder.headerDate.setText("TODAY");
} else if (show(msgHeader) == -1) {
holder.headerDate.setText("YESTERDAY");
} else {
holder.headerDate.setText(msgHeader);
}
return convertView;
}
private class HeaderViewHolder {
private TextView headerDate;
}
@Override
public long getHeaderId(int position) {
String mDate = mChatArrayList.get(position).getDatetime();
int mDatePos = Integer.parseInt(mDate);
return mDatePos;
}
private class ViewHolder {
TextView receive_message_text;
TextView receive_message_date;
TextView receive_member_name;
TextView sent_message_date;
TextView sent_message_text;
LinearLayout chat_receive_layout;
LinearLayout sent_msg_ll;
}
private int show(String time) {
try {
String outputPattern = "dd MMMM yyyy";
SimpleDateFormat format = new SimpleDateFormat(outputPattern);
Date Date1 = format.parse(getdate());
Date Date2 = format.parse(time);
long mills = Date2.getTime() - Date1.getTime();
long Day1 = mills / (1000 * 60 * 60);
day = (int) Day1 / 24;
} catch (Exception e) {
e.printStackTrace();
}
return day;
}
private String getdate() {
String time = "";
try {
String outputPattern = "dd MMMM yyyy";
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat(outputPattern);
time = df.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return time;
}
}
chat_header.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/header_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#BDD9E4"
android:gravity="center"
android:padding="10dp"
android:textAllCaps="true"
android:textSize="12sp"
android:textColor="@color/gray"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
GroupChatHistoryData Bean是这样的:
public class GroupChatHistoryData {
private SimpleDateFormat headerdateformat = new SimpleDateFormat("ddMMyyyy");
private Date headerDate = null;
private Date mTime = null;
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
private SimpleDateFormat timeformat = new SimpleDateFormat("hh:mm a");
@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("group_id")
@Expose
private String groupId;
@SerializedName("message")
@Expose
private String message;
@SerializedName("datetime")
@Expose
private String datetime;
@SerializedName("fname")
@Expose
private String fname;
@SerializedName("time")
@Expose
private String time;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDatetime() {
String date = "";
try {
if (datetime != null && !datetime.equalsIgnoreCase("")) {
headerDate = format.parse(datetime);
}
} catch (ParseException e) {
e.printStackTrace();
}
if (headerDate != null) {
date = headerdateformat.format(headerDate);
}
return date;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public boolean isMine() {
return isMine;
}
public void setMine(boolean mine) {
isMine = mine;
}
public boolean isMine = false;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
答案 1 :(得分:1)
我认为你应该尝试使用ObservableScrollView。