moonlightcheese
moonlightcheese

Reputation: 10590

Listview disable internal scrolling on Android

I have a layout that I need to scroll in its entirety. The layout contains a listview a the bottom, which is causing some discord. Here's what's happening: listviewderp

So you can see that the bottom scrolls in its own little world, and I need to disable that so it grows and extends the entire layout for scrolling. I've tried encapsulating it all in a <scrollview><linearlayout>mystuff</linearlayout></scrollview>, to no avail. I've tried infinite combinations with "match_parent" and "wrap_content" layout_heights. I really need that listview to grow outward without putting itself in its own scrolling world.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <TableLayout android:layout_span="2"
                android:layout_width="fill_parent"
                android:layout_weight="1">
                <TableRow android:gravity="center">
                    <Button android:id="@+id/vin_btn"
                        android:width="@dimen/inb_btn_w"
                        android:height="@dimen/inb_btn_h"
                        android:text="@string/inb_vin_btn"
                        android:textSize="@dimen/inb_txt_sz" />
                    <Button android:id="@+id/clear_btn"
                        android:width="@dimen/inb_btn_w"
                        android:height="@dimen/inb_btn_h"
                        android:text="@string/inb_sc_btn"
                        android:textSize="@dimen/inb_txt_sz" />
                    <Button android:id="@+id/transmit_btn"
                        android:width="@dimen/inb_btn_w"
                        android:height="@dimen/inb_btn_h"
                        android:text="@string/inb_tr_btn"
                        android:textSize="@dimen/inb_txt_sz"/>
                </TableRow>
            </TableLayout>
        </TableRow>
        <TableRow android:gravity="center_vertical"
            android:layout_width="fill_parent">
            <TextView
                android:gravity="right"
                android:id="@+id/text_cust"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_cust"/>
            <Spinner
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:id="@+id/cust_spn"/>
        </TableRow>
        <TableRow android:layout_width="fill_parent">
            <TextView
                android:gravity="right"
                android:id="@+id/text_drv"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_drv"/>
            <EditText
                android:id="@+id/drv_in"
                android:inputType="number"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:singleLine="true"/>
        </TableRow>
        <TableRow android:gravity="center_vertical">
            <TextView
                android:gravity="right"
                android:id="@+id/text_prd"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_prd"/>
            <Spinner
                android:id="@+id/prd_spn"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
        </TableRow>
        <TableRow android:gravity="center_vertical">
            <TextView
                android:gravity="right"
                android:id="@+id/text_scale"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_scale"/>
            <Spinner
                android:id="@+id/scale_spn"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
        </TableRow>
        <TableRow android:gravity="center_vertical">
            <TextView
                android:gravity="right"
                android:id="@+id/text_dir"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_dir"/>
            <Spinner
                android:id="@+id/dir_spn"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
        </TableRow>
        <TableRow>
            <TextView
                android:gravity="right"
                android:id="@+id/text_make"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_make"/>
            <EditText
                android:id="@+id/make_in"
                android:singleLine="true"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
        </TableRow>
        <TableRow>
            <TextView
                android:gravity="right"
                android:id="@+id/text_tag"
                android:textSize="@dimen/inb_txt_sz"
                android:text="@string/inb_tag"/>
            <EditText
                android:id="@+id/tag_in"
                android:singleLine="true"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
        </TableRow>
    </TableLayout>
    <ListView android:id="@+id/vin_list"
            android:isScrollContainer="false"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_width="match_parent" />
</LinearLayout>

the row layout is defined below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="4dip">
    <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_vertical|left">
        <TableRow>
            <TextView android:id="@+id/vin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"/>
        </TableRow>
        <TableRow>
            <TextView android:id="@+id/desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

Upvotes: 1

Views: 6959

Answers (3)

ol_v_er
ol_v_er

Reputation: 27284

Other and better answer (it was late yesterday...)

You can use a simple ListView and add a header TableLayout:

Create a specific layout for the TableView.

In the onCreate method:

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View headerView = inflater.(R.layout.header, mListView, false);
mListView.addHeaderView(headerView);

With this method you will have the header containing the table view automatically managed by the ListView and showed when the user scroll to the beginning of the ListView.

You keep the benefice of cell reuse from the standard adapter.

Upvotes: 3

ol_v_er
ol_v_er

Reputation: 27284

First of all, add a ScrollView as root item for your layout it will allow you to scroll between all the items in your layout (it will be necessary for the existing TableLayout on small screen phones).

Then replace the ListView by a vertical LinearLayout and inflate manually as much rows as you need with a similar method:

LinearLayout llContainer = (LinearLayout) findViewById(R.id.yourBottomLinearLayout);

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());

for (Item item : mListItems){           
    LinearLayout llItem = (LinearLayout) inflater.inflate(R.layout.row, null);
    TextView vin = (TextView) llItem.findViewById(R.id.vin);
    vin.setText(item.getFirstText());
    TextView desc = (TextView) llItem.findViewById(R.id.desc);
    desc.setText(item.getSecondText());
    // To know wich item has been clicked
    llItem.setTag(item.getId());
    // In the onClickListener just get the id using getTag() on the view
    llItem.setOnClickListener(this);
    llContainer.addView(llItem);
}

Your ScrollView will increase size as much as needed and you will be able to scroll on the entire screen.

Upvotes: 5

jorgenfb
jorgenfb

Reputation: 2237

You could simply put all your rows in a linear layout. I assume you already have a row-layout defined. All you need to do is loop through you data and inflate one row for each item. A listview is suppost to scroll and uses a lot of tricks to consume less resources than displaying all items at once.

Upvotes: 0

Related Questions