【发布时间】:2022-01-18 19:09:11
【问题描述】:
我有三个片段:
- 片段 A:类别网格列表
- 片段 B:显示已按下类别的项目
- 片段 C:显示所选项目的详细信息
这一切都很好,或者我认为直到我开始随机点击类别,以便我可以从 A 到 Bo,B 到 C,然后从 C 到 B 到 A。它适用于前 10 次点击,直到它随机停止并且不显示按下类别的项目,这意味着片段 B 除了 bottomNavigationView 不显示任何内容。当我单击 emulator 上的后退按钮时,片段 A 也不显示。只是空白并显示底部和顶部导航栏。 奇怪的是日志显示适配器已获取数据,我的列表也已填充。 有人可以帮忙吗?
编辑:
由于我检查了我的日志(正确获取数据)和布局检查器(我没有看到任何异常),因此进行了编辑以添加一些代码。 这是我的导航图:
<fragment
android:id="@+id/homePageFragment"
android:name="beatview.android.huruapp.activities.homepage.HomePageFragment"
android:label="home_page_fragment"
tools:layout="@layout/home_page_fragment" >
<action
android:id="@+id/action_homePageFragment_to_professionDetailsFragment"
app:destination="@id/professionDetailsFragment">
<argument
android:name="category"
app:argType="string" />
</action>
<action
android:id="@+id/action_homePageFragment_to_favoritesFragment"
app:destination="@id/favoritesFragment" />
<action
android:id="@+id/action_homePageFragment_self"
app:popUpTo="@id/homePageFragment" />
</fragment>
<fragment
android:id="@+id/professionDetailsFragment"
android:name="beatview.android.huruapp.activities.homepage.professions.ProfessionDetailsFragment"
android:label="profession_details_fragment"
tools:layout="@layout/profession_details_fragment">
<argument
android:name="category"
app:argType="string"
android:defaultValue="null" />
<action
android:id="@+id/action_professionDetailsFragment_to_startInterviewFragment"
app:destination="@id/startInterviewFragment" >
<argument
android:name="interviewName"
app:argType="string" />
<argument
android:name="interviewCat"
app:argType="string" />
</action>
<action
android:id="@+id/action_professionDetailsFragment_pop_including_homePageFragment"
app:popUpTo="@id/homePageFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/startInterviewFragment"
android:name="beatview.android.huruapp.activities.homepage.interviews.StartInterviewFragment"
android:label="start_interview_fragment"
tools:layout="@layout/start_interview_fragment" >
<argument
android:name="interviewName"
app:argType="string"
android:defaultValue="null" />
<argument
android:name="interviewCat"
app:argType="string"
android:defaultValue="null" />
<action
android:id="@+id/action_startInterviewFragment_pop_including_professionDetailsFragment"
app:popUpTo="@id/professionDetailsFragment"
app:popUpToInclusive="true" />
</fragment>
还有我的片段(它们看起来都一样):
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel=ViewModelProvider(this, HomePageModelFactory(HomePageRepository())).get(
HomePageViewModel::class.java)
binding = DataBindingUtil.inflate(inflater, R.layout.home_page_fragment, container, false)
binding.recyclerProf.layoutManager= GridLayoutManager(requireContext(),2)
val itemDecoration = GridSpacingItemDecoration(2,5,false )
binding.recyclerProf.addItemDecoration(itemDecoration)
adapter= HomePageAdapter(this.requireContext())
viewModel.getName()
Thread.sleep(300)
viewModel.setupData()
//to ensure the two way binding
binding.myViewModel = viewModel
//to make it sole owner of this lifecycle
binding.recyclerProf.adapter=adapter
return binding.root
}
fun shouldInterceptBackPress() = true
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel.setupData()
viewModel.getName()
viewModel.listCategory.observe(this,{
adapter.setListData(it)
adapter.notifyDataSetChanged()
})
binding.recyclerProf.adapter=adapter
gotoFaves()
getUserFull()
binding.greetings.setText("Hello, "+liveUserSettings.value)
}
和我的适配器(它们也有相同的结构):
fun setListData(data: List<InterviewCategoryDB>) {
this.categories = data.toMutableList()
Log.i("mycategories",data.size.toString())
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = CardItemsBinding.inflate(inflater, parent, false)
return MainViewHolder(binding)
}
override fun getItemCount(): Int {
return categories.size
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
Log.i("myAdapterSizebind", position.toString())
val category = categories[holder.adapterPosition]
// bind ui
//onlicklistener for item
holder.binding.motherCard.setOnClickListener {
val action =
HomePageFragmentDirections.actionHomePageFragmentToProfessionDetailsFragment(category.interviewCategoryName)
findNavController(holder.itemView).navigate(action)
}
}
【问题讨论】:
标签: android android-fragments android-recyclerview