Listview disable internal scrolling on Android

你说的曾经没有我的故事 提交于 2019-12-06 12:49:20

问题


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:

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>

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7017432/listview-disable-internal-scrolling-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!