【发布时间】:2018-07-31 20:23:40
【问题描述】:
我想添加一个按钮(id='Home'),它带我进入我使用这些代码但它没有反应的主要活动,这是我在 GITHUB 中的项目示例,请从 HERE 检查它(问题已被@advice-dog 修复,已添加到此项目中)
在我的 ViewPager 布局中:
<?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:id="@+id/bg2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/AppFullScreenTheme"
tools:context=".License">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我在那里上课:
package com.medanis.fneclis
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.view.ViewPager
import kotlinx.android.synthetic.main.activity_license.*
class License : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_license)
val viewPager = findViewById<ViewPager>(R.id.view_pager)
viewPager.adapter = CustomPagerAdapter(this)
}
}
在 ViewPager 的第一页中,我有 Home 按钮:
<?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:id="@+id/l1_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fneclis_l1_bg"
tools:context=".AllButtons">
<Button
android:id="@+id/Home"
android:layout_width="56dp"
android:layout_height="53dp"
android:layout_marginBottom="523dp"
android:layout_marginEnd="312dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="312dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@drawable/ic_home_black_24dp"
android:contentDescription="@string/todo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
与 AllButtons 类相关(我想在 ViewPage 的大多数页面中使用它,但不是全部):
package com.medanis.fneclis
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_l1_page.*
class AllButtons: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_l1_page)
// those are some other pages which contains the same button and they are inside the ViewPager
/* setContentView(R.layout.activity_l2_page)
setContentView(R.layout.activity_l3_page)
setContentView(R.layout.activity_m1page)
setContentView(R.layout.activity_m2page)*/
Home.setOnClickListener{
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
} }
适配器:
package com.medanis.fneclis
import android.app.Activity
import android.content.Context
import android.support.v4.view.PagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class CustomPagerAdapter(private val mContext: Context) : PagerAdapter() {
override fun instantiateItem(collection: ViewGroup, position: Int): Any {
val modelObject = ModelObject.values()[position]
val inflater = LayoutInflater.from(mContext)
val viewGroup = inflater.inflate(modelObject.titleResId, collection, false) as ViewGroup
collection.addView(viewGroup)
return viewGroup
}
override fun destroyItem(collection: ViewGroup, position: Int, view: Any) {
collection.removeView(view as View)
}
override fun getCount(): Int {
return ModelObject.values().size
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
override fun getPageTitle(position: Int): CharSequence {
val customPagerEnum = ModelObject.values()[position]
return mContext.getString(customPagerEnum.titleResId)
}
override fun getItemPosition(`object`: Any): Int {
return super.getItemPosition(`object`)
}
}
模型对象:
package com.medanis.fneclis
enum class ModelObject private constructor(val titleResId: Int ) {
LICENSEONE(R.layout.activity_l1_page),
PAGEONE(R.layout.activity_l1s1page),
PAGETWO(R.layout.activity_l1s2page),
LICENSETWO(R.layout.activity_l2_page),
PAGETHREE(R.layout.activity_l2_s3page),
PAGEFOUR(R.layout.activity_l2_s4page),
LICENSETHREE(R.layout.activity_l3_page),
PAGEFIVE(R.layout.activity_l3_s5page),
PAGESIX(R.layout.activity_l3_s6page),
}
【问题讨论】:
-
你如何使用你的
AllButtons类?您似乎只是在您的Adapter中构建了一个View,而不是使用AllButtons类。 -
我是初学者,请告诉我该怎么做?
-
给我几分钟。
-
慢慢来,请不要忘记我^_^
-
这些答案对您有帮助吗?
标签: android android-studio android-fragments kotlin android-viewpager