Interface:
1. Main interface Activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#d8e0e8"> <Android.support.v7.widget.RecyclerViewAndroid:layout_width= "Match_parent"Android:layout_height= "0DP"Android:id= "@+id/msg_recycler_view"Android:layout_weight= "1" /> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <EditTextAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:id= "@+id/input_text"Android:layout_weight= "1"Android:hint= "Type something Here"Android:maxlines= "2" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/send"Android:text= "Send" /> </LinearLayout></LinearLayout>
2. Message Interface Msg_item.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical"android:padding= "10DP"> <LinearLayoutandroid:orientation= "vertical"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/left_layout"android:layout_gravity= "Left"Android:background= "@drawable/left9"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/left_msg"android:layout_gravity= "Center"Android:layout_margin= "10DP"Android:textcolor= "#fff" /> </LinearLayout> <LinearLayoutandroid:orientation= "vertical"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/right_layout"android:layout_gravity= "Right"Android:background= "@drawable/right"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/right_msg"android:layout_gravity= "Center"Android:layout_margin= "10DP"Android:textcolor= "#fff" /> </LinearLayout></LinearLayout>
function implementation
1.main Mainactivity.java
PackageCom.example.ken.uibestpractice;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.text.method.LinkMovementMethod;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.ResourceBundle; Public classMainactivityextendsappcompatactivity {PrivateList<msg> msglist =NewArraylist<>(); PrivateEditText Inputtext; PrivateButton Send; PrivateRecyclerview Msgrecyclerview; PrivateMsgadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initmsgs (); Inputtext=(EditText) Findviewbyid (R.id.input_text); Send=(Button) Findviewbyid (r.id.send); Msgrecyclerview=(Recyclerview) Findviewbyid (R.id.msg_recycler_view); Linearlayoutmanager LayoutManager=NewLinearlayoutmanager ( This); Msgrecyclerview.setlayoutmanager (LayoutManager); Adapter=NewMsgadapter (msglist); Msgrecyclerview.setadapter (adapter); Send.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {String content=Inputtext.gettext (). toString (); if(""!=content) {msg msg=NewMsg (content, msg.type_send); Msglist.add (msg); Adapter.notifyiteminserted (Msglist.size ()-1); Msgrecyclerview.scrolltoposition (Msglist.size ()-1); Inputtext.settext (""); } } }); } Private voidinitmsgs () {MSG MSG1=NewMSG ("Hello guy.", msg.type_received); Msglist.add (MSG1); MSG MSG2=NewMSG ("Hello." Who's it? ", Msg.type_send); Msglist.add (MSG2); MSG MSG3=NewMSG ("This is Tom.") Nice to talking. ", msg.type_received); Msglist.add (MSG3); }}
2. Message Class Msg.java
PackageCom.example.ken.uibestpractice; Public classMSG { Public Static Final inttype_received = 0; Public Static Final intType_send = 1; PrivateString content; Private inttype; PublicMSG (String content,inttype) { This. Content =content; This. Type =type; } PublicString getcontent () {returncontent; } Public intGetType () {returntype; }}
3. Message Adapter Msgadapter.java
PackageCom.example.ken.uibestpractice;ImportAndroid.support.annotation.NonNull;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView;Importjava.util.List; Public classMsgadapterextendsRecyclerview.adapter<msgadapter.viewholder> { PrivateList<msg>mmsglist; Static classViewholderextendsrecyclerview.viewholder{linearlayout leftlayout; LinearLayout rightlayout; TextView leftmsg; TextView rightmsg; PublicViewholder (view view) {Super(view); Leftlayout=(LinearLayout) View.findviewbyid (r.id.left_layout); Rightlayout=(LinearLayout) View.findviewbyid (r.id.right_layout); Leftmsg=(TextView) View.findviewbyid (r.id.left_msg); Rightmsg=(TextView) View.findviewbyid (r.id.right_msg); } } PublicMsgadapter (list<msg>msglist) {Mmsglist=msglist; } @NonNull @Override PublicViewholder Oncreateviewholder (@NonNull viewgroup ViewGroup,inti) {view View= Layoutinflater.from (Viewgroup.getcontext ()). Inflate (R.layout.msg_item, ViewGroup,false); return Newviewholder (view); } @Override Public voidOnbindviewholder (@NonNull viewholder Viewholder,inti) {msg msg=Mmsglist.get (i); if(Msg.gettype () = =msg.type_received) {viewHolder.leftlayout.setVisibility (view.visible); ViewHolder.rightlayout.setVisibility (View.gone); ViewHolder.leftMsg.setText (Msg.getcontent ()); } if(Msg.gettype () = =msg.type_send) {viewHolder.leftlayout.setVisibility (View.gone); ViewHolder.rightlayout.setVisibility (view.visible); ViewHolder.rightMsg.setText (Msg.getcontent ()); }} @Override Public intGetItemCount () {returnmmsglist.size (); }}
The effect of the implementation is this:
Android Chat Interface