我是新来的,需要帮助。 这是我的代码。
活动
class ProfileActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
var adapter: PagerAdapter = IntroSliderAdapter(applicationContext)
viewpager.adapter = adapter
}
适配器
class IntroSliderAdapter(var context: Context) : PagerAdapter() {
lateinit var inflater: LayoutInflater
var texts: Array<Int> = arrayOf(R.string.intro_text1,R.string.intro_text2,R.string.intro_text3)
override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as ConstraintLayout
override fun getCount(): Int = texts.size
override fun instantiateItem(container: ViewGroup, position: Int): Any {
inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var view: View = inflater.inflate(R.layout.intro_slider, container, false)
Log.i("INFO", "View: " + view.toString())
Log.i("INFO", "Container:" + container.toString())
container!!.addView(view)
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container!!.removeView(`object` as ConstraintLayout)
}
活动布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/profileContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".ProfileActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
滑块布局
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/introLayout">
<ImageView
android:id="@+id/mainLogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/introText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
...
</android.support.constraint.ConstraintLayout>
我如何理解inflater.inflate(R.layout.intro_slider,container,false)必须推送intro_slider.xml内容才能查看,但是当我运行应用程序时,我只会看到空的幻灯片。请帮助我了解它为什么会发生。
答案 0 :(得分:1)
就我而言,问题是错综复杂的。但是主要问题是:例如,不正确的xml填充布局不正确,因为app:srcCompat必须是android:src等,所以我的徽标图像不可见。
感谢Spritzig的帮助!