2

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

Tecnologia da Net
  • 215
  • 3
  • 7
  • 23

2 Answers2

1

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?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • even though I imported the lib implementation 'androidx.viewpager2: viewpager2: 1.1.0-alpha01' changing the layout as suggested, it is not possible to find .setUserInputEnabled (false). Informa: unresolved reference. Do you know how to fix it? – Tecnologia da Net Jun 23 '20 at 18:27
  • @TecnologiadaNetc you can find a complete example of how to use `ViewPager2` from here https://stackoverflow.com/a/54643817/7666442 i have added detailed answer please take a look – AskNilesh Jun 24 '20 at 03:08
1

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

Carlos Diego
  • 348
  • 5
  • 20