'ScrollView and Wrap_Content not work
I have a problem in my xml, the ScrollView does not work when my RelativeLayout is wrap_content, but if I have a larger screen than the fixed height works correctly, but if I have a larger fixed height than screen the works correctly, but I need to be wrap_content, because I have a listview that is populated gradually as client wants, follows the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#fff">
</LinearLayout>
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="45"
android:fillViewport="true"
android:scrollbars="vertical">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="2dp"
android:background="@drawable/borda"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
android:id="@+id/lb_cabec"
android:text="@string/str_cabec" />
<TextView
android:id="@+id/lb_pessoa_status"
android:paddingTop="4dp"
android:paddingBottom="15dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:textStyle="bold"
android:text="@string/pessoas_lb_mesa_cartao"
android:layout_below="@+id/lb_cabec"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="2dp"
android:background="@drawable/borda"
android:id="@+id/relative_prod"
android:layout_below="@+id/lb_pessoa_status">
<TextView
android:layout_width="fill_parent"
android:id="@+id/lb_prodMistAdd_cabec_prod"
android:layout_height="wrap_content"
android:background="#000"
android:textColor="#fff"
android:gravity="center_horizontal"
android:text="@string/prodMistAdd_str_prod" />
<ListView
android:id="@+id/mListProdMistAdd_prod"
android:layout_width="fill_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"
android:layout_below="@+id/lb_prodMistAdd_cabec_prod" />
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_below="@+id/mListProdMistAdd_prod"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:id="@+id/bt_prodMistAdd_addProd"
android:text="@string/prodMistAdd_bt_prod" />
</RelativeLayout>
<LinearLayout
android:layout_height="10dp"
android:layout_width="fill_parent"
android:id="@+id/lb_espaco_1"
android:layout_below="@+id/relative_prod"
android:orientation="vertical">
</LinearLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="2dp"
android:background="@drawable/borda"
android:id="@+id/relative_quant"
android:layout_below="@+id/lb_espaco_1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lb_prodMistAdd_cabec_quant"
android:background="#000"
android:textColor="#fff"
android:gravity="center_horizontal"
android:text="@string/prodMistAdd_str_quant" />
<LinearLayout
android:layout_height="5dp"
android:id="@+id/tab_espaco_2"
android:layout_below="@+id/lb_prodMistAdd_cabec_quant"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#fff">
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/tab_espaco_2"
android:orientation="horizontal">
<EditText
android:id="@+id/tb_prodMistAdd_quantidade"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:editable="false"
android:gravity="center_vertical|right"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:layout_width="40dp"
android:layout_height="35dp"
android:gravity="center_horizontal"
android:id="@+id/bt_prodMistAdd_mais"
android:text="@string/prodMistAdd_bt_mais" />
<Button
android:layout_width="40dp"
android:layout_height="35dp"
android:gravity="center_horizontal"
android:id="@+id/bt_prodMistAdd_menos"
android:text="@string/prodMistAdd_bt_menos" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_height="10dp"
android:layout_width="fill_parent"
android:id="@+id/lb_espaco_2"
android:orientation="vertical"
android:layout_below="@+id/relative_quant">
</LinearLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="2dp"
android:background="@drawable/borda"
android:id="@+id/relative_obs"
android:layout_below="@+id/lb_espaco_2">
<TextView
android:layout_width="fill_parent"
android:id="@+id/lb_prodMistAdd_cabec_obs"
android:layout_height="wrap_content"
android:background="#000"
android:textColor="#fff"
android:gravity="center_horizontal"
android:text="@string/prodMistAdd_str_obs" />
<LinearLayout
android:layout_height="5dp"
android:id="@+id/tab_espaco_3"
android:layout_below="@+id/lb_prodMistAdd_cabec_obs"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#fff">
</LinearLayout>
<ListView
android:id="@+id/mListProdMistAdd_obs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_below="@+id/tab_espaco_3" />
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_below="@+id/mListProdMistAdd_obs"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:id="@+id/bt_log_voltar"
android:text="@string/prodMistAdd_bt_obs" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
My listview are configured to not scroll, through code
Solution 1:[1]
Is the listviewinside the scrollview working??
I think the real problem is this.
A listview inside a Scrollview is not a good practice.
Listview will try to implement its own scrolling. But the parent Scrollview will take up all the gestures. So Listview cannot be a child of a Scrollview.
if you still want it to be done that way.
Try this.
ListView lv = (ListView)findViewById(R.id.myListView); // your listview inside scrollview
lv.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;
}
});
Solution 2:[2]
try do define ScrollView layout_width and layout_height as match_parent, without layout_weight
by the way, you should not mix ScrollView with ListView since both have his own scroll. I suggest to split it like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
(...)
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
