Reputation: 183
The problem I have with ListView
is that only the ListView is scrollable and not the whole screen.
For example, if I have a layout like the one below, only the ListView part of the screen is scrollable, whereas I want the whole screen to scroll:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="24dp"
android:paddingBottom="24dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum."
/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Is it possible to populate a LinearLayout
, like the one above, in order to make the whole screen scrollable?
Upvotes: 0
Views: 932
Reputation: 3029
Yes it is possible.
Easy way - Wrap everything in ScrollView
its ugly, not recommended but could work.
Much better way - ListView
has addHeaderView(View v)
and addFooterView(View v)
If you want to have something above use header and if you want something below just use footer. In your case take these two TextViews
to new layout file. Inflate it like here
LayoutInflater.from(context).inflate(R.layout.new_created_layout_with_these_two_text_views,null)
and add created view as header.
Upvotes: 1
Reputation: 1973
There are two issues with what you are trying to do
1) You cannot scroll a LinearLayout
. In order to scroll a LinearLayout
, you must insert a ScrollView
as its first and only child, then put all your child views inside the ScrollView
2) What you are trying to do will not work. ScrollView
and ListView
are just a window into the content. The actual ListView
itself is only as large as the space it occupies on screen, rather than the size of the content contained within it. If you want the content of the ListView
and the TextView
's to scroll together, you should either add the TextView
's into the ListView
, or add a wrapping view as the ListView
's header view. Your other option is to not use a ListView
at all, and just append all children to your ScrollView
programatically.
Note: You should not use a ScrollView
AND a ListView
. Pick one approach and stick to it.
Upvotes: 0
Reputation: 13932
There is an easier way to handle this, ListView provides a addHeaderView(View)
method so you can do this:
Create a header_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum."/>
</LinearLayout>
then have your main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="24dp"
android:paddingBottom="24dp">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Then in your MainActivity.java
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// reference your list
ListView listView = (ListView) findViewById(R.id.listview);
// Inflate the header and add the view
View headerView = LayoutInflater.from(MainActivity.this).inflate(R.layout.header_view, null);
listView.addHeaderView(headerView);
}
}
see http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
Upvotes: 1