Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:04:55+00:00 2026-05-30T00:04:55+00:00

I’m trying to create a slider, when users fling left or right, this slider

  • 0

I’m trying to create a slider, when users fling left or right, this slider will slide animatedly, translation animation. However, it doesn’t work in a right way.

package pete.android.study.home.scrollingindicator;



import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class MainScreen extends Activity {

    protected ValueAnimator mScrollIndicatorAnimator;
    protected ValueAnimator mScrollator;
    protected ImageView mScrollIndicator = null;    
    protected static final int sScrollIndicatorFadeInDuration = 150;
    protected static final int sScrollIndicatorFadeOutDuration = 650;
    protected static final int sScrollIndicatorFlashDuration = 650;

    public static final int PAGE_COUNT = 4;
    private int mPageWidth = 0;

    private int mCurrentPage = 0;
    private int mIndicatorPos = 0;
    private int mIndicatorSpace = 0;

    private GestureDetector mGestureDetector;
    private static final boolean DEBUG = true;
    private static final String TAG = "indicator";
    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        //ht = displaymetrics.heightPixels;
        mPageWidth = displaymetrics.widthPixels;

        mHandler = new Handler();
        setupScrollingIndicator();
        mGestureDetector = new GestureDetector(this, new LearnGestureListener());  
    }

    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
      if (mGestureDetector.onTouchEvent(event))  
        return true;  
      else  
        return false;  
    }  

    public void setupScrollingIndicator() {
        if(mScrollIndicator == null) { 
            mScrollIndicator = (ImageView) findViewById(R.id.indicator);
        }

        mIndicatorSpace = mPageWidth / PAGE_COUNT;
        mIndicatorPos = mIndicatorSpace * mCurrentPage;

        if(DEBUG) {
            Log.i(TAG, "mIndicatorSpace = " + mIndicatorSpace);
            Log.i(TAG, "mCurrentPage = " + mCurrentPage);
            Log.i(TAG, "mIndicatorPos = " + mIndicatorPos);         
        }

        if(mScrollIndicator.getMeasuredWidth() != mIndicatorSpace) {
            mScrollIndicator.getLayoutParams().width = mIndicatorSpace;
            mScrollIndicator.requestLayout();           
        }

        mScrollIndicator.setTranslationX(mIndicatorPos);
        mScrollIndicator.invalidate();
    }

    public void showIndicator() {
        setupScrollingIndicator();

        mScrollIndicator.setVisibility(View.VISIBLE);
        cancelScrollingAnimations();

        mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
        mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
        mScrollIndicatorAnimator.start();

        mScrollator = ObjectAnimator.ofFloat(mScrollIndicator, "translationX", mIndicatorPos);
        mScrollator.setDuration(sScrollIndicatorFlashDuration);
        mScrollator.start();
    }
    Runnable hideScrollingIndicatorRunnable = new Runnable() {
        @Override
        public void run() {
            hideIndicator();
        }
    };

    protected void flashIndicator() {        
        showIndicator();
        mHandler.postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
    }
    public void hideIndicator() {
        setupScrollingIndicator();
        cancelScrollingAnimations();

        mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
        mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
        mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
            private boolean cancelled = false;
            @Override
            public void onAnimationCancel(android.animation.Animator animation) {
                cancelled = true;
            }
            @Override
            public void onAnimationEnd(Animator animation) {
                if (!cancelled) {
                    mScrollIndicator.setVisibility(View.INVISIBLE);
                }
            }
        });
        mScrollIndicatorAnimator.start();        
    }
    public void cancelScrollingAnimations() {
        if(mScrollIndicatorAnimator != null) {
            mScrollIndicatorAnimator.cancel();
        }
    }

    public void scrollToRight() {
        flashIndicator();
        mCurrentPage++; 
        if(mCurrentPage >= PAGE_COUNT) {
            mCurrentPage = 0;
        }
    }

    public void scrollToLeft() {
        flashIndicator();
        mCurrentPage--;
        if(mCurrentPage < 0) {
            mCurrentPage = PAGE_COUNT - 1;
        }
    }

    class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override  
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
            if(e2.getX() - e1.getX() > 50) {

                scrollToLeft();
            } else if(e2.getX() - e1.getX() < 50) {
                scrollToRight();

            } 
            return true;  
        }  
    }  

}

Please help me fix this, thanks very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:04:56+00:00Added an answer on May 30, 2026 at 12:04 am

    mScrollIndicator.setTranslationX(mIndicatorPos) just redraw your view on new position mIndicatorPos. It doesn’t do animating.

    If you wanna mScrollIndicator move along with your scroll event, try to override onScroll method instead of onFling.

    Or another way, create an android.animation.ObjectAnimator object, setup it for your view and call ObjectAnimator.start() on Fling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.