Skip to content

Commit

Permalink
add chat view
Browse files Browse the repository at this point in the history
  • Loading branch information
robbyzhaox committed Aug 9, 2015
1 parent b7527dd commit 27a01a9
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/src/main/java/com/telchina/wx/Msg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.telchina.wx;

/**
* Created by zg on 2015/8/9.
*/
public class Msg {

public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SEND = 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 void setContent(String content) {
this.content = content;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}
}
66 changes: 66 additions & 0 deletions app/src/main/java/com/telchina/wx/MsgAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.telchina.wx;

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;

/**
* Created by zg on 2015/8/9.
*/
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);
View view;
ViewHolder viewHolder;

if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, 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_SEND) {
viewHolder.rightLayout.setVisibility(View.VISIBLE);
viewHolder.leftLayout.setVisibility(View.GONE);
viewHolder.rightMsg.setText(msg.getContent());
}

return view;
}

private class ViewHolder {

LinearLayout leftLayout;
LinearLayout rightLayout;

TextView leftMsg;
TextView rightMsg;
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/telchina/wx/News.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.telchina.wx;

/**
* Created by zg on 2015/8/9.
*/
public class News {


}
Binary file added app/src/main/res/drawable/message_left.9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/message_right.9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions app/src/main/res/layout/chat_msg_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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_margin="10dp"
android:gravity="center"
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_margin="10dp"
android:gravity="center"
android:textColor="#fff" />

</LinearLayout>

</LinearLayout>
36 changes: 36 additions & 0 deletions app/src/main/res/layout/layout_chat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#D8E0E8"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/msg_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/input_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_hint_text"
android:maxLines="2"/>

<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_btn_text"/>

</LinearLayout>


</LinearLayout>

0 comments on commit 27a01a9

Please sign in to comment.