我是 Kotlin 和 Android Studio 的新手,我正在努力改進一些東西......我正在嘗試使用 View Pager 2 和圓形指示器制作一個應用程式..
這比應該的更困難,因為我正在從幾個片段中作業..
長話短說,它們是我想為 View Pager 和圓形指示器功能提供的特定片段,但它讓我在后端作業時感到困惑。
所以我這是.xml我想要賦予功能的那個片段的樣子:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".StoriesFragment">
<androidx.viewpager2.widget.ViewPager2
android:id="@ id/ssViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<me.relex.circleindicator.CircleIndicator3
android:id="@ id/ssCircleIndicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/teal_700"
app:layout_constraintBottom_toBottomOf="@ id/ssViewPager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
和 .kt 檔案:
package com.example.firstmillion
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class StoriesFragment : Fragment(R.layout.fragment_stories) {
private var titlesList = mutableListOf<String>()
private var imagesList = mutableListOf<Int>()
// unsure of the place
postToList()
// I didn't complete this code
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
ssViewPager.orientation =
ssViewPager.ad
private fun addToList(title: String, image: Int) {
titlesList.add(title)
imagesList.add(image)
}
private fun postToList() {
for (i in 1..5) {
addToList("Title $i", R.drawable.ss_coco_chanel)
}
}
}
當我得到這一切時,我正在學習一個教程,問題是我收到一個警告錯誤,當我撰寫以下內容時,其中有幾個實際上來自 .kt 檔案:
postToList()
// This is code is not complete RN
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
ssViewPager.orientation =
ssViewPager.ad
I actually don't know if I doing this right, I thought I will write the backend code in the.kt file of the fragment I want to give the feature, though the tutorial I was following wrote it in the MainActivity.kt file, he was working on a single screen though..
In sumary, My Questions are:
- Is it wrong that I'm writing the backend code in the .kt file of the fragment I want to give the feature, and not the
MainActivity.kt? - In the Tutorial he wrote this particular code giving me error (the one I pointed out above) inside the onCreate function. I tried to do same, as I cleared the .kt file of the fragment I want to have the feature, and put the codes in MainActivity.Kt I still got another error warning.
(Here's what the .kt of the fragment now looks like:)
package com.example.firstmillion
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class StoriesFragment : Fragment(R.layout.fragment_stories) {
}
(這是 MainActivity 的代碼:)
class MainActivity : AppCompatActivity() {
//ss
private var titlesList = mutableListOf<String>()
private var imagesList = mutableListOf<Int>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val homeFragment = HomeFragment()
val storiesFragment = StoriesFragment()
val menuFragment = MenuFragment()
//This set the fragment that shows when the App is Opened
//In this case homeFragment
setCurrentFragment(homeFragment)
//This makes switching to other fragments possible
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
bottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId) {
R.id.miHome -> setCurrentFragment(homeFragment)
R.id.miStories -> setCurrentFragment(storiesFragment)
R.id.miMenu -> setCurrentFragment(menuFragment)
}
true
}
//for viewpager of success story
postToList()
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
ssViewPager.orientation = ss
ssViewPager.ad
}
private fun setCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.fl_fragment, fragment)
commit()
}
// ss
private fun addToList(title: String, image: Int) {
titlesList.add(title)
imagesList.add(image)
}
private fun postToList() {
for (i in 1..5) {
addToList("Title $i", R.drawable.ss_coco_chanel)
}
}
- 我注意到這是當我參考函式時
postToList()出現錯誤,但當我MainActivity.kt在片段的 .kt 檔案中參考它時沒有出現錯誤,所以哪里是我寫的最佳位置這。 - 函式之后的最后一個也是最重要的問題,另一個給出警告錯誤的代碼是一個 ID。就像
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)ssViewPager 是一個 ID 一樣,這正是教程中的樣子,請問我遺漏了什么嗎?
這是一個很長的問題,我真的很感激你的幫助,謝謝你通過它。感謝您的反饋意見。謝謝你的耐心。并感謝您的時間..
uj5u.com熱心網友回復:
有很多方法可以解決這個問題,第一種方法是通過 CircleIndicator 做配接器并在該配接器中執行您需要的操作。
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList, circleIndicator)
其他方法是傳遞片段并從該片段呼叫一個函式來處理 CircleIndicator
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList, storiesFragment)
在故事片段中創建一個函式來為圓形指示器執行某些操作并在配接器中呼叫它
storiesFragment.stopCircleIndicator()
uj5u.com熱心網友回復:
我需要做的就是findViewByID用于 ssViewPager
這將使 MainActivity.kt 看起來像這樣:
class MainActivity : AppCompatActivity() {
//ss
private var titlesList = mutableListOf<String>()
private var imagesList = mutableListOf<Int>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val homeFragment = HomeFragment()
val storiesFragment = StoriesFragment()
val menuFragment = MenuFragment()
//This set the fragment that shows when the App is Opened
//In this case homeFragment
setCurrentFragment(homeFragment)
//This makes switching to other fragments possible
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
bottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId) {
R.id.miHome -> setCurrentFragment(homeFragment)
R.id.miStories -> setCurrentFragment(storiesFragment)
R.id.miMenu -> setCurrentFragment(menuFragment)
}
true
}
//for viewpager of success story
postToList()
val ssViewPager: ViewPager2 = findViewById(R.id.ssViewPager)
ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
ssViewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
val ssCircleIndicator = findViewById<CircleIndicator3>(R.id.ssCircleIndicator)
ssCircleIndicator.setViewPager(ssViewPager)
}
private fun setCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.fl_fragment, fragment)
commit()
}
// ss
private fun addToList(title: String, image: Int) {
titlesList.add(title)
imagesList.add(image)
}
private fun postToList() {
for (i in 1..5) {
addToList("Title $i", R.drawable.ss_coco_chanel)
}
}
}
我將故事片段的 .kt 留空,并在 MainActivity.kt 中撰寫了代碼,現在它有點作業了。謝謝你的時間..
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382581.html
標籤:安卓 安卓工作室 科特林 android-fragments
上一篇:抽象類BaseVMActivity<VM:ViewModel,B:ViewBinding>和抽象類BaseVMActivity(VM:ViewModel,B:ViewBinding)的區別
