当前位置:   article > 正文

Android Studio 制作微信界面 下_androidstudiolistview仿微信消息界面

androidstudiolistview仿微信消息界面

主界面

     上一篇文章的链接:

Android Studio 制作微信界面 上_nazonomaster的博客-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/nazonomaster/article/details/124456716

        接下来创建fragment的Java文件

                首先是WeixinFragment.java

                在包内创建一个名为fragment的文件夹,在该文件夹中创建新的Java类并命名为WeixinFragment

  1. public class WeixinFragment extends Fragment {
  2. private List<Weixin> weiList = new ArrayList<>();
  3. @Nullable
  4. @Override
  5. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  6. @Nullable Bundle savedInstanceState) {
  7. return inflater.inflate(R.layout.weixin_fragment,container,false);
  8. }
  9. @Override
  10. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  11. super.onActivityCreated(savedInstanceState);
  12. inints();
  13. WeixinAdapter adapter = new WeixinAdapter(this.getContext(),R.layout.weixin_content_item,
  14. weiList);
  15. ListView listView = getView().findViewById(R.id.weixin_listview);
  16. listView.setAdapter(adapter);
  17. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  18. @Override
  19. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  20. Weixin weixin = weiList.get(position);
  21. if (weixin.getName()=="笨笨"){
  22. Intent intent = new Intent(getActivity(), ChatActivity.class);
  23. startActivity(intent);
  24. }else {
  25. Toast.makeText(getActivity(),"不是通讯录里的联系人( ´_ゝ`)", Toast.LENGTH_SHORT).show();
  26. }
  27. }
  28. });
  29. }
  30. private void inints() {
  31. for (int i=0; i<2;i++){
  32. Weixin w1 = new Weixin();
  33. w1.setImageId(R.mipmap.dingyuehao);
  34. w1.setName("订阅号服务");
  35. w1.setNeirong("重庆科技学院:今晚,重科人用歌声献礼建党100周年!");
  36. weiList.add(w1);
  37. Weixin w2 = new Weixin();
  38. w2.setImageId(R.mipmap.yundong);
  39. w2.setName("运动健康");
  40. w2.setNeirong("[应用消息]");
  41. weiList.add(w2);
  42. Weixin w3 = new Weixin();
  43. w3.setImageId(R.mipmap.weixinzhifu);
  44. w3.setName("微信支付");
  45. w3.setNeirong("微信支付凭证");
  46. weiList.add(w3);
  47. Weixin w4 = new Weixin();
  48. w4.setImageId(R.mipmap.jizhangben);
  49. w4.setName("微信记账本");
  50. w4.setNeirong("昨日记账日报,点击查看详情");
  51. weiList.add(w4);
  52. Weixin w5 = new Weixin();
  53. w5.setImageId(R.mipmap.youxiang);
  54. w5.setName("QQ邮箱提醒");
  55. w5.setNeirong("Nintendo (Hongkong) Limited:最后召集! ...");
  56. weiList.add(w5);
  57. Weixin w6 = new Weixin();
  58. w6.setImageId(R.mipmap.haer);
  59. w6.setName("笨笨");
  60. w6.setNeirong("long may the sunshine!");
  61. weiList.add(w6);
  62. }
  63. }
  64. }

                第二个是TongxunluFragment.java

                在fragment文件夹中创建新的Java类并命名为TongxunluFragment

  1. public class TongxunluFragment extends Fragment {
  2. private List<Tongxunlu> tongxunluList = new ArrayList<>();
  3. @Nullable
  4. @Override
  5. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  6. @Nullable Bundle savedInstanceState) {
  7. return inflater.inflate(R.layout.tongxunlu_fragment,container,false);
  8. }
  9. @Override
  10. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  11. super.onActivityCreated(savedInstanceState);
  12. inints();
  13. TongxunluAdapter adapter = new TongxunluAdapter(this.getContext(),R.layout.txl_content_item,
  14. tongxunluList);
  15. ListView listView = getView().findViewById(R.id.txl_listview);
  16. listView.setAdapter(adapter);
  17. }
  18. private void inints() {
  19. Tongxunlu t1 = new Tongxunlu();
  20. t1.setImageId(R.mipmap.newf);
  21. t1.setName("新的朋友");
  22. tongxunluList.add(t1);
  23. Tongxunlu t2 = new Tongxunlu();
  24. t2.setImageId(R.mipmap.qunchat);
  25. t2.setName("群聊");
  26. tongxunluList.add(t2);
  27. Tongxunlu t3 = new Tongxunlu();
  28. t3.setImageId(R.mipmap.biaoqian);
  29. t3.setName("标签");
  30. tongxunluList.add(t3);
  31. Tongxunlu t4 = new Tongxunlu();
  32. t4.setImageId(R.mipmap.gongzhonghao);
  33. t4.setName("公众号");
  34. tongxunluList.add(t4);
  35. for (int i=0;i<2;i++){
  36. Tongxunlu t5 = new Tongxunlu();
  37. t5.setImageId(R.mipmap.t_tx01);
  38. t5.setName("笨笨");
  39. tongxunluList.add(t5);
  40. Tongxunlu t6 = new Tongxunlu();
  41. t6.setImageId(R.mipmap.t_tx02);
  42. t6.setName(" d=( ゚∀。)-好耶");
  43. tongxunluList.add(t6);
  44. Tongxunlu t7 = new Tongxunlu();
  45. t7.setImageId(R.mipmap.t_tx03);
  46. t7.setName("Hollow Knight");
  47. tongxunluList.add(t7);
  48. Tongxunlu t8 = new Tongxunlu();
  49. t8.setImageId(R.mipmap.t_tx05);
  50. t8.setName("Mr_Quin");
  51. tongxunluList.add(t8);
  52. Tongxunlu t9 = new Tongxunlu();
  53. t9.setImageId(R.mipmap.t_tx04);
  54. t9.setName("是acc啦");
  55. tongxunluList.add(t9);
  56. Tongxunlu t10 = new Tongxunlu();
  57. t10.setImageId(R.mipmap.tx_kenny);
  58. t10.setName("机智的肯尼");
  59. tongxunluList.add(t10);
  60. }
  61. }
  62. }

                第三个是FaxianFragment.java

                在fragment文件夹中创建新的Java类并命名为FaxianFragment

  1. public class FaxianFragment extends Fragment {
  2. private LinearLayout linearLayout;
  3. @Nullable
  4. @Override
  5. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  6. @Nullable Bundle savedInstanceState) {
  7. View view = inflater.inflate(R.layout.faxian_fragment, container, false);
  8. RelativeLayout relativeLayout = view.findViewById(R.id.pyquan_click);
  9. relativeLayout.setOnClickListener(new View.OnClickListener() {
  10. @Override
  11. public void onClick(View v) {
  12. Intent intent = new Intent(getActivity(),PyquanActivity.class);
  13. startActivity(intent);
  14. }
  15. });;
  16. return view;
  17. }
  18. }

                最后是WodeFragemnt.java

                在fragment文件夹中创建新的Java类并命名为WodeFragemnt 

  1. public class WodeFragemnt extends Fragment {
  2. @Nullable
  3. @Override
  4. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  5. @Nullable Bundle savedInstanceState) {
  6. return inflater.inflate(R.layout.wode_fragment,container,false);
  7. }
  8. }

        菜单按钮布局

                menu_weixin_icon_selector.xml

        首先是第一个按钮的布局文件,在main\res\drawable中创建一个新的布局文件并命名为menu_weixin_icon_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@mipmap/faxian02" android:state_pressed="true"/>
  4. <item android:drawable="@mipmap/faxian02" android:state_selected="true"/>
  5. <item android:drawable="@mipmap/faxian01"/>
  6. </selector>

                menu_tongxunlu_icon_selector.xml

        第二个按钮的布局文件,在main\res\drawable中创建一个新的布局文件并命名为menu_tongxunlu_icon_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@mipmap/tongxunlu02" android:state_pressed="true"/>
  4. <item android:drawable="@mipmap/tongxunlu02" android:state_selected="true"/>
  5. <item android:drawable="@mipmap/tongxunlu01"/>
  6. </selector>

                menu_faxian_icon_selector.xml

        第三个按钮的布局文件,在main\res\drawable中创建一个新的布局文件并命名为menu_faxian_icon_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@mipmap/faxian02" android:state_pressed="true"/>
  4. <item android:drawable="@mipmap/faxian02" android:state_selected="true"/>
  5. <item android:drawable="@mipmap/faxian01"/>
  6. </selector>

                menu_wode_icon_selector.xml

        第四个按钮的布局文件,在main\res\drawable中创建一个新的布局文件并命名为menu_wode_icon_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@mipmap/wode02" android:state_pressed="true"/>
  4. <item android:drawable="@mipmap/wode02" android:state_selected="true"/>
  5. <item android:drawable="@mipmap/wode01"/>
  6. </selector>

首页界面

        布局文件weixin_content_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:ignore="MissingDefaultResource">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:orientation="horizontal"
  13. android:gravity="center"
  14. android:layout_marginTop="5dp">
  15. <ImageView
  16. android:id="@+id/weixin_content_img"
  17. android:layout_width="48dp"
  18. android:layout_height="48dp"
  19. android:src="@mipmap/dingyuehao"
  20. android:layout_marginLeft="10dp"/>
  21. <LinearLayout
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_gravity="center"
  25. android:layout_marginLeft="10dp"
  26. android:orientation="vertical">
  27. <TextView
  28. android:id="@+id/weixin_content_txt1"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="订阅号消息"
  32. android:textSize="24sp"
  33. android:textColor="#000000"
  34. />
  35. <TextView
  36. android:id="@+id/weixin_content_txt2"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="重庆科技学院:今晚,重科人用歌声献..."
  40. android:textSize="15sp"
  41. />
  42. <View
  43. android:layout_width="match_parent"
  44. android:layout_height="2dp"
  45. android:background="#d8dde1"
  46. android:layout_marginTop="10dp"
  47. android:layout_marginRight="10dp"/>
  48. </LinearLayout>
  49. </LinearLayout>
  50. </LinearLayout>

        Weixin.java

        在包内创建一个新的文件夹entity,在该文件夹中创建新的Java类,命名为Weixin

  1. public class Weixin {
  2. private String name;
  3. private String neirong;
  4. private int imageId;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String getNeirong() {
  12. return neirong;
  13. }
  14. public void setNeirong(String neirong) {
  15. this.neirong = neirong;
  16. }
  17. public int getImageId() {
  18. return imageId;
  19. }
  20. public void setImageId(int imageId) {
  21. this.imageId = imageId;
  22. }
  23. }

         WeixinAdapter.java

         在包内创建一个新的文件夹adapter,在该文件夹中创建新的Java类,命名为WeixinAdapter

  1. public class WeixinAdapter extends ArrayAdapter<Weixin> {
  2. private int resourceId;
  3. private static final String TAG = "WeixinAdapter";
  4. private List<Weixin> list;
  5. public WeixinAdapter(@NonNull Context context, int resource, @NonNull List<Weixin> objects) {
  6. super(context, resource, objects);
  7. resourceId = resource;
  8. }
  9. @NonNull
  10. @Override
  11. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
  12. {
  13. Weixin weixin = getItem(position);
  14. View view = LayoutInflater.from(getContext()).inflate(resourceId,
  15. parent, false);
  16. ImageView fruitImage = view.findViewById(R.id.weixin_content_img);
  17. TextView fruitName = view.findViewById(R.id.weixin_content_txt1);
  18. TextView fruitNr = view.findViewById(R.id.weixin_content_txt2);
  19. fruitImage.setImageResource(weixin.getImageId());
  20. fruitName.setText(weixin.getName());
  21. fruitNr.setText(weixin.getNeirong());
  22. return view;
  23. }
  24. }

        这样首页部分就完成了。 

通讯录界面

        布局文件txl_content_item.xml

        在layout文件夹中创建一个名为txl_content_item的布局文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:orientation="horizontal">
  9. <ImageView
  10. android:id="@+id/t_tx"
  11. android:layout_width="40dp"
  12. android:layout_height="40dp"
  13. android:background="@mipmap/newf"
  14. android:layout_marginTop="5dp"
  15. android:layout_marginLeft="10dp"/>
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:orientation="vertical"
  20. android:layout_marginLeft="10dp">
  21. <TextView
  22. android:id="@+id/t_name"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="新的朋友"
  26. android:textSize="19dp"
  27. android:textColor="#000000"
  28. android:layout_marginTop="10dp"/>
  29. <View
  30. android:layout_width="match_parent"
  31. android:layout_height="1dp"
  32. android:layout_marginTop="20dp"
  33. android:background="#e0e0e0"/>
  34. </LinearLayout>
  35. </LinearLayout>
  36. </LinearLayout>

        Tongxunlu.java

        在entity文件夹中创建一个名为Tongxunlu的Java类文件

  1. public class Tongxunlu {
  2. private String name;
  3. private int imageId;
  4. public String getName(){
  5. return name;
  6. }
  7. public void setName(String name){
  8. this.name = name;
  9. }
  10. public int getImageId(){
  11. return imageId;
  12. }
  13. public void setImageId(int imageId) {
  14. this.imageId = imageId;
  15. }
  16. }

        TongxunluAdapter.java

        在adapter文件夹中创建一个名为TongxunluAdapter的Java类文件

  1. public class TongxunluAdapter extends ArrayAdapter<Tongxunlu> {
  2. private int resourceId;
  3. private static final String TAG = "TongxunluAdapter";
  4. private List<Tongxunlu> list;
  5. public TongxunluAdapter(@NonNull Context context, int resource, @NonNull List<Tongxunlu> objects) {
  6. super(context, resource, objects);
  7. resourceId = resource;
  8. }
  9. @NonNull
  10. @Override
  11. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
  12. {
  13. Tongxunlu tongxunlu = getItem(position);
  14. View view = LayoutInflater.from(getContext()).inflate(resourceId,
  15. parent, false);
  16. ImageView t_tx = view.findViewById(R.id.t_tx);
  17. TextView t_name = view.findViewById(R.id.t_name);
  18. t_tx.setImageResource(tongxunlu.getImageId());
  19. t_name.setText(tongxunlu.getName());
  20. return view;
  21. }
  22. }

聊天功能的实现

        聊天界面布局activity_chat.xml

        在layout文件夹中创建一个名为activity_chat的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:background="#d8e0e8"
  9. tools:context=".ChatActivity">
  10. <RelativeLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="70dp"
  13. android:background="#f2f2f2">
  14. <Button
  15. android:id="@+id/weixin_back"
  16. android:layout_width="40dp"
  17. android:layout_height="40dp"
  18. android:layout_alignParentLeft="true"
  19. android:layout_marginLeft="10dp"
  20. android:layout_marginTop="12dp"
  21. android:background="@mipmap/back" />
  22. <TextView
  23. android:id="@+id/chat_name"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="笨笨"
  27. android:textSize="25dp"
  28. android:textColor="#000000"
  29. android:layout_centerHorizontal="true"
  30. android:layout_marginTop="15dp"/>
  31. <Button
  32. android:layout_width="40dp"
  33. android:layout_height="40dp"
  34. android:background="@mipmap/ddd"
  35. android:layout_alignParentRight="true"
  36. android:layout_marginTop="12dp"
  37. android:layout_marginRight="10dp"/>
  38. </RelativeLayout>
  39. <androidx.recyclerview.widget.RecyclerView
  40. android:id="@+id/msg_recycler_view"
  41. android:layout_width="match_parent"
  42. android:layout_height="0dp"
  43. android:layout_weight="1"/>
  44. <RelativeLayout
  45. android:layout_width="match_parent"
  46. android:layout_height="60dp"
  47. android:orientation="horizontal"
  48. android:background="#f2f2f2">
  49. <Button
  50. android:id="@+id/yuyin"
  51. android:layout_width="40dp"
  52. android:layout_height="40dp"
  53. android:background="@mipmap/yuyin"
  54. android:layout_marginTop="10dp"
  55. android:layout_marginLeft="10dp"/>
  56. <EditText
  57. android:id="@+id/input_txt"
  58. android:layout_width="0dp"
  59. android:layout_height="42dp"
  60. android:layout_marginLeft="20dp"
  61. android:layout_marginTop="10dp"
  62. android:layout_toRightOf="@+id/yuyin"
  63. android:layout_toLeftOf="@+id/send_emoji"
  64. android:layout_marginRight="10dp"
  65. android:background="#ffffff" />
  66. <Button
  67. android:id="@+id/send_emoji"
  68. android:layout_width="40dp"
  69. android:layout_height="40dp"
  70. android:background="@mipmap/fabiaoqing"
  71. android:layout_marginTop="10dp"
  72. android:layout_marginRight="5dp"
  73. android:layout_toLeftOf="@+id/btn_send"/>
  74. <Button
  75. android:id="@+id/btn_send"
  76. android:layout_width="60dp"
  77. android:layout_height="40dp"
  78. android:text="发送"
  79. android:textColor="#ffffff"
  80. android:textSize="20sp"
  81. android:background="#32dc60"
  82. android:layout_alignParentRight="true"
  83. android:layout_marginTop="10dp"
  84. android:layout_marginRight="10dp"/>
  85. </RelativeLayout>
  86. </LinearLayout>

        聊天框的布局msg_item.xml

        在layout文件夹中创建一个名为msg_item的布局文件

        代码中涉及到聊天框的图片和路径,图片文件保存在res/mipmap_hdpi中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:padding="10dp">
  8. <LinearLayout
  9. android:id="@+id/left_layout"
  10. android:layout_gravity="left"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:orientation="horizontal"
  14. android:background="@mipmap/message_left">
  15. <TextView
  16. android:id="@+id/left_msg"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_gravity="center"
  20. android:layout_margin="10dp"
  21. android:textColor="#fff"
  22. android:text="你好,小明"/>
  23. </LinearLayout>
  24. <LinearLayout
  25. android:id="@+id/right_layout"
  26. android:orientation="horizontal"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:background="@mipmap/message_right"
  30. android:layout_gravity="right">
  31. <TextView
  32. android:id="@+id/right_msg"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_gravity="center"
  36. android:layout_margin="10dp"
  37. android:text="口口口口"
  38. />
  39. </LinearLayout>
  40. </LinearLayout>

        Msg.java

        在entity文件夹中创建一个名为Msg的Java类文件

  1. public class Msg {
  2. public static final int TYPE_RECEIVED = 0;
  3. public static final int TYPE_SENT = 1;
  4. // 消息内容
  5. private String content;
  6. // 消息类型
  7. private int type;
  8. public Msg(String content,int type){
  9. this.content = content;
  10. this.type = type;
  11. }
  12. public int getType(){
  13. return type;
  14. }
  15. public String getContent(){
  16. return content;
  17. }
  18. public void setContent(String content){
  19. this.content = content;
  20. }
  21. public void setType(int type){
  22. this.type = type;
  23. }
  24. }

        MsgAdapter.java

        在adapter文件夹中创建一个名为MsgAdapter的Java类文件

  1. public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
  2. private List<Msg> mMsgList;
  3. static class ViewHolder extends RecyclerView.ViewHolder{
  4. LinearLayout leftLayout;
  5. LinearLayout rightLayout;
  6. TextView leftMsg;
  7. TextView rightMsg;
  8. public ViewHolder(View view){
  9. super(view);
  10. leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
  11. rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
  12. leftMsg = (TextView) view.findViewById(R.id.left_msg);
  13. rightMsg = (TextView) view.findViewById(R.id.right_msg);
  14. }
  15. }
  16. public MsgAdapter(List<Msg> msgList){
  17. mMsgList = msgList;
  18. }
  19. @Override
  20. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
  21. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
  22. return new ViewHolder(view);
  23. }
  24. @Override
  25. public void onBindViewHolder(ViewHolder holder,int position){
  26. Msg msg = mMsgList.get(position);
  27. if (msg.getType() == Msg.TYPE_RECEIVED) {
  28. holder.leftLayout.setVisibility(View.VISIBLE);
  29. holder.rightLayout.setVisibility(View.GONE);
  30. holder.leftMsg.setText(msg.getContent());
  31. }else if (msg.getType() == Msg.TYPE_SENT){
  32. holder.rightLayout.setVisibility(View.VISIBLE);
  33. holder.leftLayout.setVisibility(View.GONE);
  34. holder.rightMsg.setText(msg.getContent());
  35. }
  36. }
  37. @Override
  38. public int getItemCount() {
  39. return mMsgList.size();
  40. }
  41. }

        ChatActivity.java

        在包内创建一个名为ChatActivity的Java类文件

  1. public class ChatActivity extends AppCompatActivity {
  2. private Handler mHandler = new Handler();
  3. private List<Msg> msgList = new ArrayList<>();
  4. private EditText inputText;
  5. private Button send;
  6. private RecyclerView msgRecyclerView;
  7. private MsgAdapter adapter;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_chat);
  12. //设置返回按钮
  13. Button back = (Button) findViewById(R.id.weixin_back) ;
  14. back.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View view) {
  17. finish();
  18. }
  19. });
  20. //聊天功能
  21. inintMsgs();
  22. inputText = (EditText) findViewById(R.id.input_txt);
  23. send = (Button) findViewById(R.id.btn_send);
  24. msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
  25. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  26. msgRecyclerView.setLayoutManager(layoutManager);
  27. adapter = new MsgAdapter(msgList);
  28. msgRecyclerView.setAdapter(adapter);
  29. send.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. String content = inputText.getText().toString();
  33. if (!"".equals(content)){
  34. Msg msg = new Msg(content,Msg.TYPE_SENT);
  35. msgList.add(msg);
  36. adapter.notifyItemInserted(msgList.size() - 1);
  37. msgRecyclerView.scrollToPosition(msgList.size() - 1);
  38. inputText.setText("");
  39. recrive();
  40. }else {
  41. Toast.makeText(ChatActivity.this,"请输入",Toast.LENGTH_SHORT).show();
  42. }
  43. }
  44. });
  45. }
  46. private void inintMsgs() {
  47. Msg msg1 = new Msg("Hello",Msg.TYPE_RECEIVED);
  48. msgList.add(msg1);
  49. Msg msg2 = new Msg("Never Mind the Slander and Lies",Msg.TYPE_SENT);
  50. msgList.add(msg2);
  51. Msg msg3 = new Msg("long may the sunshine!",Msg.TYPE_RECEIVED);
  52. msgList.add(msg3);
  53. }
  54. private void recrive(){
  55. Msg msg1 = new Msg("you are dragon,more dragon than me",Msg.TYPE_RECEIVED);
  56. msgList.add(msg1);
  57. }
  58. }

朋友圈功能的实现

        activity_pyquan.xml

        在layout文件夹中创建一个名为activity_pyquan的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:background="#ffffff">
  8. <RelativeLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="70dp"
  11. android:background="#f2f2f2">
  12. <Button
  13. android:id="@+id/pyquan_back"
  14. android:layout_width="40dp"
  15. android:layout_height="40dp"
  16. android:layout_alignParentLeft="true"
  17. android:layout_marginLeft="10dp"
  18. android:layout_marginTop="12dp"
  19. android:background="@mipmap/back" />
  20. <TextView
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="朋友圈"
  24. android:textSize="25dp"
  25. android:textColor="#000000"
  26. android:layout_centerHorizontal="true"
  27. android:layout_marginTop="15dp"/>
  28. <Button
  29. android:layout_width="40dp"
  30. android:layout_height="40dp"
  31. android:background="@mipmap/zhaoxiang"
  32. android:layout_alignParentRight="true"
  33. android:layout_marginTop="12dp"
  34. android:layout_marginRight="10dp"/>
  35. </RelativeLayout>
  36. <ListView
  37. android:id="@+id/pyquan_listview"
  38. android:layout_width="match_parent"
  39. android:layout_height="match_parent"
  40. android:background="#ffffff"
  41. android:overScrollMode="never"/>
  42. </LinearLayout>

        pyquan_content_item.xml

        在layout文件夹中创建一个名为pyquan_content_item的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:ignore="MissingDefaultResource">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:orientation="horizontal"
  13. android:layout_marginBottom="5dp">
  14. <!--头像-->
  15. <ImageView
  16. android:id="@+id/pyquan_touxiang"
  17. android:layout_width="60dp"
  18. android:layout_height="60dp"
  19. android:background="@mipmap/touxiang"
  20. android:layout_marginLeft="15dp"
  21. android:layout_marginTop="15dp"/>
  22. <!--内容-->
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:orientation="vertical"
  27. android:layout_marginLeft="10dp">
  28. <TextView
  29. android:id="@+id/pyquan_name"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="所长的肚子"
  33. android:textSize="20sp"
  34. android:textColor="#2999ce"
  35. android:layout_marginTop="15dp"/>
  36. <TextView
  37. android:id="@+id/pyquan_txt"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="所若干黑色和人格拉尔感觉到开发了的概率较大了开长大ad管理卡带回来卡迪夫辣豆腐静安寺电话费拉收到货了咖啡机阿拉斯加代理费"
  41. android:textSize="17dp"
  42. android:textColor="#000000"/>
  43. <ImageView
  44. android:id="@+id/pyquan_peitu"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:src="@mipmap/photo"
  48. android:layout_marginTop="5dp"/>
  49. <RelativeLayout
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:layout_marginTop="5dp">
  53. <TextView
  54. android:id="@+id/pyquan_time"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:text="2"
  58. android:textColor="#b5b5b5"/>
  59. <TextView
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_toRightOf="@+id/pyquan_time"
  63. android:text="小时前"
  64. android:textColor="#b5b5b5"/>
  65. <ImageView
  66. android:layout_width="40dp"
  67. android:layout_height="20dp"
  68. android:background="@mipmap/dian"
  69. android:layout_alignParentRight="true"
  70. android:layout_marginRight="10dp"
  71. />
  72. </RelativeLayout>
  73. </LinearLayout>
  74. </LinearLayout>
  75. </LinearLayout>

        Pyquan.java

        在entity文件夹中创建一个名为Pyquan的Java类文件

  1. public class Pyquan {
  2. private String name;
  3. private String txt;
  4. private String time;
  5. private int imageId1;
  6. private int imageId2;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public String getTxt() {
  14. return txt;
  15. }
  16. public void setTxt(String txt) {
  17. this.txt = txt;
  18. }
  19. public String getTime() {
  20. return time;
  21. }
  22. public void setTime(String time) {
  23. this.time = time;
  24. }
  25. public int getImageId1() {
  26. return imageId1;
  27. }
  28. public void setImageId1(int imageId1) {
  29. this.imageId1 = imageId1;
  30. }
  31. public int getImageId2() {
  32. return imageId2;
  33. }
  34. public void setImageId2(int imageId2) {
  35. this.imageId2 = imageId2;
  36. }
  37. }

        PyquanAdapter.java

        在adapter文件夹中创建一个名为PyquanAdapter的Java类文件

  1. public class PyquanAdapter extends ArrayAdapter<Pyquan> {
  2. private int resourceId;
  3. private static final String TAG = "PyquanAdapter";
  4. private List<Pyquan> list;
  5. public PyquanAdapter(@NonNull Context context, int resource, @NonNull List<Pyquan> objects) {
  6. super(context, resource, objects);
  7. resourceId = resource;
  8. }
  9. @NonNull
  10. @Override
  11. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
  12. {
  13. Pyquan pyquan = getItem(position);
  14. View view = LayoutInflater.from(getContext()).inflate(resourceId,
  15. parent, false);
  16. ImageView touxiang = view.findViewById(R.id.pyquan_touxiang);
  17. TextView mingcheng = view.findViewById(R.id.pyquan_name);
  18. TextView neirong = view.findViewById(R.id.pyquan_txt);
  19. TextView time = view.findViewById(R.id.pyquan_time);
  20. ImageView peitu = view.findViewById(R.id.pyquan_peitu);
  21. touxiang.setImageResource(pyquan.getImageId1());
  22. peitu.setImageResource(pyquan.getImageId2());
  23. mingcheng.setText(pyquan.getName());
  24. neirong.setText(pyquan.getTxt());
  25. time.setText(pyquan.getTime());
  26. return view;
  27. }
  28. }

        PyquanActivity.java

        在包内创建一个名为PyquanActivity的Java类文件

  1. public class PyquanActivity extends AppCompatActivity {
  2. private List<Pyquan> pyquanList = new ArrayList<>();
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_pyquan);
  7. //设置back按钮
  8. Button back = (Button) findViewById(R.id.pyquan_back) ;
  9. back.setOnClickListener(new View.OnClickListener() {
  10. @Override
  11. public void onClick(View view) {
  12. finish();
  13. }
  14. });
  15. inints();
  16. PyquanAdapter adapter = new PyquanAdapter(getApplicationContext(),R.layout.pyquan_content_item,pyquanList);
  17. ListView listView = findViewById(R.id.pyquan_listview);
  18. listView.setAdapter(adapter);
  19. }
  20. private void inints() {
  21. Pyquan p1 = new Pyquan();
  22. p1.setImageId1(R.mipmap.touxiang);
  23. p1.setImageId2(R.mipmap.photo);
  24. p1.setName("所长的肚子");
  25. p1.setTxt("所长的肚子所长的肚子所长的肚子所长的牛子所长的肚子所长的肚子所长的肚子所长的肚子");
  26. p1.setTime("2");
  27. pyquanList.add(p1);
  28. Pyquan p2 = new Pyquan();
  29. p2.setImageId1(R.mipmap.t_tx05);
  30. p2.setImageId2(R.mipmap.py02);
  31. p2.setName("Mr_Quin");
  32. p2.setTxt("分享图片");
  33. p2.setTime("5");
  34. pyquanList.add(p2);
  35. Pyquan p3 = new Pyquan();
  36. p3.setImageId1(R.mipmap.tx_kenny);
  37. p3.setImageId2(R.mipmap.py03);
  38. p3.setName("机智的肯尼");
  39. p3.setTxt("9494");
  40. p3.setTime("6");
  41. pyquanList.add(p3);
  42. Pyquan p4 = new Pyquan();
  43. p4.setImageId1(R.mipmap.t_tx04);
  44. p4.setImageId2(R.mipmap.py05);
  45. p4.setName("是acc啦");
  46. p4.setTxt("歇了");
  47. p4.setTime("7");
  48. pyquanList.add(p4);
  49. Pyquan p5 = new Pyquan();
  50. p5.setImageId1(R.mipmap.t_tx05);
  51. p5.setImageId2(R.mipmap.py04);
  52. p5.setName("Mr_Quin");
  53. p5.setTxt("都怪小樱");
  54. p5.setTime("8");
  55. pyquanList.add(p5);
  56. Pyquan p6 = new Pyquan();
  57. p6.setImageId1(R.mipmap.t_tx01);
  58. p6.setImageId2(R.mipmap.py06);
  59. p6.setName("笨笨");
  60. p6.setTxt("不列颠传说中的王。也被誉为骑士王。阿尔托莉雅是幼名,自从当上国王之后,就开始被称为亚瑟王了。在骑士道凋零的时代,手持圣剑,给不列颠带来了短暂的和平与最后的繁荣。史实上虽为男性,但在这个世界内却似乎是男装丽人。");
  61. p6.setTime("9");
  62. pyquanList.add(p6);
  63. }
  64. }

工程文件

        这是这整个工程的文件,需要的话可以通过积分下载。

用AndroidStudio制作微信的界面-Android文档类资源-CSDN下载这是一个我用AndroidStudio模仿制作微信界面的工程文件的压缩包更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/nazonomaster/85232385

 #############     2023/7/20更新,优化了界面   #################

 对应的工程文件我也进行了更新

主页界面通讯录界面

发现界面我的界面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/676321
推荐阅读
相关标签
  

闽ICP备14008679号