我正在创建一个短信应用程序,我正在使用聊天列表外观这里是我的Chatlist适配器类。
chatlistAdapter.java:
public class chatList_AdapterClass extends BaseAdapter {
private Context mContext;
private ArrayList<chatList_row> mArrayList;
ViewHolder mHolder=null;
public chatList_AdapterClass(Context context, ArrayList<chatList_row> arrayList) {
mContext = context;
mArrayList = arrayList;
}
public class ViewHolder{
TextView mReceivedMessage;
LinearLayout.LayoutParams mParams;
ViewHolder(View v){
mReceivedMessage=(TextView)v.findViewById(R.id.ChatListMessages);
}
}
@Override
public int getCount() {
return mArrayList.size();
}
@Override
public Object getItem(int i) {
return mArrayList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent) {
View row=ConvertView;
if (row == null){
LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.chat_list_row,parent,false);
mHolder=new ViewHolder(row);
mHolder.mParams=(LinearLayout.LayoutParams)mHolder.mReceivedMessage.getLayoutParams();
row.setTag(mHolder);
}else {
mHolder=(ViewHolder)row.getTag();
}
mHolder.mReceivedMessage.setText(mArrayList.get(position).getMessageContent());
boolean send=mArrayList.get(position).getTypeofMessage().equals("send");
setChatRowAppearance(send,mHolder);
return row;
}
public void setChatRowAppearance(boolean send,ViewHolder mHolder){
if (send){
mHolder.mParams.gravity= Gravity.END;
mHolder.mReceivedMessage.setTextColor(Color.WHITE);
mHolder.mReceivedMessage.setBackgroundResource(R.drawable.bubble2);
}else {
mHolder.mParams.gravity= Gravity.START;
mHolder.mReceivedMessage.setTextColor(Color.WHITE);
mHolder.mReceivedMessage.setBackgroundResource(R.drawable.bubble1);
}
}
}
发送短信
1
3
收到短信
2
4