1

I have a custom animated ViewGroup (based heavily on SlidingTab).

In my app, I've stripped this down to just a single TextView that behaves just like the SlidingTab (the slider on your lock screen). This works great, but the TextView that slides is defined in code. I'd like to have an entire RelativeLayout that slides instead.

I have looked at all sorts of posts trying different things but still haven't gotten it to work. The RelativeLayout xml file has to be inflated and added as a child of 'parent' in the Slider constructer, but I also need to be able do the measure() and layout() methods. See the relevant code below.

public class SlindingView extends ViewGroup {

private static Slider mLeftSlider;
private static int tabWidth;

/**
 * Simple container class for all things pertinent to a slider.
 */
private static class Slider {

    private final TextView tab;

    Slider(ViewGroup parent) {
        // Want to change 'tab' to a RelativeLayout in xml
        tab = new TextView(parent.getContext());
        tab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.FILL_PARENT));
        tab.setText("SLIDE THIS");

        parent.addView(tab);
    }

    void layout(int l, int t, int r, int b) {
        tabWidth = getTabWidth();
        int tabHeight = getTabHeight();
        tab.layout(0, 0, tabWidth, tabHeight);
    }

    public void measure() {
        tab.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    }

    public int getTabWidth() {
        return tab.getMeasuredWidth();
    }

    public int getTabHeight() {
        return tab.getMeasuredHeight();
    }
}

/**
 * Constructor
 */
public SlindingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mLeftSlider = new Slider(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

    mLeftSlider.measure();
    final int leftTabWidth = mLeftSlider.getTabWidth();
    final int leftTabHeight = mLeftSlider.getTabHeight();
    final int width = Math.max(widthSpecSize, leftTabWidth);
    final int height = leftTabHeight;

    setMeasuredDimension(width, height);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mLeftSlider.layout(l, t, r, b);
}

}

Community
  • 1
  • 1
kevin
  • 136
  • 2
  • 9

0 Answers0