Question

I have a simple app that I wrote for the purposes of learning Java / android coming from the .Net world. In this app I have a ListView. The ListView is bound to a collection and displays everything as it should. I have a next & previous button that allow the user to navigate and these work as well.

What I am wanting to stop is the ability of the user to scroll through the list. I want them to use the navigation buttons. I was thinking there must be some event that is raised when the user scrolls. I was going to intercept that event and just "swallow" it. In looking for those events I came across the SetScrollContainer(false). Problem is that it doesn't seem to matter do anything. I can still scroll vertically.

So my ultimate question whether by using the SetScrollContainer or intercepting events or another option is how best to disable user scrolling in a ListView?

I also came across an obscure article saying to implement a custom ListView which sounds like inheritance. I am comfortable with the concept but not sure being so new to android & java if this complexity is really needed?

TIA JB

Code as discussed in comments below:

//Get a handle on our ListView
lstvw_LiftData = (ListView)findViewById(R.id.lstvw_LiftData);

lstvw_LiftData.setOnTouchListener(new OnTouchListener() 
  {

      public boolean onTouch(View v, MotionEvent event) 
      {
          if (event.getAction() == MotionEvent.ACTION_MOVE) 
          {
              return true; // Indicates that this has been handled by you and will not be forwarded further.
          }
          return false;
      }
  }
);

setOnTouchListener error

Was it helpful?

Solution

Code I've written in a little testproject and works for me to disable scrolling on list view:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = (ListView) findViewById(R.id.listV);
        String[] items = new String[10];

        for (int i = 0; i < 10; i++) {
            items[i] = "Item " + (i+1);
        }
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
        lv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    return true;
                }
                return false;
            }

        });
    }
}

Have fun.

Kr

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top