Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implement Gesture listener on listview item

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.62k
    Comment on it

    This blog gives a simple example to implement Gesture listener ( or horizontal swipe) on ListView item.

    public class MainActivity extends Activity implements OnGestureListener{
        private ListView listView;
        private ArrayList LIST;
        private ListAdapter adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = new ListView(this);
            setContentView(listView);
            LIST = new ArrayList();
            LIST.add("One");
            LIST.add("Two");
            LIST.add("Three");
            adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, LIST);
            listView.setAdapter(adapter);
            final GestureDetector detector = new GestureDetector(this, this);
            listView.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View view, MotionEvent e) {
                    detector.onTouchEvent(e);
                    return false;
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                Toast.makeText(this, adapter.getItem( listView.pointToPosition(Math.round(e1.getX()), Math.round(e1.getY()))).toString(), Toast.LENGTH_SHORT).show();
               // return super.onFling();
            } catch( Exception e ) {
                // do nothing
            }
            return false;
        }
    
        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    
    }
    

    The above code is a full working activity, in which I have implemented OnGestureListener and created an object of GestureDetector . When user swipes on listview item a Toast is shown which contains listview item value. You can use this code to hide or show something like buttons etc or even can do an action.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: