I did not find similar questions so I ask. I have: FirstFragment, SecondFragment and transition:
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="testrenderinglistontransition.FirstFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first">
<action
app:enterAnim="@anim/right_in"
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment"/>
</fragment>
<fragment
android:id="@+id/SecondFragment"
android:name="testrenderinglistontransition.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second">
<action
app:enterAnim="@anim/right_in"
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment"/>
</fragment>
</navigation>
On SecondFragment I emulate getting data and show data during transition:
class SecondFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerview : RecyclerView = view.findViewById(R.id.recyclerview)
val progress : ProgressBar = view.findViewById(R.id.progress)
val adapter = CustomAdapter(arrayOf())
progress.visibility =View.VISIBLE
recyclerview.adapter = adapter
recyclerview.layoutManager = LinearLayoutManager(context).apply {
orientation = LinearLayoutManager.VERTICAL
}
val data = getData()
Handler().postDelayed(Runnable {
progress.visibility =View.GONE
adapter.dataSet = data
adapter.notifyDataSetChanged()
},250) // transition time is 300 so we should call notifyDataSetChanged before ending of transition
}
private fun getData() : Array<String>{
val list = mutableListOf<String>()
for(i in 0 .. 1000){
list.add(UUID.randomUUID().toString())
}
return list.toTypedArray()
}
}
What I have:
Do you have any idea how to fix it? I think I should wait end of transition.
You can take a look on adapter and layout but they are really easiest:
class CustomAdapter(var dataSet: Array<String>) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
init {
// Define click listener for the ViewHolder's View.
textView = view.findViewById(R.id.textView)
}
}
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
// Create a new view, which defines the UI of the list item
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.text_row_item, viewGroup, false)
return ViewHolder(view)
}
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
// Get element from your dataset at this position and replace the
// contents of the view with that element
viewHolder.textView.text = dataSet[position]
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = dataSet.size
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/image"
android:src="@android:drawable/alert_dark_frame"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:layout_alignLeft="@+id/image"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>