gpt4 book ai didi

java - 即使禁用了滑动,选项卡仍会随着 TabLayout 上的触摸移动而继续更改

转载 作者:行者123 更新时间:2023-12-02 12:44:58 25 4
gpt4 key购买 nike

我正在开发一个使用 tablayout 并包含 3 个指南的应用程序。第一个用于一般用户信息,第二个指南用于附加信息,最后一个指南用于用户在屏幕上绘图。是第三家有问题的公司,即使禁用了滑动,在进行绘图 Action 时,用户最终还是返回到上一个选项卡。所以,我想知道如何在这个屏幕上禁用滑动的移动,即使在标签之间的移动它已经被滑动移动禁用了,而不会使绘图变得不可能。

需要注意的一点是,当您从下向上或上下开始绘制时,即使您稍后要去某个地方,您也可以在不改变指导的情况下进行绘制。但是,如果您开始绘制

我注意到一件事,当我开始从底部向上或上下绘制时,我可以在不更改选项卡的情况下进行绘制,即使之后我去任何地方。但是,如果我从任何地方开始绘图,他就会明白这不是绘图,而是我想更改选项卡。我怎样才能解决这个问题?

class Main2Activity : AppCompatActivity() {

@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
val viewPager: ViewPager = findViewById(R.id.viewPager)
viewPager.adapter = sectionsPagerAdapter
viewPager.setOnTouchListener { _, _ -> return@setOnTouchListener true }
val tabs: TabLayout = findViewById(R.id.tabs)
tabs.setupWithViewPager(viewPager)

}
}

fragment .xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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="com.ufrn.dissertation.activities.Main2Activity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="@dimen/appbar_padding"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

图像 fragment
class ImagesFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_imagens, container, false)
...CODE DRAWING...

从底部/向上或向上/向下开始绘制



从左/右或右/左开始绘制

最佳答案

迁移到 viewPager 的第 2 版。为此,请按照以下步骤操作:
- 添加必要的实现:

implementation 'com.google.android.material:material:1.3.0-alpha01'
implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'

- 更新你的布局:
  <androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchorGravity="bottom"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
- 更新您的适配器:
class SectionsPagerAdapter(
private val activity: FragmentActivity
) : FragmentStateAdapter(activity) {

override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> ProfileFragment()
1 -> TreatmentFragment()
else -> ImagesFragment()
}
}

fun getPageTitle(position: Int): CharSequence =
activity.resources.getString(TAB_TITLES[position])

override fun getItemCount(): Int = TAB_TITLES.size

companion object {
private val TAB_TITLES = arrayOf(
R.string.tab_text_1,
R.string.tab_text_2,
R.string.tab_text_3
)
}
}
- 您的 PageViewModel:
class PageViewModel : ViewModel() {

private val _index = MutableLiveData<Int>()
val text: LiveData<String> = Transformations.map(_index) {
"Hello world from section: $it"
}

fun setIndex(index: Int) {
_index.value = index
}
}
- 您的占位符 fragment :
class PlaceholderFragment : Fragment() {

private lateinit var pageViewModel: PageViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_main2, container, false)
val textView: TextView = root.findViewById(R.id.section_label)
pageViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}

companion object {

private const val ARG_SECTION_NUMBER = "section_number"

@JvmStatic
fun newInstance(sectionNumber: Int): PlaceholderFragment {
return PlaceholderFragment().apply {
arguments = Bundle().apply {
putInt(ARG_SECTION_NUMBER, sectionNumber)
}
}
}
}
}
- 还有你的 MainAcitivity:
class MainActivity : AppCompatActivity() {


@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val sectionsPagerAdapter = SectionsPagerAdapter(this)
val viewPager = findViewById<ViewPager2>(R.id.viewPager).apply {
adapter = sectionsPagerAdapter
isUserInputEnabled = false
}
val tabs: TabLayout = findViewById(R.id.tabs)
TabLayoutMediator(tabs, viewPager, true) { tab, position ->
tab.text = sectionsPagerAdapter.getPageTitle(position)
}.attach()

}
}
这样就可以解决你的问题

关于java - 即使禁用了滑动,选项卡仍会随着 TabLayout 上的触摸移动而继续更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62474423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com