I have an app in which, the main page, is 100% occupied by a ListView that contains an ArrayList of objects. My goal is that when I slide my finger from right to left on that ListView, I will go to a new window (activity).
I was guided by this tutorial: link and if I reduce the size of the ListView to be able to touch the "background "on the screen, it works. How could I make it detect that glide on the ListView?
My relevant code in the MainActivity class:
public class MainActivity extends AppCompatActivity {
private GestureDetectorCompat gestureDetector;
ListView character_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (audios_megumin.isEmpty()) {
fill_audios_list();
}
if (anime_characters_list.isEmpty()) {
fill_anime_character_list();
}
gestureDetector = new GestureDetectorCompat(this, new LearnGesture());
character_list = findViewById(R.id.list_view_main_characters);
character_list.setAdapter(new CharacterListAdapter(this, R.layout.main_list_item, anime_characters_list));
// character_list.setAdapter(new Adapter(this, anime_characters_list));
character_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
AnimeCharacter anime_character = anime_characters_list.get(position);
Intent intent = new Intent(getBaseContext(), CharacterAudiosActivity.class);
intent.putExtra("ANIME_CHARACTER", anime_character);
startActivity(intent);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event){
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
class LearnGesture extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY){
if (event1.getX() > event2.getX()){
Intent intent = new Intent(
MainActivity.this, DonationActivity.class);
startActivity(intent);
}
return true;
}
}
There is some way to detect the displacement on the listview (or one of its items since the new activity does not vary depending on these)