标签:
人生就像弈棋,一步失误,全盘皆输。
本讲内容:精美的聊天界面
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#d8e0e8" android:orientation="vertical" > <ListView android:id="@+id/msg_list_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider="#000" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/input_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Type somthing here" android:maxLines="2" /> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" /> </LinearLayout> </LinearLayout>
android:divider指定ListView分隔线的颜色,这里设为透明色。
下面是res/layout/msg_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" android:padding="10dp" > <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:background="@drawable/message_left" > <TextView android:id="@+id/left_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@drawable/message_right" > <TextView android:id="@+id/right_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" /> </LinearLayout> </LinearLayout>
下面是Msg.java文件:
//定义消息的实体类 public class Msg { public static final int TYPE_RECEIVED=0;//表示这是一条收到的消息 public static final int TYPE_SENT=1;//表示这是一条发送消息 private String content;//消息的内容 private int type;//消息的类型 public Msg(String content,int type) { this.content=content; this.type=type; } public String getContent() { return content; } public int getType() { return type; } }
下面是MsgAdapter.java文件:
public class MsgAdapter extends ArrayAdapter<Msg>{ private int resource; //调用三个参数的构造方法 public MsgAdapter(Context context, int resource, List<Msg> objects) { super(context, resource, objects); this.resource=resource; } // 在每個子項被滾動到屏幕內的時候會被調用 public View getView(int position, View convertView, ViewGroup parent) { Msg msg=getItem(position); View view; ViewHolder viewHolder ; if(convertView==null){ //根据自定义的R.layout.msg_item布局加载布局 view=LayoutInflater.from(getContext()).inflate(resource, null); viewHolder=new ViewHolder(); //得到各个控件的对象 viewHolder.leftLayout=(LinearLayout) view.findViewById(R.id.left_layout); viewHolder.rightLayout=(LinearLayout) view.findViewById(R.id.right_layout); viewHolder.leftMsg=(TextView) view.findViewById(R.id.left_msg); viewHolder.rightMsg=(TextView) view.findViewById(R.id.right_msg); view.setTag(viewHolder); }else{ view=convertView; viewHolder=(ViewHolder) view.getTag(); } //如果是收到的消息,则显示左边的消息布局,将右边的消息布局隐藏 if(msg.getType()==Msg.TYPE_RECEIVED){ viewHolder.leftLayout.setVisibility(View.VISIBLE); viewHolder.rightLayout.setVisibility(View.GONE); viewHolder.leftMsg.setText(msg.getContent()); }else if(msg.getType()==Msg.TYPE_SENT){ //如果是发出的消息,则显示右边的消息布局,将左边的消息布局隐藏 viewHolder.leftLayout.setVisibility(View.GONE); viewHolder.rightLayout.setVisibility(View.VISIBLE); viewHolder.rightMsg.setText(msg.getContent()); } return view; } //用于對控件 的實例進行緩存,优化我们每次在getView()方法中避免每次都调用View的findViewById()方法来获取控件的实例 class ViewHolder{ LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; } }
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity { private ListView msgListView; private EditText inputText; private Button send; private MsgAdapter adapter; private List<Msg> msgList=new ArrayList<Msg>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initMsgs(); initViews(); } //初始化控件 private void initViews(){ adapter=new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList); inputText=(EditText) findViewById(R.id.input_text); send=(Button) findViewById(R.id.send); msgListView=(ListView) findViewById(R.id.msg_list_view); msgListView.setAdapter(adapter); send.setOnClickListener(new OnClickListener() { public void onClick(View v) { String content=inputText.getText().toString(); if(!"".equals(content)){ Msg msg=new Msg(content,Msg.TYPE_SENT); msgList.add(msg); //当有新消息时,刷新ListView中的显示 adapter.notifyDataSetChanged(); //将ListView定位到最后一行 msgListView.setSelection(msgList.size()); //清空输入框中的内容 inputText.setText(""); } } }); } //初始化消息数据 private void initMsgs(){ Msg msg1=new Msg("Hello dan.",Msg.TYPE_RECEIVED); msgList.add(msg1); Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT); msgList.add(msg2); Msg msg3=new Msg("This is jin.Nice talking to you",Msg.TYPE_RECEIVED); msgList.add(msg3); } }
标签:
原文地址:http://blog.csdn.net/liguojin1230/article/details/44926127