K
K
Koshkasobaka2021-06-08 08:52:39
Android
Koshkasobaka, 2021-06-08 08:52:39

Why is RecycleView not showing?

The NotesFragment takes messages from another fragment and adds them to the RecycleView, but nothing is displayed on the screen. Messages reach the fragment,
Activity checked:

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()

    }
}

Fragment with 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
    }
}


Adapter:
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()
    }
}


Fragment sending user text to 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 Item)
<?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>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2021-06-08
@zagayevskiy

Doesn't it bother you that initView is called after the return from the method? The studio will shout about it to you.

R
Roman Vasilenko, 2021-06-17
@gigacoder

Perhaps this will help:

class NotesFragment : Fragment() {
    ...
    override fun onResume() {
        ...
        if (result != null) {
            binding!!.rcList.post { // Добавить это
                adapter.addNote(result)
            } // и это
        }
        ...
    }
    ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question