3

I'm trying to use the function View::scrollBy(int, int) to scroll a LinearLayout in a custom view. The problem is that when the height of the views added in the LinearLayout are WRAP_CONTENT 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 WRAP_CONTENT instade of a fixed height. How can I do this?

I write this simple code to reproduce the problem. You can see the problem in this image:

Clipped LinearLayout

The 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 < 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;
    }
}

Where R.layout.scroll is this XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FF0000"
        android:orientation="vertical" />

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#00FF00"
        android:orientation="vertical" />

</LinearLayout>

@Anne Droid comments a workaround in this related question. But I do not like the collateral problems of it.

Community
  • 1
  • 1
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92

0 Answers0