主要功能:
·创建笔记
·插入图片
·插入链接
·设置标签颜色
·保存笔记
·主页搜索笔记
·删除笔记
工程文件后端与部分代码展示:
MainAvtivity:
package com.codingwithme.notesapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
replaceFragment(HomeFragment.newInstance(),false)
}
fun replaceFragment(fragment:Fragment, istransition:Boolean){
val fragmentTransition = supportFragmentManager.beginTransaction()
if (istransition){
fragmentTransition.setCustomAnimations(android.R.anim.slide_out_right,android.R.anim.slide_in_left)
}
fragmentTransition.add(R.id.frame_layout,fragment).addToBackStack(fragment.javaClass.simpleName).commit()
}
override fun onBackPressed() {
super.onBackPressed()
val fragments = supportFragmentManager.fragments
if (fragments.size == 0){
finish()
}
}
}
在MainActivity
中管理片段的替换和返回操作。replaceFragment
方法用于替换片段,并可以选择性地使用过渡动画,而onBackPressed
方法用于处理返回操作,当返回栈中没有其他片段时,结束当前活动。
HomeFragment:
package com.codingwithme.notesapp
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SearchView
import android.widget.Toast
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.codingwithme.notesapp.adapter.NotesAdapter
import com.codingwithme.notesapp.database.NotesDatabase
import com.codingwithme.notesapp.entities.Notes
import kotlinx.android.synthetic.main.fragment_create_note.*
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList
class HomeFragment : BaseFragment() {
var arrNotes = ArrayList<Notes>()
var notesAdapter: NotesAdapter = NotesAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
HomeFragment().apply {
arguments = Bundle().apply {
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recycler_view.setHasFixedSize(true)
recycler_view.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
launch {
context?.let {
var notes = NotesDatabase.getDatabase(it).noteDao().getAllNotes()
notesAdapter!!.setData(notes)
arrNotes = notes as ArrayList<Notes>
recycler_view.adapter = notesAdapter
}
}
notesAdapter!!.setOnClickListener(onClicked)
fabBtnCreateNote.setOnClickListener {
replaceFragment(CreateNoteFragment.newInstance(),false)
}
search_view.setOnQueryTextListener( object : SearchView.OnQu