Все просто, у меня есть viewpager
с некоторыми изображениями, которые включены в макет fragment
.Когда я запускаю проект, все макеты отображаются, но viewpager
не ... просто так:
Итак, япоместит здесь код, который имеет значение.Перво-наперво, Viewpager
:
class ViewPageAdapter(private val context: Context) : PagerAdapter(){
private var layoutInflater:LayoutInflater?= null
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
override fun getCount(): Int {
return images.size
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
/**Set our vals to "inflate"**/
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val v = layoutInflater!!.inflate(R.layout.custom_layout, null)
val image = v.findViewById<View>(R.id.image_view) as ImageView
/**Positions of images**/
auxPosition = images[position]
val vp = container as ViewPager
/** Add custom_layout.xml to our viewpager**/
vp.addView(v, 0)
return v
}
}
Соответствующий фрагмент:
class FeridasFragment : Fragment() {
internal lateinit var viewPager: ViewPager
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val conteudo_feridas=inflater!!.inflate(R.layout.fragment_feridas, container, false)
//the viewpager
viewPager = conteudo_feridas.findViewById<View>(R.id.viewPagerGaleria) as ViewPager
val adapter = ViewPageAdapter(activity!!.application)
viewPager.adapter = adapter
viewPager.currentItem = images.size
//nav dots of viewpager
val pageIndicatorView = conteudo_feridas.findViewById<View>(R.id.pageIndicatorView) as PageIndicatorView
pageIndicatorView.setViewPager(viewPagerGaleria)
pageIndicatorView.count = images.size
pageIndicatorView.selection = images.size
return conteudo_feridas
}
Компоновка виджета:
<android.support.constraint.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">
<android.support.v4.view.ViewPager
android:id="@+id/viewPagerGaleria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/pageIndicatorView">
</android.support.v4.view.ViewPager>
//Forget about this, is just a indicator
<com.rd.PageIndicatorView
android:id="@+id/pageIndicatorView"
android:layout_width="wrap_content"
app:layout_constraintBottom_toTopOf="@id/viewPagerGaleria"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:piv_padding="50dp"
app:piv_animationType="scale"
app:piv_viewPager="@id/viewPagerGaleria"
android:layout_height="wrap_content"
/>
</android.support.constraint.ConstraintLayout>