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);
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");
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);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xxx);
lista1.setAdapter(adapter);
lista2.setAdapter(adapter);
lista3.setAdapter(adapter);
lista4.setAdapter(adapter);
lista5.setAdapter(adapter);
setListViewHeightBasedOnChildren(lista1);
setListViewHeightBasedOnChildren(lista2);
setListViewHeightBasedOnChildren(lista3);
setListViewHeightBasedOnChildren(lista4);
setListViewHeightBasedOnChildren(lista5);
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.