这是我的项目的简化版。假设我在text view
上方有一个recycler view
,像下面的图片一样位于nested scroll view
内
这是我的项目的XML:
<androidx.core.widget.NestedScrollView
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"
android:background="@android:color/white" android:id="@+id/nestedScrollView_user_control">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/constraintLayout_profile">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:nestedScrollingEnabled="false"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView3"
tools:listitem="@layout/item_general_event" android:id="@+id/recyclerView_attended_event_user_control"
android:layout_marginTop="120dp">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar_user_control"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
以下是我的应用问题的视频:https://drive.google.com/file/d/1E6N0xGDp8sBPrLgwfctTEWeSfyPHevyj/view
如您所见,它位于tab
中的最后一个bottom navigation view
中。当我第一次点击该选项卡时(完全关闭应用程序后),第二个小部件的行为类似于:
text view
将立即出现progress bar
将显示(从服务器中获取数据)recycler view
最终会出现。但是,当我从bottom navigation
视图中的最后一个选项卡切换时,先说第一个选项卡,然后再返回到最后一个选项卡,这会使我的应用看起来很滞后。 textView
将不会立即显示。它会等待,甚至progressBar
也不会显示,并且会与recyclerView
同时显示。
似乎recyclerView
正在设置项目并同时获取数据(不确定原因),这使我的应用看上去很滞后。
我想在点击bottom navigation view
中的标签后立即显示文本视图
下面是我使用的代码:
class UserControlFragment : Fragment(), EasyPermissions.PermissionCallbacks {
lateinit var nestedScrollView: NestedScrollView
lateinit var progressBar : ProgressBar
lateinit var fragmentView : View
lateinit var recyclerView : RecyclerView
lateinit var sharedPrefManager : SharedPreferenceManager
lateinit var userControlAdapter : GeneralEventRecyclerViewAdapter
private var attendedEvents = ArrayList<Event>()
private var userData : User? = null
lateinit var mContext : Context
lateinit var mActivity : FragmentActivity
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
activity?.let { mActivity = it }
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = inflater.inflate(R.layout.fragment_user_control, container, false)
sharedPrefManager = SharedPreferenceManager.getInstance(mContext)
setUpViewsDeclaration()
getUserData()
initRecyclerView()
getAttendedEvents()
return fragmentView
}
private fun setUpViewsDeclaration() {
nestedScrollView = fragmentView.findViewById(R.id.nestedScrollView_user_control)
progressBar = fragmentView.findViewById(R.id.progressBar_user_control)
recyclerView = fragmentView.findViewById(R.id.recyclerView_attended_event_user_control)
}
private fun getUserData() {
userData = sharedPrefManager.loadUserData()
}
private fun initRecyclerView() {
userControlAdapter = GeneralEventRecyclerViewAdapter(mContext)
val layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL,false)
recyclerView.adapter = userControlAdapter
recyclerView.layoutManager = layoutManager
nestedScrollView.isNestedScrollingEnabled = false
userControlAdapter.setOnItemClickListener(object: OnEventKMListener {
override fun eventKMClicked(position: Int) {
val selectedEvent = attendedEvents[position]
val eventDetailDestination = UserControlFragmentDirections.actionGlobalDestinationEventDetail(selectedEvent)
Navigation.findNavController(fragmentView).navigate(eventDetailDestination)
}
})
}
private fun getAttendedEvents() {
val user = userData ?: return
progressBar.visibility = View.VISIBLE
FirestoreKMClient.getRecommendedEvents(user.domicile) { errorMessage, events ->
progressBar.visibility = View.GONE
errorMessage?.let {
mActivity.toast(it)
} ?: run {
val eventList = events ?: ArrayList()
attendedEvents = eventList
userControlAdapter.submitList(eventList)
}
}
}
}
这是我用于适配器的代码:
class GeneralEventRecyclerViewAdapter(val mContext: Context): ListAdapter<Event, RecyclerView.ViewHolder>(DIFF_CALLBACK) {
lateinit var mOnEventKMListener : OnEventKMListener
fun setOnItemClickListener(listener: OnEventKMListener) {
mOnEventKMListener = listener
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_general_event, parent, false)
return GeneralEventViewHolder(itemView, mOnEventKMListener)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val currentEvent = getItem(position)
val generalEventViewHolder = holder as GeneralEventViewHolder
Glide.with(mContext).load(currentEvent.thumbnailDownloadPath)
.apply(RequestOptions.bitmapTransform(BlurTransformation(25, 3)))
.into(generalEventViewHolder.blurThumbnailImageView)
Glide.with(mContext).load(currentEvent.thumbnailDownloadPath).into(generalEventViewHolder.thumbnailImageView)
generalEventViewHolder.eventNameTextView.text = currentEvent.title
generalEventViewHolder.eventVenueTextView.text = currentEvent.venue
generalEventViewHolder.eventDateTimeTextView.text = DateTimeService.changeDateToString("EEEE, d MMMM. HH:mm zzz",currentEvent.dateTimeStart)
}
companion object {
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Event>() {
override fun areItemsTheSame(oldItem: Event, newItem: Event): Boolean {
return oldItem.eventID == newItem.eventID
}
override fun areContentsTheSame(oldItem: Event, newItem: Event): Boolean {
return oldItem == newItem
}
}
}
}
Java
代码很好