Fragments UI's overlapping

Viewed 53

I'm creating a shopping app where the user can choose next product from the fragment. That will open the next fragment of the same class with more products and so on. I want to use swipe to dismiss behaviour, so when I swipe my finger down I dissmiss the current fragment. It's essential that while swiping the current fragment the user will be able to see the fragment underneath, so I can only use add instead of replace. Here's the problem: When I use add in my fragment manager, the created fragments UI's overlap, which makes the user swipe multiple fragments at once and not show swiping animation. The fragment uses one xml layout so it shares id across all fragments so I have no way of controlling which layouts are disabled. How do I make the user only swipe one fragment/disable touching fragments underneath? The swipe to dissmis is working well with replace but that's not the point. Here's my code.

Activity:

class PullDismissActivity : AppCompatActivity(), PullDismissLayout.Listener {

private lateinit var viewModel: StackingFragmentsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_pulldismiss)
     viewModel = ViewModelProvider(this).get(StackingFragmentsViewModel::class.java)
    supportFragmentManager.beginTransaction()
        .add(R.id.pull, SwipeFragment(), null)
        .addToBackStack(null) // name can be null
        .commit()
    val layout = findViewById<PullDismissLayout>(R.id.pull)
    layout.setListener(this)
}
override fun onDismissed() {
    viewModel.popBackStack()
    supportFragmentManager.popBackStack()
}

override fun onShouldInterceptTouchEvent(): Boolean {
    return false
}

Fragment:

class SwipeFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val viewModel = ViewModelProvider(requireActivity()).get(StackingFragmentsViewModel::class.java)
    viewModel.addFragment(this.hashCode())
    val fragid = view.findViewById<TextView>(R.id.fragid)
    fragid?.text = System.currentTimeMillis().toString()
    val button = view.findViewById<Button>(R.id.button)
    button.setOnClickListener {
        requireActivity().supportFragmentManager.beginTransaction()
                .add(R.id.pull, SwipeFragment(), null)
                .addToBackStack(null)
                .commit()
    }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment,container,false)
}
0 Answers
Related