NotesFragment принимает сообщения из другого фрагмента и добавляет их в RecycleView, но на экране ничего не отображается. Сообщения до фрагмента доходят, проверяла
Activity:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
startNotesFragment()
initListeners()
}
private fun initListeners() {
binding.bAdd.setOnClickListener {
startEditFragment()
}
}
private fun startEditFragment() {
supportFragmentManager.beginTransaction()
.replace(R.id.fl_container, EditFragment.newInstance()).addToBackStack(null).commit()
}
private fun startDetailFragment() {
supportFragmentManager.beginTransaction().replace(
R.id.fl_container,
DetailFragment.newInstance(DetailFragmentParams(detail = "note"))
).addToBackStack(null).commit()
}
private fun startNotesFragment() {
supportFragmentManager.beginTransaction().replace(
R.id.fl_container,
NotesFragment.newInstance()
).addToBackStack(null).commit()
}
}
Фрагмент с RecycleView:
class NotesFragment : Fragment() {
private var binding: FragmentNotesBinding? = null
private val adapter = NoteAdapter()
companion object {
fun newInstance() = NotesFragment()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentNotesBinding.inflate(layoutInflater)
initView()
return binding?.root
}
private fun initView() {
binding!!.rcList.layoutManager = GridLayoutManager(context, 3)
binding!!.rcList.adapter = adapter
}
override fun onResume() {
super.onResume()
setFragmentResultListener("key") { key, bundle ->
val result = bundle.getString("bundleKey")
if (result != null) {
adapter.addNote(result)
}
}
}
override fun onDestroy() {
super.onDestroy()
binding = null
}
}
Адаптер:
class NoteAdapter : RecyclerView.Adapter<NoteAdapter.NoteHolder>() {
private val noteList = ArrayList<String>()
class NoteHolder(item: View) : RecyclerView.ViewHolder(item) {
private val binding = MyListItemBinding.bind(item)
fun bind(note: String) = with(binding) {
tvMessage.text = note
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.note_item, parent, false)
return NoteHolder(view)
}
override fun onBindViewHolder(holder: NoteHolder, position: Int) {
holder.bind(noteList[position])
}
override fun getItemCount(): Int {
return noteList.size
}
fun addNote(note: String) {
noteList.add(note)
notifyDataSetChanged()
}
}
Фрагмент, отправляющий текст пользователя в NotesFragment:
class EditFragment : Fragment() {
private var binding: FragmentEditBinding? = null
companion object {
fun newInstance() = EditFragment()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentEditBinding.inflate(layoutInflater)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding!!.bUndo.setOnClickListener {
activity?.onBackPressed()
}
binding?.bSave?.setOnClickListener {
val result = binding!!.etAuthorText.text.toString()
setFragmentResult("key", bundleOf("bundleKey" to result))
activity?.onBackPressed()
}
}
override fun onDestroy() {
super.onDestroy()
binding = null
}
}
activity_main_xml:
<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"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/bAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="@dimen/marginAroundButton"
android:text="@string/add"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_notes_xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotesFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
my_list_item.xml (Элемент RecycleView)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="start"
android:maxLines="3"
android:textSize="@dimen/smallPrint">
</TextView>