简体   繁体   English

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

[英]Even with swiping disabled, tab continues to be changed with touch movement on TabLayout

I am developing an application that uses the tablayout and contains 3 guides.我正在开发一个使用 tablayout 并包含 3 个指南的应用程序。 The first is for general user information, the second guide is for additional information and the last guide is for the user to draw on the screen.第一个用于一般用户信息,第二个指南用于附加信息,最后一个指南用于用户在屏幕上绘图。 is a third company that has problems, even with swiping disabled, when making drawing movements, the user ends up returning to the previous tab.是第三家存在问题的公司,即使禁用了滑动,在进行绘图动作时,用户最终还是返回到上一个选项卡。 So, I would like to know how to disable the movement of the swip on this screen, even with the movement between tabs it is already disabled with the sliding movement, without making it impossible for the drawing to be made.所以,我想知道如何在这个屏幕上禁用滑动的移动,即使标签之间的移动它已经被滑动移动禁用了,而不会使绘图变得不可能。

One thing to note is that when you start drawing from the bottom up or up and down, you can draw without changing as guides, even if you are going somewhere later.需要注意的一点是,当您从底部向上或上下开始绘制时,即使您稍后要去某个地方,您也可以在不更改引导的情况下进行绘制。 However, if you start drawing但是,如果您开始绘制

One thing I noticed, that when I start to draw from the bottom up or up and down, I can draw without changing the tabs, even if I go anywhere afterwards.我注意到的一件事是,当我开始从底部向上或上下绘制时,我可以在不更改选项卡的情况下进行绘制,即使我之后的任何地方都是 go。 However, if I start drawing anywhere, he understands that it is not the drawing but that I want to change tabs.但是,如果我从任何地方开始绘图,他就会明白这不是绘图,而是我想更改选项卡。 How can I fix this?我怎样才能解决这个问题?

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

<?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>

Image 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...

Drawind starting from bottom/up or up/down从底部/向上或向上/向下开始绘制

Drawind starting from left/right or right/left从左/右或右/左开始绘制

You should migrate from ViewPager to ViewPager2您应该从 ViewPager 迁移到 ViewPager2

You should use你应该使用

<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        app:layout_anchor="@id/tabs"
        app:layout_anchorGravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
/>

instead of代替

<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"/>

There are built-in methods to disable swiping in ViewPager2ViewPager2中有禁用滑动的内置方法

To disable swiping in ViewPager2在 ViewPager2 中禁用滑动

myViewPager2.setUserInputEnabled(false);

To enable swiping in ViewPager2在 ViewPager2 中启用滑动

myViewPager2.setUserInputEnabled(true);

For more information please check out below link欲了解更多信息,请查看以下链接

if you still want to use ViewPager then please take look in this post How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?如果您仍想使用ViewPager ,那么请查看这篇文章如何通过在 ViewPager 中用手指滑动来禁用分页但仍然能够以编程方式滑动?

Migrate to version 2 of the viewPager.迁移到 viewPager 的第 2 版。 To do this, follow the steps below:为此,请按照以下步骤操作:

- Add the necessary implementations: - 添加必要的实现:

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

- update your layout: - 更新你的布局:

  <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" />

- Update yout adapter: - 更新你的适配器:

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
    )
}
}

- Your PageViewModel: - 您的 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
}
}

- Your PlaceholderFragment: - 你的占位符片段:

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)
            }
        }
    }
}
}

- And your MainAcitivity: - 还有你的 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()

}
}

That way it will fix your problem这样就可以解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM