Android Open Source - EtViewPager_Android Scroll View Pager






From Project

Back to project page EtViewPager_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project EtViewPager_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//EtViewPager_Android
/**// w w  w. j  a v  a2s.c  o  m
 * Copyright (C) <2013>  <Emil Todorov>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package com.blogspot.techzealous.etviewpager;

import java.util.ArrayList;

import com.blogspot.techzealous.testbuttonstrech.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScrollViewPager extends Activity {
  
  private HorizontalScrollView scrollViewPager;
  private LinearLayout linearLayoutPager;
  private TextView textViewOne;
  private TextView textViewTwo;
  private TextView textViewThree;

  private int widthScreen;
  private int numPages;
  private int position;
  private float xDown;
  ArrayList<String> arrayTexts;
  private int prevPage;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_srollviewpager);
    
    scrollViewPager = (HorizontalScrollView) findViewById(R.id.horizontalScrollViewPager);
    linearLayoutPager = (LinearLayout) findViewById(R.id.linearLayoutPager);
    textViewOne = (TextView) findViewById(R.id.textViewPageOne);
    textViewTwo = (TextView) findViewById(R.id.textViewPageTwo);
    textViewThree = (TextView) findViewById(R.id.textViewPageThree);
    
    Display disp = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    widthScreen = disp.getWidth();
    
    numPages = 6;
    position = 0;
    arrayTexts = new ArrayList<String>();
    for(int x = 0; x < (numPages + 1); x++) {
      arrayTexts.add("Text for position" + x);
    }
    
    textViewOne.setWidth(widthScreen);
    textViewTwo.setWidth(widthScreen);
    textViewThree.setWidth(widthScreen);
    
    scrollViewPager.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
          xDown = event.getX();
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
          int pg = (scrollViewPager.getScrollX() + widthScreen / 2) / widthScreen;
          scrollViewPager.scrollTo(pg * widthScreen, 0);
          Log.i("ScrollViewPager", "pg= " + pg);
          
          float xUp = event.getX();
          if(xDown > xUp) {
            //wants to scroll to right
            if(position < numPages && position != 0) {if(pg != 1) {position++;}}
            if(position == 0 && prevPage != pg) {position++;}
          } else {
            //wants to scroll to left
            if(position > 0 && position != numPages) {if(pg != 1) {position--;}}
            if(position == numPages && prevPage != pg) {position--;}
          }
          
          if(pg == 2) {
            if(position < numPages) {
            TextView textViewNew = new TextView(ScrollViewPager.this);
            textViewNew.setWidth(widthScreen);
            textViewNew.setText(arrayTexts.get(position + 1));
            linearLayoutPager.addView(textViewNew, 3);
            linearLayoutPager.removeViewAt(0);
            scrollViewPager.scrollTo(widthScreen, 0);
            }
          } else if(pg == 0) {
            if(position > 0) { 
            TextView textViewNew = new TextView(ScrollViewPager.this);
            textViewNew.setWidth(widthScreen);
            textViewNew.setText(arrayTexts.get(position - 1));
            linearLayoutPager.addView(textViewNew, 0);
            linearLayoutPager.removeViewAt(3);
            scrollViewPager.scrollTo(widthScreen, 0);  
            }
          } 
          prevPage = pg;
          Log.i("ScrollViewPager", "position=" + position);
          return true;
        }
        return false;
      }
    });
  }
}




Java Source Code List

com.blogspot.techzealous.etviewpager.ScrollViewPager.java