简体   繁体   中英

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

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 ViewPager2

To disable swiping in ViewPager2

myViewPager2.setUserInputEnabled(false);

To enable swiping in 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?

Migrate to version 2 of the viewPager. 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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