简体   繁体   中英

Android scrollable results view with scrollable header section

I have an activity where the bottom half of the page is a scrollable results view. At the top of the results view, is a relativelayout with some text and a button. This button will make new items appear in this relative layout to refine the search. This part is all working. However, below this relative layout, I need to add a list of search results. I had this working with a listview, but since I need the entire bottom of the portion of the page (including that header relative layout) scrollable and since you cant have a listview in a scrollview, this wont work.

So, I was hoping I could do something like make another view, populate it with the result data for each result item, and programatically add them below the relative layout. Perhaps just having a linearlayout beneath that header relative layout.

Am I on the right track with this thinking? What is the right way to do this?

If it matters, my app has a min sdk version of 8. I am using the support libraries.

EDIT: here is my current code:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DealerFragment" 
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">

    <ImageView
        android:id="@+id/topBar" 
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        />

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/CD_logo"
        android:src="@drawable/logo" />

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <ScrollView
        android:id="@+id/scrollBox"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:background="#00000000" >

        <RelativeLayout
            android:id="@+id/scrollViewRelativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000">

            <RelativeLayout 
                android:id="@+id/searchHeaderBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:background="#c0000000">

                <TextView
                    android:id="@+id/near"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/near"
                    android:textColor="#ffffff" />

                <TextView
                    android:id="@+id/nearZip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/near"
                    android:layout_marginLeft="4dp"
                    android:layout_centerVertical="true"
                    android:text="78749"
                    android:textColor="#ffffff" />

                <ImageView
                    android:id="@+id/narrowSearchImage"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/filter"
                    android:contentDescription="@string/CD_Narrow_Results"/>

                <TextView 
                    android:id="@+id/narrowSearchText"
                    android:layout_toLeftOf="@+id/narrowSearchImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/narrow_results"
                    android:textColor="#ffffff"/>
            </RelativeLayout>
            <LinearLayout 
                android:id="@+id/resultsLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/searchHeaderBox"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:background="#00000000">
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

Basically, I want to know if I should just try to add my result items (which I currently have as a seperate .XML file) and if so how I do that. Or if there is some other device I should be using to accomplish this. My goal, is to have everything in that scrollview to scroll. I don't know how many result items I will have until the page is loaded.

How about, we actually put the ListView in a ScrollView !

I know people say you can't, but I found a way.


1. Wrap the layout that contains your ListView , with a ScrollView .

2. Add this to the class with the layout containing your ListView . Make sure to place it after you set your adapter. The only thing you need to change is dp to the height of your ListView row layout.

int listViewAdapterSize = yourListView.getAdapter().getCount();
LayoutParams listViewParams = yourListView.getLayoutParams();

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) ((listViewAdapterSize * dp) * scale + 0.5f);
params.height = pixels;

Let me know if you have any problems!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM