package com.bigfat.uibestpractice; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; /** * @author <a href="mailto:fbzhh007@gmail.com">bigfat</a> * @since 2014/12/6 */ public class MsgAdapter extends ArrayAdapter<Msg> { private int resourceId; public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { Msg msg = getItem(position); ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(getContext()).inflate(resourceId, null); holder.leftLayout = (LinearLayout) convertView.findViewById(R.id.left_layout); holder.rightLayout = (LinearLayout) convertView.findViewById(R.id.right_layout); holder.leftMsg = (TextView) convertView.findViewById(R.id.left_msg); holder.rightMsg = (TextView) convertView.findViewById(R.id.right_msg); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } //判断显示消息类别 if (msg.getType() == Msg.TYPE_RECEIVED) { holder.leftLayout.setVisibility(View.VISIBLE); holder.rightLayout.setVisibility(View.GONE); holder.leftMsg.setText(msg.getContent()); } else { holder.leftLayout.setVisibility(View.GONE); holder.rightLayout.setVisibility(View.VISIBLE); holder.rightMsg.setText(msg.getContent()); } return convertView; } private final class ViewHolder { LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; } }