Android 如何使用kotlin添加按钮片段、ViewPager
我想添加一个按钮(id='Home'),它将我带到我使用这些代码的主要活动,但它没有反应,这是我在GITHUB中的项目示例,请检查它(问题已由@advice dog修复,它已添加到此项目中) 在我拥有的ViewPager布局中:Android 如何使用kotlin添加按钮片段、ViewPager,android,android-studio,android-fragments,kotlin,android-viewpager,Android,Android Studio,Android Fragments,Kotlin,Android Viewpager,我想添加一个按钮(id='Home'),它将我带到我使用这些代码的主要活动,但它没有反应,这是我在GITHUB中的项目示例,请检查它(问题已由@advice dog修复,它已添加到此项目中) 在我拥有的ViewPager布局中: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/
<?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>
<?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>
适配器:
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
活动,您只是使用布局并在CustomPagerAdapter
上对其进行充气,并且由于您没有使用活动,因此未设置主页按钮上的单击侦听器
- 有几件重要的事情需要知道。通过安卓系统。正如您在该页面上所看到的,您只能向视图寻呼机添加片段。因此,您需要将
转换为所有按钮
。然后在片段
片段的
方法的
上设置单击侦听器onViewCreated
class AllButtonsFragment: Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val root = inflater.inflate(R.layout.activity_l1_page, container, false) return root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) home.setOnClickListener { val intent = Intent(this, MainActivity::class.java) startActivity(intent) } } }
newInstance()
的fragment
上创建一个静态函数来实现这一点,这是一个很好的练习),并根据位置在instantiateItem
函数上返回适当的片段
override fun instantiateItem(collection: ViewGroup, position: Int): Any {
return when (position) {
0 -> A_FUNCTION.newInstance()
1 -> B_FUNCTION.newInstance()
else -> C_FUNCTION.newInstance()
}
}
我重新编写了您的代码,我将解释一些关于如何改进的提示和建议 首先,始终在
活动
或片段
的末尾添加活动
或片段
,这将有助于使事情更容易理解
class LicenseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_license)
// You can simply use the id of the View here.
view_pager.adapter = CustomPagerAdapter(supportFragmentManager)
}
}
接下来,我将您的所有按钮
活动
更改为片段
class AllButtonsFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.activity_l1_page, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
home.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
}
这段代码有点复杂,但它使用了一种非常常见的模式,newInstance
模式。要创建新的片段
,您需要创建一个静态方法,该方法将返回一个片段
,并将您的值传递到参数
class AllButtonsFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.activity_l1_page, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
home.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
}
class AllButtonsFragment : Fragment() {
companion object {
private const val EXTRA_COLOR = "EXTRA_COLOR"
private const val EXTRA_LAYOUT = "EXTRA_LAYOUT"
fun newInstance(modelObject: ModelObject): Fragment {
val fragment = AllButtonsFragment()
// Creating a Bundle with the various values we want to pass, such as the color and layout.
val bundle = Bundle()
bundle.putInt(EXTRA_COLOR, modelObject.titleResId)
bundle.putInt(EXTRA_LAYOUT, modelObject.layoutResId)
fragment.arguments = bundle
return fragment
}
}
// Getting the layout from the arguments to inflate the View.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val layout = arguments?.getInt(EXTRA_LAYOUT)
?: throw IllegalStateException("arguments does not contain a layout.")
return inflater.inflate(layout, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val color = arguments?.getInt(EXTRA_COLOR)
// TODO: Do whatever with your colour.
// Using findViewById since this isn't on every layout.
view?.findViewById<View>(R.id.Home)?.setOnClickListener {
val intent = Intent(context, MainActivity::class.java)
startActivity(intent)
}
}
}
所以,再解释一下。您创建了一个名为newInstance
的函数,该函数获取此片段
应该知道的信息,在本例中,是我们希望它显示的布局
接下来,创建一个捆绑包
,并将布局放入其中
最后,将片段
的参数分配给包
class AllButtonsFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.activity_l1_page, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
home.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
}
这是创建片段的最安全的方法
class AllButtonsFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.activity_l1_page, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
home.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
}
class AllButtonsFragment : Fragment() {
companion object {
private const val EXTRA_COLOR = "EXTRA_COLOR"
private const val EXTRA_LAYOUT = "EXTRA_LAYOUT"
fun newInstance(modelObject: ModelObject): Fragment {
val fragment = AllButtonsFragment()
// Creating a Bundle with the various values we want to pass, such as the color and layout.
val bundle = Bundle()
bundle.putInt(EXTRA_COLOR, modelObject.titleResId)
bundle.putInt(EXTRA_LAYOUT, modelObject.layoutResId)
fragment.arguments = bundle
return fragment
}
}
// Getting the layout from the arguments to inflate the View.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val layout = arguments?.getInt(EXTRA_LAYOUT)
?: throw IllegalStateException("arguments does not contain a layout.")
return inflater.inflate(layout, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val color = arguments?.getInt(EXTRA_COLOR)
// TODO: Do whatever with your colour.
// Using findViewById since this isn't on every layout.
view?.findViewById<View>(R.id.Home)?.setOnClickListener {
val intent = Intent(context, MainActivity::class.java)
startActivity(intent)
}
}
}
您如何使用所有按钮类?似乎您只是在适配器中构建视图
,但是不要使用AllButtons
类。我是初学者,请告诉我该怎么做?给我几分钟时间。慢慢来,请不要忘记我这些答案对你有帮助吗?它给了我一个关于a_函数/b_函数和C_函数的错误。我在github中添加了一个项目示例,请检查一下,不要给我一个关于allbuttonf片段的错误:return setContentView(layout)and in Intent()我在github中添加了一个项目示例,请检查一下,我犯了一些错误。我把它们修好了,我甚至还为你的回购提出了一个请求。现在一切都应该正常了。我已经更新了我的答案,以反映这些代码更改。谢谢。我明天会检查它,并告诉您发生了什么。^再次感谢