Admiral Land Admiral Land - 5 days ago 6
Android Question

Swipe does not work in second activity

Swipe does not work in second activity:
I create two activities and use swipe library.
First activity swipes well, but second doesn't work.
Here some piece of code:

private var _swipe: Swipe = Swipe();

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_1)
_swipe.addListener(object :SwipeListener {
override fun onSwipedLeft(event: MotionEvent?) {
val intent = Intent(this@FirstActivity,SecondActivity::class.java)
startActivity(intent)
}
}
}


The second activity have the same code, but it named
second_activity
.
Swipe in first activity works well, it redirects to second, but then swipe does'n work! Not to left, not to right and so on.

Can you help me?

Second activity:

public class SecondActivity : MvpActivity<ISecondView, SecondPresenter> (),ISecondView {

private var _manager: PlayerManager = PlayerManager();
/**
* Instantiate a presenter instance

* @return The [MvpPresenter] for this view
*/
override fun createPresenter(): SelectPresenter {
return SecondPresenter(_manager);
}

private var _swipe: Swipe = Swipe();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_select_2)

val realm = Realm.getDefaultInstance()

val players = realm.where(Player::class.java).findAll()


val arrayList = ArrayList<String>(players.size)
for (player in players) {

arrayList.add(player.Name);
}


val adapter = ArrayAdapter(applicationContext, android.R.layout.simple_list_item_checked, arrayList)

val lvArr = findViewById(R.id.ListView) as ListView;
lvArr.choiceMode= ListView.CHOICE_MODE_SINGLE;
lvArr.adapter = adapter

lvArr.setOnItemClickListener { adapterView: AdapterView<*>, view1: View, i: Int, l: Long ->
//select elem
}

_swipe.addListener(object : SwipeListener {
override fun onSwipedLeft(event: MotionEvent?) {
Toast.makeText(applicationContext, "swiped left", Toast.LENGTH_SHORT).show();
val intent= Intent(this@SecondfActivity,ThirdActivity::class.java);
startActivity(intent);
}

override fun onSwipingLeft(event: MotionEvent?) {
Toast.makeText(applicationContext, "swiping left", Toast.LENGTH_SHORT).show();
val intent= Intent(this@SecondActivity,ThirdActivity::class.java);
startActivity(intent);
}
}
})
}
}


I remove some methods implementations. When i try to swipe- nothing happens.

Answer

I'm an author of the library you're using. Are you sure you're overriding dispatchTouchEvent(event) method and calling a swipe.dispatchTouchEvent(event) method inside it in both activities? You haven't mentioned it in the code snippets, but it's required to make everything work.

  override fun dispatchTouchEvent(event: MotionEvent): Boolean {
    (swipe as Swipe).dispatchTouchEvent(event)
    return super.dispatchTouchEvent(event)
  }

3 steps regarding library configuration are described in README.md file on GitHub.

Regards,

Piotr

Comments