0

New to Android development. I have one page(layout) that includes ImageView, TextView and ListView, I want to everything is scrollable. So I put a Scrollview as container for all of them, but it doesn't work as the ListView cannot be put inside the Scrollview. Here are what I am thinking: 1) Somebody said I can put the ListView header to includes ImageView and TextView, but the thing is that my ListView maybe empty... 2) Is there any other views, containers can do the similar thing? For example I don't have to use ListView as there is another similar container...? Thanks, Patrick

MicrorFrank
  • 156
  • 1
  • 2
  • 12

4 Answers4

0

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. Try something like this: Put a RelativeLayout container which contains your components and put the RelativeLayout inside the ScrollView.

<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout>
     ...
     ...
     your components
     ...
    </RelativeLayout>
</ScrollView>
Jain
  • 46
  • 1
  • 7
  • Doesn't work. As the ListView is also scrollable, it only shows the first items, no matter how many children the scrollview has... – MicrorFrank Aug 08 '15 at 13:10
  • The ScrollView should have only one child anyway. I am not able to understand what exactly you want to achieve. May be you want this, have a look : http://stackoverflow.com/a/19311197/5201396 – Jain Aug 08 '15 at 13:19
0

enter your code to this format:

 <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="123dp" >
        </ListView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="71dp"
            android:layout_marginTop="48dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>
</ScrollView>
0

You can put textView and imageView into separated layout e.g. header.xml and set it as listview header in some controller class (activity or fragment):

    ListView list = ListView.class.cast(findViewById(R.id.list));
    View header = getLayoutInflater().inflate(R.layout.header, null, false);
    list.addHeaderView(header);
    list.setAdapter(adapter());

but in this solution you should set adapter after header view.

UPD:

1) If my ListView is empty, is there a way to handle?

You can understand whether empty or not with source adapter getCount() method

2) The 'Header' will also scrollable with the whole page or stacked there?

Header scrolling as zero listview item (above first)

Kirill
  • 7,580
  • 6
  • 44
  • 95
0

my code this and worked `public class MainActivity extends ListActivity implements SearchView.OnQueryTextListener {

String name[]={"sajad","sara","mahya","fatemeyas","majid"};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adapter=new ArrayAdapter<String>(this,R.layout.activity_main, name);
    setListAdapter(adapter);
}`