Kotlin高仿微信-项目实践58篇详细讲解了各个功能点,包括:注册、登录、主页、单聊(文本、表情、语音、图片、小视频、视频通话、语音通话、红包、转账)、群聊、个人信息、朋友圈、支付服务、扫一扫、搜索好友、添加好友、开通VIP等众多功能。
Kotlin高仿微信-项目实践58篇,点击查看详情
效果图:
实现代码:
<?xml version="1.0" encoding="utf-8"?> <layout> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.wn.wechatclientdemo.view.LineBaseView android:id="@+id/contacts_new_friends" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="12dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/contacts_recyclerview" app:layout_constraintTop_toBottomOf="@+id/contacts_new_friends" app:layout_constraintBottom_toBottomOf="parent" android:layout_width="match_parent" android:layout_height="0dp"/> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
/** * author : wangning * email : maoning20080809@163.com * Date : 2022/4/7 13:13 * description : 通讯录页面 */ class ContactsFragment : BaseDataBindingFragment<WcFragmentContactsBinding>() { override fun getLayoutRes() = R.layout.wc_fragment_contacts private val contactsViewModel : ContactsViewModel by viewModels() private var adapter : ContactsAdapter = ContactsAdapter() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) //TagUtils.d("新联系人 onViewCreated ") contacts_new_friends.builder() .hideRightImage() .hideContent() .setTitle("新的朋友") .setRootClick(View.OnClickListener { //Navigation.findNavController(it).navigate(R.id.action_contacts_friends) ToastUtils.showBaseTip() }) .setLeftImage(R.drawable.wc_friends_contacts) if(!EventBus.getDefault().isRegistered(this)){ EventBus.getDefault().register(this) } var linearLayoutManager = LinearLayoutManager(requireContext()) linearLayoutManager.orientation = LinearLayoutManager.VERTICAL contacts_recyclerview.layoutManager = linearLayoutManager contacts_recyclerview.adapter = adapter var account = DataStoreUtils.getAccount() contactsViewModel.getContactsList(account) contactsViewModel.contactsListLiveData.observe(viewLifecycleOwner) { adapter.refresh(it) } adapter.setOnItemClickInterface(object : ContactsAdapter.OnItemClickInterface{ override fun onItemClick(contactsBean: ContactsBean) { var account = DataStoreUtils.getAccount() var chatccount = "" if(account.equals(contactsBean.fromAccount)){ chatccount = contactsBean.toAccount } else { chatccount = contactsBean.fromAccount } var bundle = bundleOf(ChatFragment.USER_ID to chatccount) Navigation.findNavController(contacts_recyclerview).navigate(R.id.action_chat, bundle) } }) } /*override fun onResume() { super.onResume() TagUtils.d("通讯录 onHiddenChanged = onResume") } override fun onStop() { super.onStop() TagUtils.d("通讯录 onHiddenChanged = onStop ") } override fun onHiddenChanged(hidden: Boolean) { super.onHiddenChanged(hidden) TagUtils.d("通讯录 onHiddenChanged = ${hidden}") if(!hidden){ TagUtils.d("通讯录 onHiddenChanged = ") } }*/ @Subscribe(threadMode = ThreadMode.MAIN) fun onMessageCallback(eventMsgBean: EventMsgBean) { //TagUtils.d("刷新联系人列表 type = ${eventMsgBean.type}") if(eventMsgBean.type == EventMsgBean.TYPE_REFRESH_CONTACTS){ refresh() } } fun refresh(){ var account = DataStoreUtils.getAccount() contactsViewModel.getContactsList(account) } override fun onDestroy() { super.onDestroy() EventBus.getDefault().unregister(this) } }
/** * Author : wangning * Email : maoning20080809@163.com * Date : 2022/4/24 12:09 * Description : 通讯录 */ class ContactsAdapter : RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder>() { private var contactList : MutableList<ContactsBean> = ArrayList() private var onItemClickInterface : OnItemClickInterface? = null fun setOnItemClickInterface(onItemClickInterface: OnItemClickInterface){ this.onItemClickInterface = onItemClickInterface } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactsViewHolder { var view = LayoutInflater.from(parent.context).inflate(R.layout.wc_contacts_item, parent, false) return ContactsViewHolder(view) } override fun onBindViewHolder(holder: ContactsViewHolder, position: Int) { var contactsBean = contactList.get(position) contactsBean?.let { var account = DataStoreUtils.getAccount() var avatarAccount = "" if(account.equals(contactsBean.fromAccount)){ avatarAccount = contactsBean.toAccount } else { avatarAccount = contactsBean.fromAccount } BaseUtils.showAvatarRounded(avatarAccount, holder.avatar, holder.name, BaseUtils.getDimension(R.dimen.distance_4), R.drawable.wc_avatar_default) holder.rootLayout.setOnClickListener { onItemClickInterface?.onItemClick(contactsBean) } } } fun refresh(contactList : MutableList<ContactsBean>) { this.contactList = contactList notifyDataSetChanged() } override fun getItemCount() = contactList?.size class ContactsViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) { var avatar = itemView.findViewById<ImageView>(R.id.contacts_item_avatar) var name = itemView.findViewById<TextView>(R.id.contacts_item_name) var rootLayout = itemView.findViewById<ConstraintLayout>(R.id.contacts_item_layout) } interface OnItemClickInterface{ fun onItemClick(contactsBean: ContactsBean) } }