Example usage for android.widget ScrollView getViewTreeObserver

List of usage examples for android.widget ScrollView getViewTreeObserver

Introduction

In this page you can find the example usage for android.widget ScrollView getViewTreeObserver.

Prototype

public ViewTreeObserver getViewTreeObserver() 

Source Link

Document

Returns the ViewTreeObserver for this view's hierarchy.

Usage

From source file:com.google.reviewit.ReviewChangesFragment.java

private void init() {
    SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) v(R.id.swipeRefreshLayout);
    swipeRefreshLayout.setColorSchemeColors(widgetUtil.color(R.color.progressBar));
    swipeRefreshLayout.setRefreshing(true);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override/*from   ww  w  .  jav a  2  s .  c om*/
        public void onRefresh() {
            getApp().getQueryHandler().reset();
            loadAndDisplay(true);
        }
    });

    v(R.id.reloadButton).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            reloadQuery();
        }
    });

    final View nextPageProgress = v(R.id.nextPageProgress);
    final ScrollView scrollView = (ScrollView) v(R.id.scrollView);
    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (isAdded() && getApp().getQueryHandler().hasNext()
                    && nextPageProgress.getVisibility() == View.GONE) {
                View lastChild = scrollView.getChildAt(scrollView.getChildCount() - 1);
                if ((lastChild.getBottom() - (scrollView.getHeight() + scrollView.getScrollY())) == 0) {
                    setVisible(nextPageProgress);
                    scrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            scrollView.fullScroll(View.FOCUS_DOWN);
                            loadAndDisplay();
                        }
                    });
                }
            }
        }
    });
}

From source file:us.koller.todolist.Activities.InfoActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_info);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.info_activity_toolbar);
    setSupportActionBar(toolbar);/*from  w w  w .j a  v a2  s  .  c o  m*/
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");
    }

    initTheme(toolbar);

    final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (scrollView.getScrollY() != 0) {
                elevateToolbar(toolbar);
            } else {
                deelevateToolbar(toolbar);
            }
        }
    });

    TextView version = (TextView) findViewById(R.id.version);
    if (helper.lightCoordColor()) {
        version.setTextColor(ContextCompat.getColor(this, R.color.grey700));
    }
    version.setText(BuildConfig.VERSION_NAME);

    ImageView icon = (ImageView) findViewById(R.id.icon);
    Glide.with(this).load("http://todolist.koller.us/todolist_icon_512px").into(icon);

    //Glide license
    View license_item_1 = findViewById(R.id.license_item_1);
    ((TextView) license_item_1.findViewById(R.id.text)).setText(R.string.glide);
    license_item_1.findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Intent.ACTION_VIEW)
                    .setData(Uri.parse("https://github.com/bumptech/glide/blob/master/LICENSE")));
        }
    });
    license_item_1.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(
                    new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://github.com/bumptech/glide")));
        }
    });

    //MaterialNumberPicker license
    View license_item_2 = findViewById(R.id.license_item_2);
    ((TextView) license_item_2.findViewById(R.id.text)).setText(R.string.materialnumberpicker);
    license_item_2.findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Intent.ACTION_VIEW).setData(
                    Uri.parse("https://github.com/KasualBusiness/MaterialNumberPicker/blob/master/LICENSE")));
        }
    });
    license_item_2.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Intent.ACTION_VIEW)
                    .setData(Uri.parse("https://github.com/KasualBusiness/MaterialNumberPicker")));
        }
    });

    //Google Play Service Attribution
    View license_item_3 = findViewById(R.id.license_item_3);
    ((TextView) license_item_3.findViewById(R.id.text)).setText(R.string.google_play_servcie_attribution);
    license_item_3.findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            WebView webView = new WebView(InfoActivity.this);
            webView.loadData(
                    GoogleApiAvailability.getInstance().getOpenSourceSoftwareLicenseInfo(InfoActivity.this),
                    "text/plain", "utf-8");

            AlertDialog dialog = new AlertDialog.Builder(InfoActivity.this)
                    .setTitle("Google Play Servcie Attribution").setView(webView).setPositiveButton("Ok", null)
                    .create();
            dialog.show();
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(helper.get(ThemeHelper.FAB_COLOR));
        }
    });
    license_item_3.findViewById(R.id.button).setVisibility(View.GONE);

    //Google Play Service Attribution
    View license_item_4 = findViewById(R.id.license_item_4);
    ((TextView) license_item_4.findViewById(R.id.text)).setText("Privacy Policy");
    license_item_4.findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Intent.ACTION_VIEW).setData(
                    Uri.parse("https://github.com/kollerlukas/TODOList-Android-App/blob/master/PRIVACY.md")));
        }
    });
    license_item_4.findViewById(R.id.button).setVisibility(View.GONE);
}

From source file:jp.mydns.sys1yagi.android.viewsticker.ViewSticker.java

private ViewGroup wrap(final View target, final ScrollView monitored, final ViewGroup container) {
    ViewGroup parent = (ViewGroup) target.getParent();
    int index = parent.indexOfChild(target);
    parent.removeView(target);//  w w  w .j  a va  2 s .  c o m
    final int[] dimension = new int[2];

    FrameLayout stuffing = new FrameLayout(target.getContext()) {
        ViewTreeObserver.OnScrollChangedListener mObserver = new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                monitored.getLocationOnScreen(dimension);
                int offset = dimension[1];
                getLocationOnScreen(dimension);
                int top = dimension[1] - offset;
                offset(target, top);
                if (top < 0 && indexOfChild(target) >= 0) {
                    removeView(target);
                    container.addView(target);
                } else if (top >= 0 && indexOfChild(target) < 0) {
                    container.removeView(target);
                    addView(target);
                }
            }
        };

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
            measureChildWithMargins(target, widthMeasureSpec, 0, heightMeasureSpec, 0);
            setMeasuredDimension(widthSpecSize, heightSpecSize + target.getMeasuredHeight());
        }

        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            monitored.getViewTreeObserver().addOnScrollChangedListener(mObserver);
        }

        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            monitored.getViewTreeObserver().removeOnScrollChangedListener(mObserver);
        }
    };
    stuffing.addView(target);
    parent.addView(stuffing, index);
    return stuffing;
}