0
votes

I have a ScrollView and in that scrollview i have a linear layout and in that linear layout i have multiple listviews.

There are 10 listviews below each other.

Now, if i want to scroll within the listview and not the scrollview, i am doing this inside listview's touch listener :-

scrollView.requestDisallowInterceptTouchEvent(true);

and if i want to scroll the whole view so that the listviews which are currently invisiblie come into focus, i am leaving some space on the right side to intercept scrollview's touch listener.

Please see the image below :-

The issue here is that, i want that the whole screen to be covered by listviews and also if i touch on the right side of the listview, the parent's listener should fire and the whole view be scrolled instead of the listview

and if i scroll inside the listview except the right side, then only the listview scroll should work.

I hope i am explanatory enough. enter image description here

1

1 Answers

0
votes

Actually just by your description it already should work. But if you still have problems, here is what i had created just to test your idea:

XML layout file

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="10dp" >

        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ListView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lista1"/>

            <ListView 
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="2"
                android:id="@+id/lista2"/>

            <ListView 
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:id="@+id/lista3"/>

            <ListView 
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:id="@+id/lista4"/> 

            <ListView 
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:id="@+id/lista5"/> 

        </LinearLayout>
</ScrollView>

@EDIT as requested, i removed margin from LinearLayout, but i left padding in ScrollView, and it still works.

OLD NOTE:

note the layout_marginRight = "10dp" inside LinearLayout, it will provide some space on the right side of the screen for ScrollView slider.

Now onCreate() of my activity code:

protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //create list on the run, just for testing
        xxx.add("Objekt 1");
        xxx.add("Objekt 2");
        xxx.add("Objekt 3");
        xxx.add("Objekt 4");
        xxx.add("Objekt 5");
        xxx.add("Objekt 6");
        xxx.add("Objekt 7");

        //connect variables with UI elemetns
        lista1 = (ListView)findViewById(R.id.lista1);
        lista2 = (ListView)findViewById(R.id.lista2);
        lista3 = (ListView)findViewById(R.id.lista3);
        lista4 = (ListView)findViewById(R.id.lista4);
        lista5 = (ListView)findViewById(R.id.lista5);

        //define adapter, one for all lists
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xxx);

        //set it to lists
        lista1.setAdapter(adapter);
        lista2.setAdapter(adapter);
        lista3.setAdapter(adapter);
        lista4.setAdapter(adapter);
        lista5.setAdapter(adapter);

        //this method fixes problem with ListView showing only one element when placed inside ScrollView
        //code below
        setListViewHeightBasedOnChildren(lista1);
        setListViewHeightBasedOnChildren(lista2);
        setListViewHeightBasedOnChildren(lista3);
        setListViewHeightBasedOnChildren(lista4);
        setListViewHeightBasedOnChildren(lista5);

        //set onTouchListener, also one for all lists
        lista1.setOnTouchListener(onTouchListener);
        lista2.setOnTouchListener(onTouchListener);
        lista3.setOnTouchListener(onTouchListener);
        lista4.setOnTouchListener(onTouchListener);
        lista5.setOnTouchListener(onTouchListener);
    }

and onTouchListener()

private OnTouchListener onTouchListener = new OnTouchListener() 
    {
        @Override
        public boolean onTouch(View view, MotionEvent motion) 
        {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    };

In here, just like you've mentioned, i'm disabling ScrollView from intercepting touch event, so it won't react on scroll.

Now method to fix ListView height (if you have this problem too)

public static void setListViewHeightBasedOnChildren(ListView listView) 
    {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;

        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < (listAdapter.getCount() / 2); i++) 
        {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

Everythin combined together works just like you've described.