Reputation: 1644
I am using TableLayout
and Some TableRow
elements has ListView
inside. Until I load data in ListView
All Items in TableRow
works fine. As Data has been loaded in ListViews
layout_width
match_parent
and fill_parent
stops working.
Note: In this Question I am asking about layout_width
issue. My layout_height
is working great and also I don't have scrolling
issue.
Following is the XML of my Layout.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tr_lbl_topSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_orange"
android:padding="5dp" >
<TextView
android:id="@+id/tv_lbl_topSongs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Songs"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/app_white" />
</TableRow>
<TableRow
android:id="@+id/tr_content_topSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lv_topSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</TableRow>
<TableRow
android:id="@+id/tr_viewMore_topSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:background="@color/app_offwhite"
android:gravity="center_horizontal|center_vertical"
android:padding="5dp" >
<TextView
android:id="@+id/tv_viewMore_topSongs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View More"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/app_gray" />
</TableRow>
<TableRow
android:id="@+id/tr_lbl_topVideos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_orange"
android:padding="5dp" >
<TextView
android:id="@+id/tv_lbl_topVideos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Videos"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/app_white" />
</TableRow>
<TableRow
android:id="@+id/tr_content_topVideos"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lv_topVideos"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</TableRow>
<TableRow
android:id="@+id/tr_viewMore_topVideos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:background="@color/app_offwhite"
android:gravity="center_horizontal|center_vertical"
android:padding="5dp" >
<TextView
android:id="@+id/tv_viewMore_topVideos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View More"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/app_gray" />
</TableRow>
<TableRow
android:id="@+id/tr_lbl_topRingtones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_orange"
android:padding="5dp" >
<TextView
android:id="@+id/tv_lbl_topRingtones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Ringtones"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/app_white" />
</TableRow>
<TableRow
android:id="@+id/tr_content_topRingtones"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lv_topRingtones"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</TableRow>
<TableRow
android:id="@+id/tr_viewMore_topRingtones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:background="@color/app_offwhite"
android:gravity="center_horizontal|center_vertical"
android:padding="5dp" >
<TextView
android:id="@+id/tv_viewMore_topRingtones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View More"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/app_gray" />
</TableRow>
</TableLayout>
</ScrollView>
Update:
I have managed to use ListView inside of ScrollView with following codes. I registered TouchListner
lv_topSongs = (ListView) rootView.findViewById(R.id.lv_topSongs);
lv_topSongs.setOnTouchListener(this);
My TouchListner in which I am passing touch event to ScrollView and Disabling Touch Even on ListView.
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
mScrollView.onTouchEvent(event);
return false;
}
I am setting height after loading data in ListView With following Code.
setListViewHeightBasedOnChildren(lv_topSongs);
And My setListViewHeightBasedOnChildren
function
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
params.width = LayoutParams.MATCH_PARENT;
listView.setLayoutParams(params);
listView.requestLayout();
}
Screenshot
Upvotes: 0
Views: 1232
Reputation: 1644
I solved this Problem by Assigning weight to ListView
android:layout_weight="1"
.
Still unable to understand why I have to assign weight even there is only one Item in TableRow
however This solved my problem.
Upvotes: 1
Reputation: 676
If you want your list to fill the height equal to your table row you will have to give some fixed height to your table row.. Try it.
Upvotes: 0