Search in sources :

Example 91 with ViewPager

use of android.support.v4.view.ViewPager in project actor-platform by actorapp.

the class TourActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tour_activity);
    final VerticalViewPager viewPager = (VerticalViewPager) findViewById(R.id.viewpager);
    final View backToTopText = findViewById(R.id.back_to_top);
    final View backToTopArrow = findViewById(R.id.back_to_top_arrow);
    final View paralax = findViewById(R.id.paralax);
    final View paralaxImage = findViewById(R.id.paralax_image);
    final View background = findViewById(R.id.background);
    final View loginHolder = findViewById(R.id.login_holder);
    final View welcomeImage = findViewById(R.id.welcome_logo);
    final View welcomeText = findViewById(R.id.welcome_text);
    background.findViewById(R.id.background_colored).setBackgroundColor(ActorSDK.sharedActor().style.getMainColor());
    TextView signupButtonText = (TextView) findViewById(R.id.signUpButtonText);
    StateListDrawable states = SelectorFactory.get(ActorSDK.sharedActor().style.getPrimaryAltColor(), this);
    signupButtonText.setBackgroundDrawable(states);
    signupButtonText.setTextColor(ActorSDK.sharedActor().style.getTextPrimaryInvColor());
    signupButtonText.setTypeface(Fonts.medium());
    TextView buttonSignUp = (TextView) findViewById(R.id.signIn);
    buttonSignUp.setTextColor(ActorSDK.sharedActor().style.getTextPrimaryInvColor());
    buttonSignUp.setTypeface(Fonts.medium());
    ((TextView) findViewById(R.id.app_name)).setTextColor(ActorSDK.sharedActor().style.getTextPrimaryInvColor());
    ((TextView) findViewById(R.id.tour_welcome_subtitle)).setTextColor(ActorSDK.sharedActor().style.getTextPrimaryInvColor());
    findViewById(R.id.signUpButton).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Bundle authExtras = new Bundle();
            authExtras.putInt(AuthActivity.SIGN_TYPE_KEY, AuthActivity.SIGN_TYPE_UP);
            ActorSDK.sharedActor().startAuthActivity(TourActivity.this, authExtras);
            finish();
        }
    });
    findViewById(R.id.signIn).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Bundle authExtras = new Bundle();
            authExtras.putInt(AuthActivity.SIGN_TYPE_KEY, AuthActivity.SIGN_TYPE_IN);
            ActorSDK.sharedActor().startAuthActivity(TourActivity.this, authExtras);
            finish();
        }
    });
    View.OnClickListener jumpToTopListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            backToTopArrow.animate().alpha(0).setDuration(0).start();
            backToTopText.animate().alpha(0).setDuration(0).start();
            viewPager.setCurrentItem(0, true);
        }
    };
    backToTopText.setOnClickListener(jumpToTopListener);
    backToTopArrow.setOnClickListener(jumpToTopListener);
    viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {

        @Override
        public Fragment getItem(int position) {
            if (position == 0 || position == lastPageIndex + 1)
                return new Fragment();
            return TourFragment.getInstance(position);
        }

        @Override
        public int getCount() {
            //6;
            return 4;
        }
    });
    //Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    viewPager.setOffscreenPageLimit(3);
    //A little space between pages
    viewPager.setPageMargin(-Screen.dp(165));
    //If hardware acceleration is enabled, you should also remove
    // clipping on the pager for its children.
    viewPager.setClipChildren(false);
    viewPager.setOnScrollListener(new ScrollListener() {

        public static final int LOGINHOLDER_STATUS_OUT = 1;

        public static final int LOGINHOLDER_STATUS_VISIBLE = 2;

        public int loginHolderStatus;

        public boolean welcomeShowed = false;

        public boolean mainContentHidden = false;

        public boolean loginHolderBackgroundVisible = false;

        @Override
        public void onScroll(int y, float page) {
            if (page < 1) {
                float alpha = 1 - page * 2;
                backToTopArrow.animate().alpha(alpha).setDuration(0).start();
                //status1.setText("alpha:" + alpha);
                if (loginHolder.getTop() != 0) {
                    float loginY = (float) loginHolder.getTop() * ((float) 1 - (page));
                    loginHolder.animate().y(loginY).setDuration(0).setStartDelay(0).start();
                    background.animate().y(loginY - background.getHeight() + loginHolder.getHeight() + Screen.dp(8)).setDuration(0).start();
                    /*if(loginHolderBackgroundVisible){
                            loginHolderBackgroundVisible = false;
                            loginHolderBackground.animate().alpha(0).setDuration(0).start();
                        } else {

                        }*/
                    //loginHolderBackground.animate().alpha(page).setDuration(0).start();
                    background.animate().alpha(page).setDuration(0).start();
                    loginHolderStatus = LOGINHOLDER_STATUS_VISIBLE;
                    float welcomeImageY = welcomeImage.getTop() - y / 2;
                    float welcomeTextY = welcomeText.getTop() - y / 2;
                    if (alpha > 0) {
                        welcomeImage.animate().alpha(alpha).y(welcomeImageY).setDuration(0).start();
                        welcomeText.animate().alpha(alpha).y(welcomeTextY).setDuration(0).start();
                    } else {
                        welcomeImage.animate().alpha(0).setDuration(0).start();
                        welcomeText.animate().alpha(0).setDuration(0).start();
                    }
                }
                mainContentHidden = false;
            } else {
                if (page >= 1 && page <= lastPageIndex) {
                    // todo freeze?
                    if (!mainContentHidden) {
                        mainContentHidden = true;
                        loginHolderBackgroundVisible = true;
                        //signinLastView.setEnabled(false);
                        //signinView.setEnabled(false);
                        //signupLastView.setEnabled(false);
                        //signupView.setEnabled(false);
                        loginHolder.animate().y(0).setDuration(0).start();
                        //loginHolderBackground.animate().alpha(1).setDuration(0).start();
                        background.animate().y(-loginHolder.getTop() + Screen.dp(8)).alpha(1).setDuration(0).start();
                        welcomeImage.animate().alpha(0).setDuration(0).start();
                        welcomeText.animate().alpha(0).setDuration(0).start();
                        backToTopArrow.animate().alpha(0).setDuration(0).start();
                        backToTopText.animate().alpha(0).setDuration(0).start();
                    }
                    if (page > lastPageIndex - 1) {
                        float alpha = (page - (lastPageIndex - 1)) * 3 - 2;
                        if (alpha > 0) {
                            backToTopText.animate().scaleX(alpha).scaleY(alpha).alpha(alpha).setDuration(0).start();
                        } else {
                            backToTopText.animate().scaleX(0).scaleY(0).alpha(0).setDuration(0).start();
                        }
                    }
                } else {
                    if (page > lastPageIndex) {
                        float progress = page - lastPageIndex;
                        if (progress > 0.5) {
                            if (loginHolderStatus == LOGINHOLDER_STATUS_VISIBLE) {
                                loginHolderStatus = LOGINHOLDER_STATUS_OUT;
                                loginHolder.animate().y(-loginHolder.getHeight()).setDuration(200).setInterpolator(new AccelerateDecelerateInterpolator()).setStartDelay(0).start();
                            }
                            mainContentHidden = false;
                        } else {
                            if (loginHolderStatus == LOGINHOLDER_STATUS_OUT) {
                                loginHolderStatus = LOGINHOLDER_STATUS_VISIBLE;
                                loginHolder.animate().y(0).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(200).setStartDelay(0).start();
                            }
                            if (!mainContentHidden) {
                                backToTopArrow.animate().alpha(0).setDuration(0).start();
                                backToTopText.animate().alpha(0).setDuration(0).start();
                            }
                        }
                    }
                }
            }
            int paralaxHolderHeight = paralax.getHeight();
            int paralaxImageHeight = paralaxImage.getHeight();
            float paralaxY = -(((float) paralaxImageHeight - paralaxHolderHeight) * ((float) page / (lastPageIndex)));
            paralaxImage.animate().y(paralaxY).setDuration(0).setStartDelay(0).start();
        }
    });
    if (Build.VERSION.SDK_INT >= 19) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        contentTopPadding = Screen.getStatusBarHeight();
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) findViewById(R.id.pager_container).getLayoutParams();
        params.topMargin = contentTopPadding;
        findViewById(R.id.pager_container).setLayoutParams(params);
        params = (RelativeLayout.LayoutParams) backToTopArrow.getLayoutParams();
        params.topMargin = contentTopPadding + params.topMargin;
        backToTopArrow.setLayoutParams(params);
        params = (RelativeLayout.LayoutParams) welcomeImage.getLayoutParams();
        params.topMargin = contentTopPadding + params.topMargin;
        welcomeImage.setLayoutParams(params);
        params = (RelativeLayout.LayoutParams) welcomeText.getLayoutParams();
        params.topMargin = contentTopPadding + params.topMargin;
        welcomeText.setLayoutParams(params);
        params = (RelativeLayout.LayoutParams) backToTopText.getLayoutParams();
        params.topMargin = contentTopPadding + params.topMargin;
        backToTopText.setLayoutParams(params);
    }
}
Also used : FragmentStatePagerAdapter(android.support.v4.app.FragmentStatePagerAdapter) Bundle(android.os.Bundle) StateListDrawable(android.graphics.drawable.StateListDrawable) TextView(android.widget.TextView) View(android.view.View) Fragment(android.support.v4.app.Fragment) RelativeLayout(android.widget.RelativeLayout) AccelerateDecelerateInterpolator(android.view.animation.AccelerateDecelerateInterpolator) TextView(android.widget.TextView)

Example 92 with ViewPager

use of android.support.v4.view.ViewPager in project material-intro-screen by TangoAgency.

the class OverScrollViewPager method canOverScrollAtEnd.

private boolean canOverScrollAtEnd() {
    SwipeableViewPager viewPager = getOverScrollView();
    PagerAdapter adapter = viewPager.getAdapter();
    if (null != adapter && adapter.getCount() > 0) {
        if (viewPager.alphaExitTransitionEnabled() && viewPager.getCurrentItem() == adapter.getCount() - 1) {
            return true;
        }
        return false;
    }
    return false;
}
Also used : PagerAdapter(android.support.v4.view.PagerAdapter)

Example 93 with ViewPager

use of android.support.v4.view.ViewPager in project material-intro-screen by TangoAgency.

the class CustomViewPager method initViewPager.

void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;
    mTouchSlop = configuration.getScaledPagingTouchSlop();
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffectCompat(context);
    mRightEdge = new EdgeEffectCompat(context);
    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);
    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());
    if (ViewCompat.getImportantForAccessibility(this) == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }
    ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {

        private final Rect mTempRect = new Rect();

        @Override
        public WindowInsetsCompat onApplyWindowInsets(final View v, final WindowInsetsCompat originalInsets) {
            // First let the ViewPager itself try and consume them...
            final WindowInsetsCompat applied = ViewCompat.onApplyWindowInsets(v, originalInsets);
            if (applied.isConsumed()) {
                // If the ViewPager consumed all insets, return now
                return applied;
            }
            // Now we'll manually dispatch the insets to our children. Since ViewPager
            // children are always full-height, we do not want to use the standard
            // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
            // the rest of the children will not receive any insets. To workaround this
            // we manually dispatch the applied insets, not allowing children to
            // consume them from each other. We do however keep track of any insets
            // which are consumed, returning the union of our children's consumption
            final Rect res = mTempRect;
            res.left = applied.getSystemWindowInsetLeft();
            res.top = applied.getSystemWindowInsetTop();
            res.right = applied.getSystemWindowInsetRight();
            res.bottom = applied.getSystemWindowInsetBottom();
            for (int i = 0, count = getChildCount(); i < count; i++) {
                final WindowInsetsCompat childInsets = ViewCompat.dispatchApplyWindowInsets(getChildAt(i), applied);
                // Now keep track of any consumed by tracking each dimension's min
                // value
                res.left = Math.min(childInsets.getSystemWindowInsetLeft(), res.left);
                res.top = Math.min(childInsets.getSystemWindowInsetTop(), res.top);
                res.right = Math.min(childInsets.getSystemWindowInsetRight(), res.right);
                res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(), res.bottom);
            }
            // Now return a new WindowInsets, using the consumed window insets
            return applied.replaceSystemWindowInsets(res.left, res.top, res.right, res.bottom);
        }
    });
}
Also used : Context(android.content.Context) Rect(android.graphics.Rect) View(android.view.View) ViewConfiguration(android.view.ViewConfiguration) EdgeEffectCompat(android.support.v4.widget.EdgeEffectCompat) Scroller(android.widget.Scroller)

Example 94 with ViewPager

use of android.support.v4.view.ViewPager 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 95 with ViewPager

use of android.support.v4.view.ViewPager in project Reader by TheKeeperOfPie.

the class FragmentHistory method showDateRangeDialog.

private void showDateRangeDialog() {
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date_range, null, false);
    final DatePicker datePickerStart = (DatePicker) view.findViewById(R.id.date_picker_start);
    final DatePicker datePickerEnd = (DatePicker) view.findViewById(R.id.date_picker_end);
    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager_time);
    viewPager.setAdapter(new PagerAdapter() {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            switch(position) {
                case 0:
                    return datePickerStart;
                case 1:
                    return datePickerEnd;
            }
            return super.instantiateItem(container, position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position) {
                case 0:
                    return viewPager.getContext().getResources().getString(R.string.start);
                case 1:
                    return viewPager.getContext().getResources().getString(R.string.end);
            }
            return super.getPageTitle(position);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    });
    final TabLayout layoutTab = (TabLayout) view.findViewById(R.id.layout_tab);
    layoutTab.setupWithViewPager(viewPager);
    final Calendar calendarStart = Calendar.getInstance();
    calendarStart.setTimeZone(TimeZone.getTimeZone("UTC"));
    datePickerStart.init(calendarStart.get(Calendar.YEAR), calendarStart.get(Calendar.MONTH), calendarStart.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            calendarStart.set(year, monthOfYear, dayOfMonth);
            layoutTab.getTabAt(0).setText(DateUtils.formatDateTime(getActivity(), calendarStart.getTimeInMillis(), FORMAT_DATE));
        }
    });
    long timeStart = controllerHistory.getTimeStart();
    if (timeStart > 0) {
        calendarStart.setTimeInMillis(timeStart);
        datePickerStart.updateDate(calendarStart.get(Calendar.YEAR), calendarStart.get(Calendar.MONTH), calendarStart.get(Calendar.DAY_OF_MONTH));
    }
    final Calendar calendarEnd = Calendar.getInstance();
    calendarEnd.setTimeZone(TimeZone.getTimeZone("UTC"));
    datePickerEnd.init(calendarEnd.get(Calendar.YEAR), calendarEnd.get(Calendar.MONTH), calendarEnd.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            calendarEnd.set(year, monthOfYear, dayOfMonth);
            layoutTab.getTabAt(1).setText(DateUtils.formatDateTime(getActivity(), calendarEnd.getTimeInMillis(), FORMAT_DATE));
        }
    });
    datePickerEnd.setMaxDate(System.currentTimeMillis() + AlarmManager.INTERVAL_DAY);
    long timeEnd = controllerHistory.getTimeEnd();
    if (timeEnd < Long.MAX_VALUE) {
        calendarEnd.setTimeInMillis(timeEnd);
        calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
        datePickerEnd.updateDate(calendarEnd.get(Calendar.YEAR), calendarEnd.get(Calendar.MONTH), calendarEnd.get(Calendar.DAY_OF_MONTH));
    }
    AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(view).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Calendar calendarStart = Calendar.getInstance();
            calendarStart.set(datePickerStart.getYear(), datePickerStart.getMonth(), datePickerStart.getDayOfMonth());
            calendarStart.setTimeZone(TimeZone.getTimeZone("UTC"));
            Calendar calendarEnd = Calendar.getInstance();
            calendarEnd.set(datePickerEnd.getYear(), datePickerEnd.getMonth(), datePickerEnd.getDayOfMonth());
            calendarEnd.setTimeZone(TimeZone.getTimeZone("UTC"));
            calendarEnd.add(Calendar.DAY_OF_MONTH, 1);
            controllerHistory.setTimeStart(calendarStart.getTimeInMillis());
            controllerHistory.setTimeEnd(calendarEnd.getTimeInMillis());
            controllerHistory.reload();
        }
    }).setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            controllerHistory.setTimeStart(0);
            controllerHistory.setTimeEnd(Long.MAX_VALUE);
            controllerHistory.reload();
        }
    }).setNegativeButton(R.string.cancel, null).create();
    dialog.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) ViewGroup(android.view.ViewGroup) Calendar(java.util.Calendar) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) ViewPager(android.support.v4.view.ViewPager) PagerAdapter(android.support.v4.view.PagerAdapter) TabLayout(android.support.design.widget.TabLayout) DatePicker(android.widget.DatePicker)

Aggregations

ViewPager (android.support.v4.view.ViewPager)246 View (android.view.View)95 PagerAdapter (android.support.v4.view.PagerAdapter)55 Fragment (android.support.v4.app.Fragment)44 TabLayout (android.support.design.widget.TabLayout)37 TextView (android.widget.TextView)35 FragmentPagerAdapter (android.support.v4.app.FragmentPagerAdapter)34 Toolbar (android.support.v7.widget.Toolbar)30 Bundle (android.os.Bundle)28 ViewGroup (android.view.ViewGroup)27 FragmentStatePagerAdapter (android.support.v4.app.FragmentStatePagerAdapter)21 ImageView (android.widget.ImageView)20 ArrayList (java.util.ArrayList)20 Intent (android.content.Intent)17 ActionBar (android.support.v7.app.ActionBar)15 RecyclerView (android.support.v7.widget.RecyclerView)11 ViewTreeObserver (android.view.ViewTreeObserver)9 AdapterView (android.widget.AdapterView)9 LinearLayout (android.widget.LinearLayout)9 UiController (android.support.test.espresso.UiController)8