GridView
and ListView
in an Activity
(one screen). To day, in this small tip, I'll present a simple way to put 2 ListViews
in an Activity
(although this is a "pure design", not recommended, in most of cases, please use ExpandableListView
instead).Warning
RecyclerView
is developed as the alternative widget to build list and grid layout in Android, so this solution maybe obsolete. Please read my newer post to learn how to use RecyclerView
with this design!
ListView
scrolling", set their height expand to max based on their children and finally, put all them in to a ScrollView
. So that, actually we scroll this ScrollView
. Programmatically code for get ListView
dynamic height:
/**
* Set listview height based on listview children
*
* @param listView
*/
public static void setDynamicHeight(ListView listView) {
ListAdapter adapter = listView.getAdapter();
//check adapter if null
if (adapter == null) {
return;
}
int height = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height = height + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(layoutParams);
listView.requestLayout();
}
In the Activity
layout, put all ListViews
into a ScrollView
as root view:
activity_main.xml
Full source code for this main activity:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/asia" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_vertical"
android:text="@string/europe" />
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#4A9C67" >
</ListView>
</LinearLayout>
</ScrollView>
MainActivity.java
Running program, we have this result:package hongthaiit.blogspot.com.multiplelistviews;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView asiaListView;
private ListView europeListView;
private String[] asiaCountries = {"Vietnam", "China", "Japan", "Korea", "India", "Singapore", "Thailand", "Malaysia"};
private String[] europeCountries = {"France", "Germany", "Sweden", "Denmark", "England", "Spain", "Portugal", "Norway"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//locate Views
asiaListView = (ListView) findViewById(R.id.listView1);
europeListView = (ListView) findViewById(R.id.listView2);
//set all Listview adapter
asiaListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, asiaCountries));
europeListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, europeCountries));
//set dynmic height for all listviews
setDynamicHeight(asiaListView);
setDynamicHeight(europeListView);
}
/**
* Set listview height based on listview children
*
* @param listView
*/
public static void setDynamicHeight(ListView listView) {
ListAdapter adapter = listView.getAdapter();
//check adapter if null
if (adapter == null) {
return;
}
int height = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height = height + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(layoutParams);
listView.requestLayout();
}
}
(sorry for having ads)