Two listview inside scrollView - android

I have two listview. i need to scroll one listview as the continuation of another list view. In between the list there should be a text view. if i use the two listview and textview inside a scrollview icould not able to scroll both the list. if i use the listview without scrollview i can see the two listview but scrolling is not continues.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FAF0BE" >
<Button
android:id="#+id/btnheader"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#A1CAF1"
android:text="DisoderDetail" />
<TextView
android:id="#+id/txtTmtDnam"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btnheader"
android:layout_marginLeft="#dimen/marginLLeft"
android:layout_marginTop="#dimen/marginTopp"
android:text="jgjggjh"
android:textColor="#000000" />
<TextView
android:id="#+id/txtdisDdtail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtTmtDnam"
android:layout_marginLeft="#dimen/marginLLeft"
android:layout_marginTop="#dimen/marginTopp"
android:text="jgjggjh hjhhhjh jhjhh"
android:textColor="#000000" />
<RelativeLayout
android:id="#+id/Rvlytt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/lyt2"
android:layout_below="#id/txtdisDdtail"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/marginBottomm"
android:layout_marginTop="#dimen/marginTop"
android:orientation="horizontal" >
<ImageView
android:id="#+id/starimg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:onClick="star1Clicked"
android:src="#drawable/starunselected"
android:tag="1" />
<ImageView
android:id="#+id/starimg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg1"
android:onClick="star2Clicked"
android:src="#drawable/starunselected"
android:tag="2" />
<ImageView
android:id="#+id/starimg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg2"
android:onClick="star3Clicked"
android:src="#drawable/starunselected"
android:tag="3" />
<ImageView
android:id="#+id/starimg4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg3"
android:onClick="star4Clicked"
android:src="#drawable/starunselected"
android:tag="4" />
</RelativeLayout>
<ScrollView
android:id="#+id/Scrlvw"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#id/Rvlytt2" >
<RelativeLayout
android:id="#+id/rvl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtDcmt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/marginTop"
android:text="comment"
android:textColor="#000000" />
<ListView
android:id="#+id/lstvwDis11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtDcmt" >
</ListView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rvl2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/rvl1"
>
<TextView
android:id="#+id/txtDrate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/lstvwDis11"
android:layout_marginTop="#dimen/marginTop"
android:text="Rate"
android:textColor="#000000" />
<ListView
android:id="#+id/lstvwDis12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtDrate" >
</ListView>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/Rvlt22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/Edtcmmt"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:background="#ffffff"
android:hint="Add a comment"
android:textColor="#000000" />
<Button
android:id="#+id/btnpost"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/Edtcmmt"
android:onClick="OnPostbtnclick"
android:text="Post" />
</RelativeLayout>
</RelativeLayout>

This is not correct design for starters but if you want to stick with the design one quick fix that will make your code work is to put both of your listViews inside one more list view and put that under scroll view. that should solve your problem of scrolling.

When placing ListView inside ScrollView I use the following method to set listView height and the scrolling works properly. Use this after you've added all views to your listview.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}

Related

listview dynamic items height

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100"
android:background="#drawable/round_shape"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="25">
<TextView
android:text="Title"
android:paddingStart="20dp"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/borrowBookTitle"
android:maxLines="1"
android:textSize="25sp"
android:textColor="#000000"
/>
<TextView
android:text="Locate"
android:paddingStart="20dp"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/locate"
android:maxLines="2"
android:textSize="15sp"
android:textColor="#838383"
/>
<TextView
android:text="borrowStartDate"
android:layout_marginStart="10dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="8dp"
android:paddingStart="10dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
android:layout_width="wrap_content"`enter code here`
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/borrowStartDate"
android:maxLines="1"
android:textSize="17sp"
android:textColor="#000000"
android:background="#color/childview_background"
/>
<TextView
android:text="borrowEndDate"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:paddingStart="10dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/borrowEndDate"
android:visibility="gone"
android:maxLines="1"
android:textSize="17sp"
android:textColor="#000000"
android:background="#color/childview_background"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="75">
<TextView
android:text="D-11"
android:paddingStart="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/borrowDday"
android:textSize="35sp"
android:textColor="#000000"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/button1"
android:id="#+id/renewBtn"
android:textSize="20sp"
android:textColor="#000000"
android:visibility="gone"
android:text="RENEW"
/>
</LinearLayout>
Above xml files is Listview's item xml file.
borrowStartDate, borrowEndDate and renewBtn visiblility is gone.
But When click on item, change gone to visible
borrowListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
TextView startDate = (TextView)view.findViewById(R.id.borrowStartDate);
TextView endDate = (TextView)view.findViewById(R.id.borrowEndDate);
Button renewBtn = (Button)view.findViewById(R.id.renewBtn);
if ( startDate.getVisibility() == View.GONE) {
startDate.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_down_and_fade_in));
endDate.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_down_and_fade_in));
renewBtn.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in));
startDate.setVisibility(View.VISIBLE);
endDate.setVisibility(View.VISIBLE);
renewBtn.setVisibility(View.VISIBLE);
}
else {
startDate.setVisibility(View.GONE);
endDate.setVisibility(View.GONE);
renewBtn.setVisibility(View.GONE);
}
}
});
It's change visibility code
Problem is Listview height matched When Items Gone's height.
So When I clicked each item not directly showing under parts.
I want to When click on items, Listview height automatically expand
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<WebView
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
android:id="#+id/rightWebView"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh"
android:backgroundTint="#color/button1"
android:drawableEnd="#drawable/refresh_picto"
android:id="#+id/refreshBorrowBook"
android:onClick="refreshBorrow"
android:layout_marginBottom="10dp"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/borrowBookList"
android:dividerHeight="10dp"
android:divider="#color/main_background"
/>
<TextView
android:text="예약 목록"
android:paddingStart="50dp"
android:visibility="gone"
android:layout_marginTop="15dp"
android:layout_marginBottom="20dp"
android:textColor="#000000"
android:textSize="25sp"
android:background="#drawable/round_shape"
android:backgroundTint="#color/childview_background"
android:id="#+id/reservation_list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reservationBookList"
android:dividerHeight="10dp"
android:divider="#color/main_background"
/>
</LinearLayout>
</ScrollView>
This is fragments code
Add snapshots
after refresh button after click refresh button
when click item when click item
If scrolling down, then showing listview's other parts
I don't want to scrolling.
Just When click items then listview get heights automaticially.
I think this problem is not relative with notifyDataSetChanged();
Use
view.getLayoutParams().Height=value
You don't have to toggle visibility by the way
Hope that helps
Below method calculate each Child View height , You must call below method after pressing button of any child view in Listview to visible and invisible.
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
Call this function right after you change ListView items, like that:
Utility.setListViewHeightBasedOnChildren(myListView);

Listview height not correct

I used this method to calculate and set listview height at runtime
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/lv_cart" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SubTotal:"
android:textSize="18sp"
android:textColor="#000"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs. 600"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/colorPrimary"
android:id="#+id/tv_sub_total"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Order Type:"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textColor="#4CAF50"
android:textStyle="bold"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Delivery"
android:textAllCaps="false"
android:textSize="18sp"
android:textColor="#ffffff"
android:background="#color/colorPrimary"
android:id="#+id/btn_delivery"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="PickUp"
android:textAllCaps="false"
android:textSize="18sp"
android:textColor="#ffffff"
android:background="#color/colorPrimary"
android:id="#+id/btn_pickup"/>
</LinearLayout>
</ScrollView>
I debug this method and checked that this method is returning correct height based on children. But when listview is shown then the view in which list is shown is not correct.
I added 8 items in my listview but it only shows 5 when I ran it but height that method returned was correct as it showed 1 view height=51 and 8 view height=415 including divider height.
I also tried listview height in xml with match parent and wrap content but result was same.
What's the problem here?
pleaes try this one :
listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, the height here));

List view displaying only one record when Android scroll view is used

I am new to android and I am displaying a ListView getting data from a webservice. Below listview there are some other views/controls on the activity.Now the issue I am getting is that when the webservice returns more than one record controls below the listview are getting hidden at the bottom of window for that I tried to use scrollView in my android activity but the issue I am getting after using scroll view is that only one record is getting displayed in the listview. So the problem is 2 way :
when I implement listview with other controls at the bottom of it I am not able to scroll when listview displays multiple records.
when I use scrollView with and put the listview and other controls inside scrollview , listview displays only one record.
My xml for this is like this:
<LinearLayout 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="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:singleLine="true"
android:width="70dp"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
You shouldn't put a listview in scrollview if still you want to place then set the height of listview programatically with this method.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop()
+ listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}

ListView layout_width match_parent and fill_parent not working inside of TableRow

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
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.
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.

ListView inside a ScrollView in Android

I have researched a lot regarding this topic and I'm finally posting this question cause I did not find what I searched for.
I have a layout with Listview inside a ScrollView.
Listview is used to display the comments from various users.
The layout is as follows :
Layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/panel_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/image" />
<View
android:id="#+id/image_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#+id/image"
android:background="#c2c2c2" />
<TextView
android:id="#+id/prod_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_divider"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:text="SUV car by Nissaan"
android:textAlignment="gravity"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/productCost_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/prod_name_text"
android:layout_below="#+id/prod_name_text"
android:text="$150"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/likes_details_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/prod_name_text"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/productCost_text"
android:text="rob + 1,124"
android:textColor="#000000" />
<ImageButton
android:id="#+id/dreamitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_details_text"
android:layout_margin="10dp"
android:background="#null"
android:src="#drawable/dreamplus" />
<ImageButton
android:id="#+id/addtoListBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_details_text"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/dreamitBtn"
android:background="#null"
android:src="#drawable/add_to_list_bg" />
<View
android:id="#+id/_first_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_below="#+id/addtoListBtn"
android:background="#838B8B" />
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/_first_divider"
android:layout_marginTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
<Button
android:id="#+id/cost"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignRight="#+id/image"
android:layout_alignTop="#+id/addtoListBtn"
android:layout_marginLeft="120dp"
android:layout_toRightOf="#+id/addtoListBtn"
android:background="#drawable/buy_button"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="60$"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/second_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_marginTop="10dp"
android:background="#838B8B" />
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<TextView
android:id="#+id/comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/upper_divider"
android:layout_alignParentLeft="true"
android:text="1"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/comments_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/comments_count"
android:text="Comment(s)"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:id="#+id/upper_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="#id/comments_text"
android:background="#000000" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/upper_divider"
>
</ListView>
<View
android:id="#+id/lower_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="#id/list"
android:background="#000000" />
<EditText
android:id="#+id/comments_edit"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_below="#+id/lower_divider"
android:background="#FFFFFF"
android:hint="Enter your comment"
android:paddingLeft="8dp"
android:paddingTop="10dp" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:background="#000000" />
<Button
android:id="#+id/post_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lower_divider"
android:layout_toRightOf="#+id/comments_edit"
android:background="#FFFFFF"
android:text="Post" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
And the layout looks like :
So the layout consists of three parts : (Thick Grey Line acts as a divider)
First Part :
Includes Webservice call for image and cost,product name and users etc.
Second Part :
Has 3 tabs where all the tabs content will be retrieved from single web service.
Third Part :
This is where the actual listview exists but it fails to show. The edit text at the bottom is actually below the listview which fails to show up. This also includes a web service call.
I have added a header to listview.
Solutions that I have tried so far :
I have used a helper class to set the height of Listview dynamically(atleast thats what I think it does) :
The class is as follows :
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = myListView.getPaddingBottom() + myListView.getPaddingTop();
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
System.out.println("List view id: "+myListView.getId());
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter
.getCount() - 1));
System.out.println("The params height is " + params.height);
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
And I have called this as
Helper.getListViewSize(MainActivity.listView);
after setting the adapter to list.
Second Solution:
listView.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
From what I read,this syncs the scrollview according to listview.
Web Service Call :
I am calling the webservice for comments as soon as the above layout launches. I can see the web service being called as I can see the response printed in the logcat. But it does not set the listview.
Note :
I have implemented the webservice call of listview separately and it works and sets the comments to the listview. So, the real problem exists with the fact that I have used scrollview inside a listview.
Any help will be much appreciated. Thanks in advance.
ListView inside a ScrollView is not a good practice because listview has it own scrolling if it doesnt fit on the screen.

Categories

Resources