Johann
Johann

Reputation: 29877

Create a scrollable LinearLayout without a ScrollView

I spent a good part of a day looking into scrolling content in Android. However, in my app I need to apply a custom type of scrolling that contains among other things a listview. A listview has its own scrolling and a scrollview should never be used with a list view together.

But there must be some intrinsic scroll functionality for views. The documentation on View does indicate support but the methods provided do not seem to provide any listeners to detect scrolling.

A scrollview also is built on a FrameLayout which would also be problematic in my app.

You can make a LinearLayout scrollable with:

android:isScrollContainer="true"

but this doesn't seem to do anything.

In fact you can even set the scrollbars for a LinearLayout:

android:scrollbars="vertical"

but that also doesn't seem to do anything.

I don't want to control scrolling but rather have a listener that detects scrolling on the screen regardless whether a listview, framelayout or any other control is visible. I guess what I am looking for is writing my own custom ScrollView control but without the limitations of its FrameLayout and all the unnecessary overhead that I don't need. Any suggestions?

Upvotes: 2

Views: 2913

Answers (3)

Chandana_Raj
Chandana_Raj

Reputation: 116

Actually after doing some research, I come up with a solution for this problem:

At first I want to explain the problem in a very simple way.

1.LinearLayout will be scrollable.
2.To do this we can use ScrollView but sometimes we need to use ListView inside LinearLayout.
3.We know that inside ScrollView we cannot use another scrollview like ListView

How to solve that?

ListView is scrollable inherently so we can add header and footer in the ListView. As a conclusion:

1.Create Layout header.xml and footer.xml and list.xml
2.Find the ListView reference from list.xml in the main activity and dynamically add header and footer in the ListView reference.

Upvotes: 1

Johann
Johann

Reputation: 29877

Here is what seems like a viable solution which I tested out but not thoroughly. Create a custom control that extends a LinearLayout and then override the dispatchTouchEvent:

  @Override
  public boolean dispatchTouchEvent(MotionEvent event)
  {
    MotionEvent vtev = MotionEvent.obtain(event);

    final int actionMasked = event.getActionMasked();

    float x = event.getRawX();
    float y = event.getRawY();

    switch (actionMasked)
    {
      case MotionEvent.ACTION_DOWN:

        break;
      case MotionEvent.ACTION_MOVE:
        int z = 0;
        z++;
        break;
      case MotionEvent.ACTION_UP:
        break;
      case MotionEvent.ACTION_CANCEL:

        break;
      case MotionEvent.ACTION_POINTER_DOWN:
      {
        final int index = event.getActionIndex();

        break;
      }
      case MotionEvent.ACTION_POINTER_UP:

        break;
    }

    boolean ret = super.dispatchTouchEvent(event);
    return ret;
  }
}

You can then place child controls inside this custom LinearLayout and detect motion events, even when ListViews and ScrollViews are present. This doesn't mess with their own scrolling.

Upvotes: 0

Santosh Kathait
Santosh Kathait

Reputation: 1444

On way to resolve above problem is to have 2 ScrollView, one is the outer ScrollView that contain all the view and other inner Scrollview instead of listview and adding the items inside LinearLayout dynamically(Inside inner ScrollView)

Upvotes: 1

Related Questions