我想在片段中用tablelayout
设置我的viewpager
碎片:
class HomeFragment : Fragment() {
lateinit var bindingFragment: FragmentHomeBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
bindingFragment =
DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
return bindingFragment.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindingFragment.viewPagerActivity = this
}
companion object {
@JvmStatic
@BindingAdapter("set_view_pager")
fun setViewPager(viewPager: ViewPager, context: ActivityMainBinding) {
val ls2 = arrayListOf<ModelAdapterVpCafeList2>()
ls2.add(
ModelAdapterVpCafeList2(
1,
"cafename",
"ali",
4.2f,
"adress khione",
"2",
1,
1,
"https://www.google.com/url?sa=i&url=https%3A%2F%2Fcds.cern.ch%2Fimages%2FCERN-PHOTO-201802-030-10&psig=AOvVaw1E7wOC9sEGShBjS65AYqsy&ust=1616769050893000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCLC83pvUy-8CFQAAAAAdAAAAABAI",
0
)
)
ls2.add(
ModelAdapterVpCafeList2(
2,
"cafename2",
"ali2",
4.2f,
"adress khione2",
"22",
1,
1,
"https://www.google.com/url?sa=i&url=https%3A%2F%2Fcds.cern.ch%2Fimages%2FCERN-PHOTO-201802-030-10&psig=AOvVaw1E7wOC9sEGShBjS65AYqsy&ust=1616769050893000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCLC83pvUy-8CFQAAAAAdAAAAABAI",
0
),
)
ls2.add(
ModelAdapterVpCafeList2(
3,
"cafename3",
"ali3",
4.2f,
"adress khione3",
"22",
1,
1,
"https://www.google.com/url?sa=i&url=https%3A%2F%2Fcds.cern.ch%2Fimages%2FCERN-PHOTO-201802-030-10&psig=AOvVaw1E7wOC9sEGShBjS65AYqsy&ust=1616769050893000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCLC83pvUy-8CFQAAAAAdAAAAABAI",
0
),
)
val ls: List<ModelAdapterVpCafeList> = arrayListOf(
ModelAdapterVpCafeList("همه", ls2),
ModelAdapterVpCafeList("نزدیک من", ls2),
ModelAdapterVpCafeList("باز", ls2)
)
val vpAdapter = AdapterVpCafeList(ls)
viewPager.adapter = vpAdapter
}
@JvmStatic
@BindingAdapter("set_tab_layout")
fun setTabLayout(tabLayout: TabLayout, viewPager: ViewPager) {
tabLayout.setupWithViewPager(viewPager, true)
}
}
}
适配器:
class AdapterVpCafeList(private val ls: List<ModelAdapterVpCafeList>): PagerAdapter(){
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val inflater = LayoutInflater.from(container.context)
val binding: LayooutRecyclerviewHomeSearchBinding = DataBindingUtil.inflate(inflater, R.layout.layoout_recyclerview_home_search, container, false)
binding.cafeList = ls[position]
container.addView(binding.root)
return binding.root
}
override fun getPageTitle(position: Int): CharSequence = ls[position].name
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) = container.removeView(`object` as View)
override fun getCount(): Int = ls.size
override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`
}
我的布局:
<data>
<variable
name="viewPagerActivity"
type="ir.arinateam.cafechejori.view.fragments.HomeFragment" />
<import type="android.view.View"/>
</data>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
app:set_tab_layout="@{vp}">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:set_view_pager="@{viewPagerActivity}"/>
但在布局上,app:set_view_pager=@{viewPagerActivity}
我有这个错误
找不到接受参数类型"ir.arinateam.cafechejori.view.frages.HomeFragment"的setter
如何解决此问题?
我用clean project
和rebuild project
多次求解。