如何在Android中使用RecylerView实现聊天框效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联公司拥有十多年成都网站建设工作经验,为各大企业提供网站设计、成都做网站服务,对于网页设计、PC网站建设(电脑版网站建设)、app软件开发公司、wap网站建设(手机版网站建设)、程序开发、网站优化(SEO优化)、微网站、域名与空间等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了很多网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等网站化运作于一体,具备承接各种规模类型的网站建设项目的能力。1、activity_main.xml的代码如下:
2、layout_item_content.xml的代码如下:
3、RecyclerViewAdapter.java的代码如下:
package com.deepreality.recyclerviewdemo; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; public class RecyclerViewAdapter extends RecyclerView.Adapter{ private Context mContext; private List tbChatContentList; private Tb_ChatContent tb_chatContent; static class ViewHolder extends RecyclerView.ViewHolder { private LinearLayout lLayoutReceive, lLayoutSend; private TextView tvReceive, tvSend; public ViewHolder(View itemView) { super(itemView); lLayoutReceive = itemView.findViewById(R.id.Layout_Item_Content_lLayoutReceive); lLayoutSend = itemView.findViewById(R.id.Layout_Item_Content_lLayoutSend); tvReceive = itemView.findViewById(R.id.Layout_Item_Content_tvContentReceive); tvSend = itemView.findViewById(R.id.Layout_Item_Content_tvContentSend); } } public RecyclerViewAdapter(Context mContext, List tbChatContentList) { this.mContext = mContext; this.tbChatContentList = tbChatContentList; } @NonNull @Override public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_content, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) { tb_chatContent = tbChatContentList.get(position); if (tb_chatContent.getType() == 0) { holder.lLayoutReceive.setVisibility(View.VISIBLE); holder.lLayoutSend.setVisibility(View.GONE); holder.tvReceive.setText(tb_chatContent.getContent()); } else { holder.lLayoutReceive.setVisibility(View.GONE); holder.lLayoutSend.setVisibility(View.VISIBLE); holder.tvSend.setText(tb_chatContent.getContent()); } } @Override public int getItemCount() { return tbChatContentList.size(); } }
4、MainActivity.java的代码如下:
package com.deepreality.recyclerviewdemo; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Context mContext; private RecyclerView rViewChat; private EditText etContent; private Button btnSend; private ListAndroid是什么tbChatContentList; private Tb_ChatContent tb_chatContent; private RecyclerViewAdapter recyclerViewAdapter; private String[] arrayContents = new String[]{"How are you", "Fine,Thank you.", "How are you" , "Fine,Thank you.", "How are you", "Fine,Thank you."}; private int[] arrayTypes = new int[] {0, 1, 0, 1, 0, 1}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); baseDataInit(); bindViews(); viewsAddListener(); viewsDataInit(); } private void baseDataInit() { mContext = this; tbChatContentList = new ArrayList<>(); } private void bindViews() { rViewChat = findViewById(R.id.Main_rView); etContent = findViewById(R.id.Main_etContent); btnSend = findViewById(R.id.Main_btnSend); } private void viewsAddListener() { btnSend.setOnClickListener(this); } private void viewsDataInit() { rViewSetAdapter(); } private void rViewSetAdapter() { for (int i = 0; i < arrayContents.length; i++) { tb_chatContent = new Tb_ChatContent(arrayContents[i], arrayTypes[i]); tbChatContentList.add(tb_chatContent); } //设置RecylerView的排列方式(线性,网格,瀑布流三种) LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext); rViewChat.setLayoutManager(linearLayoutManager); //创建并绑定数据适配器 recyclerViewAdapter = new RecyclerViewAdapter(mContext, tbChatContentList); rViewChat.setAdapter(recyclerViewAdapter); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.Main_btnSend: { tb_chatContent = new Tb_ChatContent(etContent.getText().toString(), Tb_ChatContent.TYPE_SEND); tbChatContentList.add(tb_chatContent); //数据刷新 recyclerViewAdapter.notifyDataSetChanged(); //滑动到某一位置 rViewChat.smoothScrollToPosition(tbChatContentList.size() - 1); break; } default:break; } } }
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
关于如何在Android中使用RecylerView实现聊天框效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。