Example usage for android.view ViewTreeObserver addOnScrollChangedListener

List of usage examples for android.view ViewTreeObserver addOnScrollChangedListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver addOnScrollChangedListener.

Prototype

public void addOnScrollChangedListener(OnScrollChangedListener listener) 

Source Link

Document

Register a callback to be invoked when a view has been scrolled.

Usage

From source file:org.thoughtcrime.securesms.ProfileFragment.java

private void refreshLayout() {
    gDataPreferences = new GDataPreferences(getActivity());
    boolean isMyProfile = (GUtil.numberToLong(gDataPreferences.getE164Number() + "") + "")
            .contains(GUtil.numberToLong(profileId) + "");

    layout_status = (RelativeLayout) getView().findViewById(R.id.layout_status);
    layout_phone = (RelativeLayout) getView().findViewById(R.id.layout_phone);
    layout_group = (RelativeLayout) getView().findViewById(R.id.layout_member);

    statusDate = (TextView) getView().findViewById(R.id.profile__date);
    leaveGroup = (Button) getView().findViewById(R.id.buttonLeaveGroup);
    profileHeader = (TextView) getView().findViewById(R.id.profile_header);
    profileStatus = (AutoCompleteTextView) getView().findViewById(R.id.profile_status);
    xCloseButton = (ImageView) getView().findViewById(R.id.profile_close);
    imageText = (TextView) getView().findViewById(R.id.image_text);
    profilePhone = (TextView) getView().findViewById(R.id.profile_phone);
    groupMember = (ListView) getView().findViewById(R.id.selected_contacts_list);
    historyLayout = (LinearLayout) getView().findViewById(R.id.historylayout);
    historyContentTextView = (TextView) getView().findViewById(R.id.history_content);
    historyScrollView = (HorizontalScrollView) getView().findViewById(R.id.horizontal_scroll);
    recipientsPanel = (PushRecipientsPanel) getView().findViewById(R.id.recipients);
    profilePhone.setText(phonenumber);//  w  w  w .  j a  v  a  2  s.c  om
    profilePicture = (ThumbnailView) getView().findViewById(R.id.profile_picture);
    phoneCall = (ImageView) getView().findViewById(R.id.phone_call);
    (getView().findViewById(R.id.contacts_button)).setOnClickListener(new AddRecipientButtonListener());
    historyLine = (RelativeLayout) getView().findViewById(R.id.layout_history);
    recipient = recipients.getPrimaryRecipient();
    attachmentAdapter = new ProfileImageTypeSelectorAdapter(getActivity());
    scrollView = (ScrollView) getView().findViewById(R.id.scrollView);
    seekBarFont = (SeekBar) getView().findViewById(R.id.seekbar_font);
    chatPartnersColor = (CheckBox) getView().findViewById(R.id.enabled_chat_partners_color);
    colorDefault = (CheckBox) getView().findViewById(R.id.color_default);
    layoutColor = (RelativeLayout) getView().findViewById(R.id.layout_color);
    floatingActionColorButton = (FloatingActionButton) getView().findViewById(R.id.fab_new_color);
    final ImageView profileStatusEdit = (ImageView) getView().findViewById(R.id.profile_status_edit);

    if (!isGroup) {
        ImageSlide slide = ProfileAccessor.getProfileAsImageSlide(getActivity(), masterSecret, profileId);
        if (slide != null && !isMyProfile) {
            if (masterSecret != null) {
                try {
                    profilePicture.setImageResource(slide, masterSecret);
                } catch (IllegalStateException e) {
                    Log.w("GDATA", "Unable to load profile image");
                }
                profileStatus.setText(ProfileAccessor.getProfileStatusForRecepient(getActivity(), profileId),
                        TextView.BufferType.EDITABLE);
                profileStatus.setEnabled(false);
                layout_status.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        profileStatusEdit.performClick();
                        return false;
                    }
                });
                statusDate.setText(GUtil.getLocalDate(
                        ProfileAccessor.getProfileUpdateTimeForRecepient(getActivity(), profileId),
                        getActivity().getApplicationContext()));
                imageText.setText(recipient.getName());
            }
            profilePicture.setThumbnailClickListener(new ThumbnailClickListener());
        } else if (ProfileAccessor.getMyProfilePicture(getActivity()).hasImage() && isMyProfile) {
            profileStatus.setText(ProfileAccessor.getProfileStatus(getActivity()),
                    TextView.BufferType.EDITABLE);
            imageText.setText(getString(R.string.MediaPreviewActivity_you));
            initColorSeekbar();
            profilePicture.setThumbnailClickListener(new ThumbnailClickListener());
            if ((ProfileAccessor.getMyProfilePicture(getActivity()).getUri() + "").equals("")) {
                profilePicture.setImageBitmap(ContactPhotoFactory.getDefaultContactPhoto(getActivity()));
            } else {
                profilePicture.setImageResource(ProfileAccessor.getMyProfilePicture(getActivity()));
            }
            historyLine.setVisibility(View.GONE);
        } else {
            imageText.setText(recipient.getName());
            profilePicture.setImageBitmap(recipient.getContactPhoto());
        }
        layout_group.setVisibility(View.GONE);
    } else {
        String groupName = recipient.getName();
        Bitmap avatar = recipient.getContactPhoto();
        String encodedGroupId = recipient.getNumber();
        if (encodedGroupId != null) {
            try {
                groupId = GroupUtil.getDecodedId(encodedGroupId);
            } catch (IOException ioe) {
                groupId = null;
            }
        }
        GroupDatabase db = DatabaseFactory.getGroupDatabase(getActivity());
        Recipients recipients = db.getGroupMembers(groupId, false);

        recipientsPanel.setPanelChangeListener(new PushRecipientsPanel.RecipientsPanelChangedListener() {
            @Override
            public void onRecipientsPanelUpdate(Recipients recipients) {
                Log.w("GDATA", "onRecipientsPanelUpdate received.");
                if (recipients != null) {
                    addAllSelectedContacts(recipients.getRecipientsList());
                    syncAdapterWithSelectedContacts();
                }
            }
        });

        if (recipients != null) {
            final List<Recipient> recipientList = recipients.getRecipientsList();
            if (recipientList != null) {
                if (existingContacts == null)
                    existingContacts = new HashSet<>(recipientList.size());
                existingContacts.addAll(recipientList);
            }
            if (recipientList != null) {
                if (existingContacts == null)
                    existingContacts = new HashSet<>(recipientList.size());
                existingContacts.addAll(recipientList);
            }

            SelectedRecipientsAdapter adapter = new SelectedRecipientsAdapter(getActivity(), android.R.id.text1,
                    new ArrayList<SelectedRecipientsAdapter.RecipientWrapper>());
            adapter.clear();

            if (existingContacts != null) {
                for (Recipient contact : existingContacts) {
                    adapter.add(new SelectedRecipientsAdapter.RecipientWrapper(contact, false));
                }
            }
            adapter.setMasterSecret(masterSecret);
            adapter.setThreadId(threadId);
            adapter.setOnRecipientDeletedListener(new SelectedRecipientsAdapter.OnRecipientDeletedListener() {
                @Override
                public void onRecipientDeleted(Recipient recipient) {
                    removeSelectedContact(recipient);
                }
            });
            groupMember.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
        if (avatar != null) {
            profilePicture.setVisibility(View.GONE);
            getView().findViewById(R.id.profile_picture_group).setVisibility(View.VISIBLE);
            scaleImage((ImageView) getView().findViewById(R.id.profile_picture_group), avatar);
        }
        imageText.setText(groupName);
        if (profileStatusString.equals("")) {
            profileStatus.setText(groupName);
        }
        layout_phone.setVisibility(View.GONE);

        leaveGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isActiveGroup()) {
                    handleLeavePushGroup();
                } else {
                    handleDeleteThread();
                }
            }
        });
        if (!isActiveGroup()) {
            leaveGroup.setText(getString(R.string.conversation__menu_delete_thread));
        }

        heightMemberList = GUtil.setListViewHeightBasedOnChildren(groupMember);
    }
    ImageView profileImageEdit = (ImageView) getView().findViewById(R.id.profile_picture_edit);
    ImageView profileImageDelete = (ImageView) getView().findViewById(R.id.profile_picture_delete);
    if (!isMyProfile && !isGroup) {
        profileStatusEdit.setVisibility(View.GONE);
        profileImageDelete.setVisibility(View.GONE);
        profileImageEdit.setVisibility(View.GONE);
    } else {
        if (isGroup) {
            profileImageDelete.setVisibility(View.GONE);
            profileHeader.setText(getString(R.string.group_title));
        } else {
            profileImageDelete.setVisibility(View.VISIBLE);
        }
        profileStatusEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                profileStatus.setEnabled(!profileStatus.isEnabled());
                if (!profileStatus.isEnabled()) {
                    hasChanged = true;
                    hasLeft = false;
                    profileStatusEdit.setImageDrawable(getResources().getDrawable(R.drawable.ic_content_edit));

                    InputMethodManager imm = (InputMethodManager) getActivity()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(profileStatus.getWindowToken(), 0);
                    if (isGroup) {
                        new UpdateWhisperGroupAsyncTask().execute();
                    } else {
                        ProfileAccessor.setProfileStatus(getActivity(), profileStatus.getText() + "");
                    }
                } else {
                    profileStatusEdit
                            .setImageDrawable(getResources().getDrawable(R.drawable.ic_send_sms_gdata));
                    profileStatus.showDropDown();
                    profileStatus.requestFocus();
                }
            }
        });
        profileImageDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hasChanged = true;
                hasLeft = false;
                ProfileAccessor.deleteMyProfilePicture(getActivity());
                refreshLayout();
                profileStatus.dismissDropDown();
                gDataPreferences.hasProfileImageChanged(true);
            }
        });
        profileImageEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gDataPreferences.hasProfileImageChanged(true);
                hasChanged = true;
                hasLeft = false;
                handleAddAttachment();
            }
        });
    }
    if (isMyProfile) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_dropdown_item_1line,
                getResources().getStringArray(R.array.status_suggestions));
        profileStatus.setAdapter(adapter);
        profileStatus.setCompletionHint(getString(R.string.status_hint));
        profileStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                profileStatus.dismissDropDown();
            }
        });
        profileStatus.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                profileStatus.setEnabled(!profileStatus.isEnabled());
                if (!profileStatus.isEnabled()) {
                    hasChanged = true;
                    hasLeft = false;
                    profileStatusEdit.setImageDrawable(getResources().getDrawable(R.drawable.ic_content_edit));

                    InputMethodManager imm = (InputMethodManager) getActivity()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(profileStatus.getWindowToken(), 0);
                    if (isGroup) {
                        new UpdateWhisperGroupAsyncTask().execute();
                    } else {
                        ProfileAccessor.setProfileStatus(getActivity(), profileStatus.getText() + "");
                    }
                }
            }
        });
    } else {
        //        if(!isMyProfile) {
        setMediaHistoryImages();
    }
    xCloseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finishAndSave();
        }
    });
    phoneCall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            handleDial(recipient);
        }
    });
    final RelativeLayout scrollContainer = (RelativeLayout) getView().findViewById(R.id.scrollContainer);
    final LinearLayout mainLayout = (LinearLayout) getView().findViewById(R.id.mainlayout);
    scrollView.setSmoothScrollingEnabled(true);
    ViewTreeObserver vto = scrollView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            scrollView.scrollTo(0, mainLayout.getTop() - PADDING_TOP);
        }
    });
    scrollContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // getActivity().finish();
        }
    });
    onScrollChangeListener = new ViewTreeObserver.OnScrollChangedListener() {

        @Override
        public void onScrollChanged() {
            if (BuildConfig.VERSION_CODE >= 11) {
                scrollContainer.setBackgroundColor(Color.WHITE);
                scrollContainer
                        .setAlpha((float) ((1000.0 / scrollContainer.getHeight()) * scrollView.getHeight()));
            }
            int keyboardHeight = 150;
            int paddingBottom = 250;
            int scrollViewHeight = scrollView.getHeight();
            if (getActivity().getResources()
                    .getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                scrollViewHeight = 2 * scrollViewHeight;
            }
            int heightDiff = scrollView.getRootView().getHeight() - scrollView.getHeight();
            if (pxToDp(heightDiff) > keyboardHeight) {
                keyboardIsVisible = true;
            } else {
                keyboardIsVisible = false;
            }
            if (!keyboardIsVisible) {
                if ((mainLayout.getTop() - scrollViewHeight) > scrollView.getScrollY()
                        - pxToDp(paddingBottom)) {
                    finishAndSave();
                }
                if ((scrollViewHeight + (heightMemberList)) < (scrollView.getScrollY() - mainLayout.getTop())) {
                    finishAndSave();
                }
            }
        }
    };
    scrollView.getViewTreeObserver().addOnScrollChangedListener(onScrollChangeListener);
    scrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            ViewTreeObserver observer = scrollView.getViewTreeObserver();
            observer.addOnScrollChangedListener(onScrollChangeListener);
            return false;
        }

    });
}