0

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.

PFranchise
  • 6,642
  • 11
  • 56
  • 73
  • I'm having a little trouble understanding what you are trying to accomplish. Can you post a screenshot of what you have and what you want, or some code? – Mario Stoilov Dec 11 '13 at 19:07
  • @MarioStoilov Sure thing. Edited with code and additional notes. – PFranchise Dec 11 '13 at 19:07
  • I found this solution, which I am going to attempt: http://stackoverflow.com/a/2397869/361815 – PFranchise Dec 11 '13 at 19:14
  • So you basically you have a page in 2 parts: The first (top) part is static and doesn't move The second (bottom) part is scrollable and in it there are views and one of them dynamically expands(other views are added)? – Mario Stoilov Dec 11 '13 at 19:21
  • Using the solution I linked, I was able to get this to work. – PFranchise Dec 11 '13 at 19:30
  • Well I would recommend a listview, but if you say you cannot place a listview in a scrollview, than the solution in your comment should work – Mario Stoilov Dec 11 '13 at 19:30

1 Answers1

1

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!

Evan Bashir
  • 5,501
  • 2
  • 25
  • 29