Search in sources :

Example 1 with AppBarLayout

use of android.support.design.widget.AppBarLayout in project Reader by TheKeeperOfPie.

the class FragmentNewPost method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_new_post, container, false);
    layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
    layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
    scrollText = (NestedScrollView) view.findViewById(R.id.scroll_text);
    textInfo = (TextView) view.findViewById(R.id.text_info);
    textSubmit = (TextView) view.findViewById(R.id.text_submit);
    editTextTitle = (EditText) view.findViewById(R.id.edit_title);
    editTextBody = (EditText) view.findViewById(R.id.edit_body);
    toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    toolbar.setTitle(getString(R.string.new_post));
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
    toolbar.setNavigationOnClickListener(v -> {
        UtilsInput.hideKeyboard(editTextBody);
        mListener.onNavigationBackClick();
    });
    toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    setUpOptionsMenu();
    textInfo.setText(getString(R.string.submitting_post, getArguments().getString(SUBREDDIT), getArguments().getString(USER)));
    String submitTextHtml = getArguments().getString(SUBMIT_TEXT_HTML);
    Log.d(TAG, "submitTextHtml: " + submitTextHtml);
    if (TextUtils.isEmpty(submitTextHtml) || "null".equals(submitTextHtml)) {
        textSubmit.setVisibility(View.GONE);
    } else {
        textSubmit.setText(UtilsReddit.getFormattedHtml(submitTextHtml));
    }
    textSubmit.setMovementMethod(LinkMovementMethod.getInstance());
    if (Reddit.PostType.LINK == postType) {
        editTextBody.setHint("URL");
    } else {
        editTextBody.setHint("Text");
    }
    View.OnFocusChangeListener onFocusChangeListener = (v, hasFocus) -> {
        if (hasFocus) {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }
    };
    editTextTitle.setOnFocusChangeListener(onFocusChangeListener);
    editTextBody.setOnFocusChangeListener(onFocusChangeListener);
    editMarginDefault = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
    editMarginWithActions = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
    textPreview = (TextView) view.findViewById(R.id.text_preview);
    viewDivider = view.findViewById(R.id.view_divider);
    toolbarActions = (Toolbar) view.findViewById(R.id.toolbar_actions);
    toolbarActions.inflateMenu(R.menu.menu_editor_actions);
    toolbarActions.setOnMenuItemClickListener(this);
    tabLayout = (TabLayout) view.findViewById(R.id.layout_tab);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabTextColors(themer.getColorFilterTextMuted().getColor(), themer.getColorFilterPrimary().getColor());
    viewPager = (ViewPager) view.findViewById(R.id.view_pager);
    viewPager.setAdapter(new PagerAdapter() {

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position) {
                case PAGE_POST:
                    return getString(R.string.page_post);
                case PAGE_PREVIEW:
                    return getString(R.string.page_preview);
            }
            return super.getPageTitle(position);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return viewPager.getChildAt(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }

        @Override
        public int getCount() {
            if (Reddit.PostType.LINK == postType) {
                tabLayout.setVisibility(View.GONE);
                toolbarActions.setVisibility(View.GONE);
                viewDivider.setVisibility(View.GONE);
                itemHideActions.setVisible(false);
                return 1;
            }
            return viewPager.getChildCount();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    });
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (position == PAGE_POST && toolbarActions.getVisibility() == View.VISIBLE) {
                float translationY = positionOffset * (toolbarActions.getHeight() + viewDivider.getHeight());
                viewDivider.setTranslationY(translationY);
                toolbarActions.setTranslationY(translationY);
            }
        }

        @Override
        public void onPageSelected(int position) {
            if (position == PAGE_PREVIEW) {
                if (editTextBody.length() == 0) {
                    textPreview.setText(R.string.empty_reply_preview);
                } else {
                    textPreview.setText(Html.fromHtml(Processor.process(editTextBody.getText().toString())));
                }
            }
            if (Reddit.PostType.SELF == postType) {
                itemHideActions.setVisible(position == PAGE_POST);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    tabLayout.setupWithViewPager(viewPager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    layoutCaptcha = (RelativeLayout) view.findViewById(R.id.layout_captcha);
    imageCaptcha = (ImageView) view.findViewById(R.id.image_captcha);
    editCaptcha = (EditText) view.findViewById(R.id.edit_captcha);
    buttonCaptchaRefresh = (ImageButton) view.findViewById(R.id.button_captcha_refresh);
    buttonCaptchaRefresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            loadCaptcha();
        }
    });
    if (getArguments().getBoolean(IS_EDIT, false)) {
        loadEditValues();
    } else {
        reddit.needsCaptcha().subscribe(new Observer<String>() {

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(String response) {
                if ("true".equalsIgnoreCase(response)) {
                    layoutCaptcha.setVisibility(View.VISIBLE);
                    loadCaptcha();
                }
            }
        });
    }
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Menu menu = toolbarActions.getMenu();
            int maxNum = (int) (view.getWidth() / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()));
            int numShown = 0;
            for (int index = 0; index < menu.size(); index++) {
                MenuItem menuItem = menu.getItem(index);
                menuItem.getIcon().setColorFilter(themer.getColorFilterIcon());
                if (numShown++ < maxNum - 1) {
                    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
                } else {
                    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                }
            }
            // Toggle visibility to fix weird bug causing tabs to not be added
            tabLayout.setVisibility(View.GONE);
            tabLayout.setVisibility(View.VISIBLE);
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
    return view;
}
Also used : AppBarLayout(android.support.design.widget.AppBarLayout) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) ImageButton(android.widget.ImageButton) Bundle(android.os.Bundle) ControllerLinks(com.winsonchiu.reader.links.ControllerLinks) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) ImageView(android.widget.ImageView) ViewPager(android.support.v4.view.ViewPager) LinkMovementMethod(android.text.method.LinkMovementMethod) MenuItem(android.view.MenuItem) TabLayout(android.support.design.widget.TabLayout) Inject(javax.inject.Inject) Picasso(com.squareup.picasso.Picasso) JSONException(org.json.JSONException) Reddit(com.winsonchiu.reader.data.reddit.Reddit) JSONObject(org.json.JSONObject) ComponentStatic(com.winsonchiu.reader.dagger.components.ComponentStatic) Toast(android.widget.Toast) Menu(android.view.Menu) Link(com.winsonchiu.reader.data.reddit.Link) View(android.view.View) JsonNode(com.fasterxml.jackson.databind.JsonNode) NestedScrollView(android.support.v4.widget.NestedScrollView) Log(android.util.Log) Processor(com.github.rjeschke.txtmark.Processor) UtilsInput(com.winsonchiu.reader.utils.UtilsInput) URLUtil(android.webkit.URLUtil) LayoutInflater(android.view.LayoutInflater) Listing(com.winsonchiu.reader.data.reddit.Listing) PagerAdapter(android.support.v4.view.PagerAdapter) TextUtils(android.text.TextUtils) Observer(rx.Observer) UtilsReddit(com.winsonchiu.reader.utils.UtilsReddit) ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) TypedValue(android.util.TypedValue) Toolbar(android.support.v7.widget.Toolbar) Html(android.text.Html) ViewTreeObserver(android.view.ViewTreeObserver) RelativeLayout(android.widget.RelativeLayout) Nullable(android.support.annotation.Nullable) Activity(android.app.Activity) EditText(android.widget.EditText) PagerAdapter(android.support.v4.view.PagerAdapter) TabLayout(android.support.design.widget.TabLayout) AppBarLayout(android.support.design.widget.AppBarLayout) Menu(android.view.Menu) ViewTreeObserver(android.view.ViewTreeObserver) ViewGroup(android.view.ViewGroup) MenuItem(android.view.MenuItem) ImageView(android.widget.ImageView) View(android.view.View) NestedScrollView(android.support.v4.widget.NestedScrollView) TextView(android.widget.TextView) ViewPager(android.support.v4.view.ViewPager) JSONObject(org.json.JSONObject)

Example 2 with AppBarLayout

use of android.support.design.widget.AppBarLayout in project Reader by TheKeeperOfPie.

the class FragmentProfile method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_profile, container, false);
    layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
    layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
    listener = new ControllerProfile.Listener() {

        @Override
        public void setSortAndTime(Sort sort, Time time) {
            menu.findItem(sort.getMenuId()).setChecked(true);
            menu.findItem(time.getMenuId()).setChecked(true);
            itemSortTime.setTitle(getString(R.string.time_description, menu.findItem(controllerProfile.getTime().getMenuId()).toString()));
        }

        @Override
        public void setPage(Page page) {
            spinnerPage.setSelection(adapterProfilePage.getPages().indexOf(page));
            if (page.getPage().equals(ControllerProfile.PAGE_HIDDEN)) {
                callback.setDrawable(getResources().getDrawable(R.drawable.ic_visibility_white_24dp));
            } else {
                callback.setDrawable(getResources().getDrawable(R.drawable.ic_visibility_off_white_24dp));
            }
        }

        @Override
        public void setIsUser(boolean isUser) {
            // TODO: Fix set page for Profile view
            adapterProfilePage.setIsUser(isUser);
        }

        @Override
        public void loadLink(Comment comment) {
            Log.d(TAG, "Link ID: " + comment.getLinkId());
            Intent intent = new Intent(activity, ActivityMain.class);
            intent.setAction(Intent.ACTION_VIEW);
            intent.putExtra(ActivityMain.REDDIT_PAGE, Reddit.BASE_URL + "/r/" + comment.getSubreddit() + "/comments/" + comment.getLinkId().replace("t3_", ""));
            startActivity(intent);
        }

        @Override
        public RecyclerView.Adapter getAdapter() {
            return adapterProfile;
        }

        @Override
        public void setToolbarTitle(CharSequence title) {
            toolbar.setTitle(title);
        }

        @Override
        public void setRefreshing(boolean refreshing) {
            swipeRefreshProfile.setRefreshing(refreshing);
        }

        @Override
        public void post(Runnable runnable) {
            recyclerProfile.post(runnable);
        }
    };
    int styleColorBackground = AppSettings.THEME_DARK.equals(mListener.getThemeBackground()) ? R.style.MenuDark : R.style.MenuLight;
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(new ThemeWrapper(activity, UtilsColor.getThemeForColor(getResources(), themer.getColorPrimary(), mListener)), styleColorBackground);
    toolbar = (Toolbar) activity.getLayoutInflater().cloneInContext(contextThemeWrapper).inflate(R.layout.toolbar, layoutAppBar, false);
    layoutAppBar.addView(toolbar);
    ((AppBarLayout.LayoutParams) toolbar.getLayoutParams()).setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    if (getFragmentManager().getBackStackEntryCount() <= 1) {
        toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mListener.openDrawer();
            }
        });
    } else {
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mListener.onNavigationBackClick();
            }
        });
    }
    toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    setUpOptionsMenu();
    adapterProfilePage = new AdapterProfilePage(activity);
    spinnerPage = new AppCompatSpinner(contextThemeWrapper);
    toolbar.addView(spinnerPage);
    ((Toolbar.LayoutParams) spinnerPage.getLayoutParams()).setMarginEnd((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()));
    spinnerPage.setAdapter(adapterProfilePage);
    spinnerPage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            controllerProfile.setPage(adapterProfilePage.getItem(position)).subscribe(getReloadObserver());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    swipeRefreshProfile = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_profile);
    swipeRefreshProfile.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            controllerProfile.reload().subscribe(getReloadObserver());
        }
    });
    linearLayoutManager = new LinearLayoutManager(activity);
    recyclerProfile = (RecyclerView) view.findViewById(R.id.recycler_profile);
    recyclerProfile.setHasFixedSize(true);
    recyclerProfile.setItemAnimator(null);
    recyclerProfile.setLayoutManager(linearLayoutManager);
    recyclerProfile.addItemDecoration(new ItemDecorationDivider(activity, ItemDecorationDivider.VERTICAL_LIST));
    AdapterListener adapterListener = new AdapterListener() {

        @Override
        public void scrollAndCenter(int position, int height) {
            UtilsAnimation.scrollToPositionWithCentering(position, recyclerProfile, false);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void clearDecoration() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void requestMore() {
            controllerProfile.loadMoreLinks().observeOn(AndroidSchedulers.mainThread()).subscribe(new ObserverError<Listing>() {

                @Override
                public void onError(Throwable e) {
                    Toast.makeText(getContext(), getString(R.string.error_loading_links), Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerProfile.requestDisallowInterceptTouchEvent(disallow);
            swipeRefreshProfile.requestDisallowInterceptTouchEvent(disallow);
            itemTouchHelper.select(null, CustomItemTouchHelper.ACTION_STATE_IDLE);
        }

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
            itemTouchHelper.setDisallow(disallow);
        }
    };
    AdapterLink.ViewHolderLink.Listener listenerLink = new AdapterLink.ViewHolderLink.Listener() {

        @Override
        public void onSubmitComment(Link link, String text) {
        }

        @Override
        public void onDownloadImage(Link link) {
        }

        @Override
        public void onDownloadImage(Link link, String title, String fileName, String url) {
        }

        @Override
        public void onLoadUrl(Link link, boolean forceExternal) {
        }

        @Override
        public void onShowFullEditor(Link link) {
        }

        @Override
        public void onVote(Link link, AdapterLink.ViewHolderLink viewHolderLink, Likes vote) {
        }

        @Override
        public void onCopyText(Link link) {
        }

        @Override
        public void onEdit(Link link) {
        }

        @Override
        public void onDelete(Link link) {
        }

        @Override
        public void onReport(Link link) {
        }

        @Override
        public void onSave(Link link) {
        }

        @Override
        public void onShowComments(Link link, AdapterLink.ViewHolderLink viewHolderLink, Source source) {
        }

        @Override
        public void onShowError(String error) {
        }

        @Override
        public void onMarkNsfw(Link link) {
        }
    };
    AdapterCommentList.ViewHolderComment.Listener listenerComments = new AdapterCommentList.ViewHolderComment.Listener() {

        @Override
        public void onToggleComment(Comment comment) {
        }

        @Override
        public void onShowReplyEditor(Comment comment) {
        }

        @Override
        public void onEditComment(Comment comment, String text) {
        }

        @Override
        public void onSendComment(Comment comment, String text) {
        }

        @Override
        public void onMarkRead(Comment comment) {
        }

        @Override
        public void onLoadNestedComments(Comment comment) {
        }

        @Override
        public void onJumpToParent(Comment comment) {
        }

        @Override
        public void onViewProfile(Comment comment) {
        }

        @Override
        public void onCopyText(Comment comment) {
        }

        @Override
        public void onDeleteComment(Comment comment) {
        }

        @Override
        public void onReport(Comment comment) {
        }

        @Override
        public void onVoteComment(Comment comment, AdapterCommentList.ViewHolderComment viewHolderComment, Likes vote) {
        }

        @Override
        public void onSave(Comment comment) {
        }
    };
    if (adapterProfile == null) {
        adapterProfile = new AdapterProfile(getActivity(), controllerProfile, adapterListener, listenerLink, listenerComments, listener);
    }
    recyclerProfile.setAdapter(adapterProfile);
    callback = new CustomItemTouchHelper.SimpleCallback(activity, R.drawable.ic_delete_white_24dp, ItemTouchHelper.START | ItemTouchHelper.END, ItemTouchHelper.START | ItemTouchHelper.END) {

        @Override
        public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            int position = viewHolder.getAdapterPosition();
            if (position == 2 || (position >= 6 && controllerProfile.getViewType(position - 6) == ControllerProfile.VIEW_TYPE_LINK)) {
                return super.getSwipeDirs(recyclerView, viewHolder);
            }
            return 0;
        }

        @Override
        public boolean isLongPressDragEnabled() {
            return false;
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
            Log.d(TAG, "onSwiped: " + viewHolder.getAdapterPosition());
            final int adapterPosition = viewHolder.getAdapterPosition();
            final int position = adapterPosition == 2 ? -1 : adapterPosition - 6;
            final Link link = adapterPosition == 2 ? controllerProfile.remove(-1) : controllerProfile.remove(position);
            mListener.getEventListenerBase().hide(link);
            if (snackbar != null) {
                snackbar.dismiss();
            }
            SpannableString text = new SpannableString(link.isHidden() ? getString(R.string.link_hidden) : getString(R.string.link_shown));
            text.setSpan(new ForegroundColorSpan(themer.getColorFilterPrimary().getColor()), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            //noinspection ResourceType
            snackbar = Snackbar.make(recyclerProfile, text, UtilsAnimation.SNACKBAR_DURATION).setActionTextColor(themer.getColorFilterPrimary().getColor()).setAction(R.string.undo, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mListener.getEventListenerBase().hide(link);
                    if (adapterPosition == 2) {
                        controllerProfile.setTopLink(link);
                        adapterProfile.notifyItemChanged(2);
                    } else {
                        controllerProfile.add(position, link);
                    }
                    recyclerProfile.invalidate();
                }
            });
            snackbar.getView().setBackgroundColor(themer.getColorPrimary());
            snackbar.show();
        }
    };
    itemTouchHelper = new CustomItemTouchHelper(callback);
    itemTouchHelper.attachToRecyclerView(recyclerProfile);
    return view;
}
Also used : ItemDecorationDivider(com.winsonchiu.reader.utils.ItemDecorationDivider) AdapterLink(com.winsonchiu.reader.links.AdapterLink) Page(com.winsonchiu.reader.data.Page) SpannableString(android.text.SpannableString) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) Sort(com.winsonchiu.reader.data.reddit.Sort) AppBarLayout(android.support.design.widget.AppBarLayout) ForegroundColorSpan(android.text.style.ForegroundColorSpan) AppCompatSpinner(android.support.v7.widget.AppCompatSpinner) SpannableString(android.text.SpannableString) AdapterView(android.widget.AdapterView) AdapterLink(com.winsonchiu.reader.links.AdapterLink) Link(com.winsonchiu.reader.data.reddit.Link) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) Time(com.winsonchiu.reader.data.reddit.Time) Source(com.winsonchiu.reader.comments.Source) ContextThemeWrapper(android.view.ContextThemeWrapper) ThemeWrapper(com.winsonchiu.reader.theme.ThemeWrapper) AdapterCommentList(com.winsonchiu.reader.comments.AdapterCommentList) ActivityMain(com.winsonchiu.reader.ActivityMain) CustomItemTouchHelper(com.winsonchiu.reader.utils.CustomItemTouchHelper) Comment(com.winsonchiu.reader.data.reddit.Comment) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) Intent(android.content.Intent) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) ContextThemeWrapper(android.view.ContextThemeWrapper) Listing(com.winsonchiu.reader.data.reddit.Listing) RecyclerView(android.support.v7.widget.RecyclerView) Likes(com.winsonchiu.reader.data.reddit.Likes)

Example 3 with AppBarLayout

use of android.support.design.widget.AppBarLayout in project Reader by TheKeeperOfPie.

the class FragmentReply method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_reply, container, false);
    layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
    layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
    toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    setUpToolbar();
    textAuthor = (TextView) view.findViewById(R.id.text_author);
    textAuthor.setText(getString(R.string.replying_from, controllerUser.getUser().getName()));
    scrollText = (NestedScrollView) view.findViewById(R.id.scroll_text);
    textParent = (TextView) view.findViewById(R.id.text_parent);
    textParent.setText(getArguments().getCharSequence(ARG_TEXT_PARENT, ""));
    editMarginDefault = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
    editMarginWithActions = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
    editReply = (EditText) view.findViewById(R.id.edit_reply);
    editReply.setText(getArguments().getString(ARG_TEXT));
    editReply.setSelection(editReply.length());
    View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
                behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
            }
        }
    };
    editReply.setOnFocusChangeListener(onFocusChangeListener);
    textPreview = (TextView) view.findViewById(R.id.text_preview);
    viewDivider = view.findViewById(R.id.view_divider);
    toolbarActions = (Toolbar) view.findViewById(R.id.toolbar_actions);
    toolbarActions.inflateMenu(R.menu.menu_editor_actions);
    toolbarActions.setOnMenuItemClickListener(this);
    viewPager = (ViewPager) view.findViewById(R.id.view_pager);
    viewPager.setAdapter(new PagerAdapter() {

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position) {
                case PAGE_PARENT:
                    return getString(R.string.page_parent);
                case PAGE_REPLY:
                    return getString(R.string.page_reply);
                case PAGE_PREVIEW:
                    return getString(R.string.page_preview);
            }
            return super.getPageTitle(position);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return viewPager.getChildAt(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }

        @Override
        public int getCount() {
            return viewPager.getChildCount();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    });
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (toolbarActions.getVisibility() == View.VISIBLE) {
                float translationY = toolbarActions.getTranslationY();
                if (position == PAGE_REPLY) {
                    translationY = positionOffset * (toolbarActions.getHeight() + viewDivider.getHeight());
                } else if (position == PAGE_PARENT) {
                    translationY = (1f - positionOffset) * (toolbarActions.getHeight() + viewDivider.getHeight());
                }
                viewDivider.setTranslationY(translationY);
                toolbarActions.setTranslationY(translationY);
            }
        }

        @Override
        public void onPageSelected(int position) {
            if (position == PAGE_PREVIEW) {
                if (editReply.length() == 0) {
                    textPreview.setText(R.string.empty_reply_preview);
                } else {
                    textPreview.setText(Html.fromHtml(Processor.process(editReply.getText().toString())));
                }
            }
            menu.findItem(R.id.item_hide_actions).setVisible(position == PAGE_REPLY);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    viewPager.setOffscreenPageLimit(viewPager.getChildCount() - 1);
    viewPager.setCurrentItem(PAGE_REPLY);
    tabLayout = (TabLayout) view.findViewById(R.id.layout_tab);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabTextColors(themer.getColorFilterTextMuted().getColor(), themer.getColorFilterPrimary().getColor());
    tabLayout.setupWithViewPager(viewPager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Menu menu = toolbarActions.getMenu();
            int maxNum = (int) (view.getWidth() / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()));
            int numShown = 0;
            for (int index = 0; index < menu.size(); index++) {
                MenuItem menuItem = menu.getItem(index);
                menuItem.getIcon().setColorFilter(themer.getColorFilterIcon());
                if (numShown++ < maxNum - 1) {
                    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
                } else {
                    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                }
            }
            // Toggle visibility to fix weird bug causing tabs to not be added
            tabLayout.setVisibility(View.GONE);
            tabLayout.setVisibility(View.VISIBLE);
            editReply.requestFocus();
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
    view.post(new Runnable() {

        @Override
        public void run() {
            view.setTranslationY(view.getHeight());
            view.setVisibility(View.VISIBLE);
            ViewCompat.animate(view).translationY(0).withEndAction(new Runnable() {

                @Override
                public void run() {
                    Fragment fragment = getFragmentManager().findFragmentByTag(fragmentParentTag);
                    if (fragment != null) {
                        getFragmentManager().beginTransaction().hide(fragment).commit();
                    }
                }
            }).start();
        }
    });
    return view;
}
Also used : ViewGroup(android.view.ViewGroup) MenuItem(android.view.MenuItem) View(android.view.View) NestedScrollView(android.support.v4.widget.NestedScrollView) TextView(android.widget.TextView) ViewPager(android.support.v4.view.ViewPager) Fragment(android.support.v4.app.Fragment) PagerAdapter(android.support.v4.view.PagerAdapter) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) TabLayout(android.support.design.widget.TabLayout) AppBarLayout(android.support.design.widget.AppBarLayout) Menu(android.view.Menu) ViewTreeObserver(android.view.ViewTreeObserver)

Example 4 with AppBarLayout

use of android.support.design.widget.AppBarLayout in project XRecyclerView by jianghejie.

the class XRecyclerView method onAttachedToWindow.

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    //解决和CollapsingToolbarLayout冲突的问题
    AppBarLayout appBarLayout = null;
    ViewParent p = getParent();
    while (p != null) {
        if (p instanceof CoordinatorLayout) {
            break;
        }
        p = p.getParent();
    }
    if (p instanceof CoordinatorLayout) {
        CoordinatorLayout coordinatorLayout = (CoordinatorLayout) p;
        final int childCount = coordinatorLayout.getChildCount();
        for (int i = childCount - 1; i >= 0; i--) {
            final View child = coordinatorLayout.getChildAt(i);
            if (child instanceof AppBarLayout) {
                appBarLayout = (AppBarLayout) child;
                break;
            }
        }
        if (appBarLayout != null) {
            appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {

                @Override
                public void onStateChanged(AppBarLayout appBarLayout, State state) {
                    appbarState = state;
                }
            });
        }
    }
}
Also used : CoordinatorLayout(android.support.design.widget.CoordinatorLayout) ViewParent(android.view.ViewParent) AppBarLayout(android.support.design.widget.AppBarLayout) RecyclerView(android.support.v7.widget.RecyclerView) ScrollView(android.widget.ScrollView) View(android.view.View)

Example 5 with AppBarLayout

use of android.support.design.widget.AppBarLayout in project materialistic by hidroh.

the class AppUtilsTest method testNavigate.

@Test
public void testNavigate() {
    AppBarLayout appBar = mock(AppBarLayout.class);
    when(appBar.getBottom()).thenReturn(1);
    Navigable navigable = mock(Navigable.class);
    AppUtils.navigate(Navigable.DIRECTION_DOWN, appBar, navigable);
    verify(appBar).setExpanded(eq(false), anyBoolean());
    verify(navigable, never()).onNavigate(anyInt());
    when(appBar.getBottom()).thenReturn(0);
    AppUtils.navigate(Navigable.DIRECTION_DOWN, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_DOWN));
    AppUtils.navigate(Navigable.DIRECTION_RIGHT, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_RIGHT));
    AppUtils.navigate(Navigable.DIRECTION_UP, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_UP));
    AppUtils.navigate(Navigable.DIRECTION_LEFT, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_LEFT));
}
Also used : AppBarLayout(android.support.design.widget.AppBarLayout) Test(org.junit.Test)

Aggregations

AppBarLayout (android.support.design.widget.AppBarLayout)27 View (android.view.View)15 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)11 RecyclerView (android.support.v7.widget.RecyclerView)7 Intent (android.content.Intent)6 ViewGroup (android.view.ViewGroup)6 ImageView (android.widget.ImageView)6 TabLayout (android.support.design.widget.TabLayout)5 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 Toolbar (android.support.v7.widget.Toolbar)5 TextView (android.widget.TextView)5 CollapsingToolbarLayout (android.support.design.widget.CollapsingToolbarLayout)4 ViewPager (android.support.v4.view.ViewPager)4 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)4 PagerAdapter (android.support.v4.view.PagerAdapter)3 NestedScrollView (android.support.v4.widget.NestedScrollView)3 ActionBar (android.support.v7.app.ActionBar)3 ViewParent (android.view.ViewParent)3 Activity (android.app.Activity)2 Bundle (android.os.Bundle)2