Can a ScrollView Work with a clickable TableLayout?
I'm trying to create a list of results from a search, and it needs to be scrollable, but I also need to be able to click on a result to go to another activity. Here is my code, any help is appreciated.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout android:id="@+id/resultsLayout"
android:layout_height="match_parent" android:layout_width="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical">
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine" android:id="@+id/tlli1">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="MUDD PUPPIES SELF SERVE DOG " android:ellipsize="none"
android:scrollHorizontally="false"/>
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="20091345" />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli2">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="MUDD, ANGELA G " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2008022979 3383 6" />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli3">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="GRABAU, ANDREW S " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2011003340 3771 110 " />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli4">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layo开发者_StackOverflow社区ut_height="wrap_content"
android:text="S M E T INC " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1987016628 652 590 " />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli5">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="A GOOD START INC " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="20040649" />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli6">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="AGUILAR, JOHN D " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2001015672 1747 306 " />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli7">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="THEIS, ARLENE C " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1998020639 1439 544 " />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli8">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="R-E INVESTMENT CO " />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1985005167 530 925 " />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli9">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Man" />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Machine" />
</TwoLineListItem>
</TableRow>
<TableRow>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine" android:id="@+id/tlli10">
<TextView android:id="@android:id/text1" android:gravity="left"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Man" />
<TextView android:id="@android:id/text2" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Machine" />
</TwoLineListItem>
</TableRow>
</TableLayout>
You should consider using a ListView for this sort of task, rather than rolling your own ScrollView.
Edit:
I think you're approaching this the wrong way. You're hardcoding the results of the search query in XML? That tells me that you aren't really doing a search query at all.
What you should be doing is having your data in an array or database and then query it appropriately and passing it to a ListActivity
via an ArrayAdapter
or CursorAdapter
. Setting the onListItemClicked()
function then will trivially allow you to send an intent with the appropriate data to your activity.
However, given that you look like you're quite a long way from this more optimal solution (though I highly commend you to explore it); you'll have to set lots of stuff manually.
In your Activity which uses the ScrollView
layout, you'll need to define several OnClickListener
and bind them to the appropriate element in the Scrollview. So something like:
TwoLineListItem tli1 = (TwoLineListItem) findViewById(R.id.tlli1);
tli1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), YourResultDetailActivity.class);
intent.putExtra("foo", 1);
startActivity(intent);
}
});
And repeated for each of your ten items. YourResultDetailActivity
should then get the Extras from the intent and use that to show the correct data.
精彩评论