1

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

GPGVM
  • 5,515
  • 10
  • 56
  • 97
  • Maybe duplicate: [Disable scrolling in listview](http://stackoverflow.com/questions/7611085/disable-scrolling-in-listview) – pinyin_samu Nov 12 '12 at 14:47
  • Yes I saw those. As per my question do I really have to create a custom ListView object just to disable scrolling which should be a standard boolean property of the object? – GPGVM Nov 12 '12 at 14:55
  • No, if you override the onTouchListener like in the second answer of the question from the link of my previous comment, the scrolling functionality of the listview is disabled. I use this method also if needed. – pinyin_samu Nov 12 '12 at 15:05
  • Ummm yes I re-read the thread after making my comment and saw that...but honestly now I'm struggling with where to put it. I thought to put it right after I get a handle on the list view. Intellisense tells me it is invalid. It gives me 8 options the one that looks best is SetOnScrollListener. I'm adding the code snippet to the OP. – GPGVM Nov 12 '12 at 15:22
  • Do you have an import of View.OnTouchListener at the top of your code? `import android.view.View.OnTouchListener;` I'll post a little test code below that works for me – pinyin_samu Nov 12 '12 at 15:35
  • There ya go. I didn't know there was a separate namespace to be imported. I had view.View but not OnTouchListener. – GPGVM Nov 12 '12 at 16:07

1 Answers1

2

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

pinyin_samu
  • 174
  • 1
  • 12