Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy a layout is clipped when use scrollBy or scrollTo?
    primarykey
    data
    text
    <p>I'm trying to use the function <a href="http://developer.android.com/reference/android/view/View.html#scrollBy%28int,%20int%29" rel="nofollow noreferrer"><code>View::scrollBy(int, int)</code></a> to scroll a <code>LinearLayout</code> in a custom view. The problem is that when the height of the views added in the <code>LinearLayout</code> are <code>WRAP_CONTENT</code> the content is clipped when scrolled. However when the views has a fixed height (for example 20px) this problem doesn't happen. I want to use <code>WRAP_CONTENT</code> instade of a fixed height. How can I do this?</p> <p>I write this simple code to reproduce the problem. You can see the problem in this image:</p> <p><img src="https://i.stack.imgur.com/yZJYt.png" alt="Clipped LinearLayout"></p> <p>The code:</p> <pre class="lang-java prettyprint-override"><code>public class Scroll extends LinearLayout { private final Context context; private int currentY; public Scroll(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater.from(context).inflate(R.layout.scroll, this, true); LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 19); fillRow((LinearLayout) findViewById(R.id.ll1), params1); fillRow((LinearLayout) findViewById(R.id.ll2), params2); } private void fillRow(LinearLayout linearLayout, LinearLayout.LayoutParams layoutParams) { for (int i = 0; i &lt; 100; i++) { TextView textView = new TextView(context); textView.setBackgroundResource(android.R.color.white); textView.setLayoutParams(layoutParams); textView.setText(i + ""); linearLayout.addView(textView); } } @Override public boolean onTouchEvent(MotionEvent event) { // Idea: https://stackoverflow.com/a/4991692/842697 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { currentY = (int) event.getRawY(); break; } case MotionEvent.ACTION_MOVE: { int y2 = (int) event.getRawY(); int scrollY = currentY - y2; currentY = y2; findViewById(R.id.ll1).scrollBy(0, scrollY); findViewById(R.id.ll2).scrollBy(0, scrollY); break; } case MotionEvent.ACTION_UP: { break; } } return true; } } </code></pre> <p>Where <code>R.layout.scroll</code> is this XML:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;LinearLayout android:id="@+id/ll1" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="#FF0000" android:orientation="vertical" /&gt; &lt;LinearLayout android:id="@+id/ll2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="#00FF00" android:orientation="vertical" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><a href="https://stackoverflow.com/users/1211663/anne-droid">@Anne Droid</a> comments a workaround in this <a href="https://stackoverflow.com/q/12050935/842697">related question</a>. But I do not like the collateral problems of it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload