我是 Kotlin 和 Android 編程的新手。
我有 2 個使用 TabLayout 和 ViewPager2 的選項卡。在我的主要活動 xml 中,我有編輯文本小部件。當我輸入文本并按 ENTER 時,程式需要從編輯文本中獲取“鍵”的“值”并將其作為按鈕添加到兩個選項卡(片段)中。現在,如果我在第一個選項卡中 - 我似乎無法將按鈕添加到第二個選項卡。因此,我僅在選擇第二個選項卡后才嘗試添加按鈕,但是在按計劃滑動到另一個選項卡的情況下,單擊該選項卡似乎不起作用。
請幫助我:
- 編輯另一個現在處于“焦點”的片段
- 修復所描述的點擊/滑動問題。
謝謝!
我的 MainActivity.kt:
class MainActivity : AppCompatActivity() {
//declare all collections of barcode, items and changes
var mConstantBarcodeMap = Constants.constantBarcodeMap
private var usedBarcodeItems = mutableMapOf<String,String>()
// declare binding object for all layouts
private lateinit var bindingMain : ActivityMainBinding
// declare tab and viewpager2 instances for building tabbed application
private lateinit var tabLayout : TabLayout
private lateinit var viewPager2 : ViewPager2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// make binding object for all views
bindingMain = ActivityMainBinding.inflate(layoutInflater)
val viewMain = bindingMain.root
setContentView(viewMain)
getWindow().setBackgroundDrawableResource(R.drawable.background_iphone2);
//get tab layout and viewPager2 from xml file
tabLayout = findViewById(R.id.tab_layout)
viewPager2 = findViewById(R.id.view_pager_2)
val adapter = ViewPagerAdapter(supportFragmentManager, lifecycle)
viewPager2.adapter = adapter
TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
when (position) {
0 -> tab.text = "Add"
1 -> tab.text = "Remove"
}
}.attach()
// declare tab selected listener
tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
tabChanged(tabLayout.selectedTabPosition)
}
override fun onTabUnselected(tab: TabLayout.Tab) {}
override fun onTabReselected(tab: TabLayout.Tab) {}
})
// Make edit text listener
val editTextInput = findViewById<EditText>(R.id.edit_text_input)
editTextInput.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
actionWithTextAfterEnter()
return@OnKeyListener true
}
false
})
}
// method to add Button to addFragment
private fun actionWithTextAfterEnter() {
when (tabLayout.selectedTabPosition) {
0 -> addTabActions()
1 -> removeTabActions()
}
}
private fun tabChanged(numOfTab: Int) {
when (numOfTab) {
0 -> switchedToAddTab()
1 -> {
val isNull = (findViewById<LinearLayout>(R.id.ll_fragment_remove) != null)
Toast.makeText(this, isNull.toString(), Toast.LENGTH_SHORT).show()
if (findViewById<LinearLayout>(R.id.ll_fragment_remove) != null) {
switchedToRemoveTab()
}
}
}
}
private fun switchedToAddTab() {
return
}
private fun switchedToRemoveTab() {
val layout = findViewById<LinearLayout>(R.id.ll_fragment_remove)
// removes all widget from add fragment
layout.removeAllViews()
// remake all widget to add fragment from collection
for (value in usedBarcodeItems.values) {
layout.addView(createButton(value))
}
}
private fun addTabActions() {
// checking if barcode is in mConstantBarcodeMap
val etText : String = bindingMain.editTextInput.text.toString()
val barcode = etText.dropLast(1)
val isInBarcodeMap : Boolean = mConstantBarcodeMap.containsKey(barcode)
val isInBarcodeItemMap: Boolean = usedBarcodeItems.containsKey(barcode)
val layout = findViewById<LinearLayout>(R.id.ll_fragment_add)
if (isInBarcodeMap && !isInBarcodeItemMap) {
usedBarcodeItems[barcode] = mConstantBarcodeMap[barcode].toString()
// removes all widget from add fragment
layout.removeAllViews()
// remake all widget to add fragment from collection
for (value in usedBarcodeItems.values) {
layout.addView(createButton(value))
}
} else if (isInBarcodeMap && isInBarcodeItemMap) {
showWarningToast("This Item is Already on the List!")
} else if (!isInBarcodeMap) {
showWarningToast("This Item is not in Barcode List!")
}
bindingMain.editTextInput.text.clear()
}
private fun removeTabActions() {
return
}
private fun createButton(buttonText : String) : Button {
// declare and configure button widget
val buttonItem = MaterialButton(this)
val params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
LinearLayoutCompat.LayoutParams.MATCH_PARENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT)
params.setMargins(20, 10, 20, 10)
buttonItem.layoutParams = params
buttonItem.text = buttonText
buttonItem.textSize = 20f
buttonItem.setTextColor(Color.BLACK)
buttonItem.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow_500))
return buttonItem
}
private fun showWarningToast(warning: String) {
Toast.makeText(this,warning,Toast.LENGTH_LONG).show()
}
}
應用程式錄制:
https://imgur.com/oM9T5Ak
注意真/假吐司:
Toast.makeText(this, isNull.toString(), Toast.LENGTH_SHORT).show()
uj5u.com熱心網友回復:
也許你需要修改viewPager的配接器代碼讓我在這里分享我的代碼帶有標簽布局和片段
這是 TabAdapter 代碼
class TabAdapter : FragmentStateAdapter {
var fragments = arrayListOf<Fragment>()
constructor(fragmentActivity: FragmentActivity, fragments: ArrayList<Fragment>) : super(fragmentActivity) {
this.fragments = fragments
}
constructor(fragmentManager: FragmentManager, lifecycle: Lifecycle, fragments: ArrayList<Fragment>) : super(
fragmentManager,
lifecycle
) {
this.fragments = fragments
}
override fun getItemCount(): Int {
return fragments.size
}
override fun createFragment(position: Int): Fragment {
return fragments[position]
}
}
附加片段
private fun setUpViewPager(fragments: ArrayList<Fragment>, titles: ArrayList<String>) {
fragmentAdapter = TabAdapter(this, fragments)
bindingMain.viewPager2.offscreenPageLimit = fragments.size
bindingMain.viewPager2.adapter = fragmentAdapter
bindingMain.viewPager2.isSaveEnabled = false
TabLayoutMediator(bindingMain.tabLayout, bindingMain.viewPager2) { tab, position ->
tab.text = titles[position]
}.attach()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471133.html
