2

I am trying to show horizontal scroll view with images to scroll in two directions.Previously for this i used Gallery View but galleryview is deprected ,i am using horizontal scroll view instead of gallery view but horizontal scroll view is different with gallery view.

Now i have to do two implementation

1) Scrolling in two directions continuously.

2) Center lock feature as same as gallery.

My screen looks like

enter image description here

koti
  • 3,681
  • 5
  • 34
  • 58
  • check out this [answer][1] might be what you are looking for. [1]: http://stackoverflow.com/questions/11577153/change-the-behaviour-of-a-gridview-to-make-it-scroll-horizontally-rather-than-ve – Proxy32 Nov 14 '12 at 06:33

1 Answers1

2

you can use viewpager,

try this code and modify it as your need .

note : this not my code already found it on the net belong to:

( Dave Smith,@devunwired Date: 8/17/12 PagerActivity).

MainActivity:

  public class MainActivity extends Activity{


PagerContainer  mContainer;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContainer = (PagerContainer) findViewById(R.id.pager_container);

    ViewPager pager = mContainer.getViewPager();
    PagerAdapter adapter = new MyPagerAdapter();
    pager.setAdapter(adapter);
    // Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    pager.setOffscreenPageLimit(adapter.getCount());
    // A little space between pages
    pager.setPageMargin(15);}

// Nothing special about this adapter, just throwing up colored views for
// demo
private class MyPagerAdapter extends PagerAdapter{

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        TextView view = new TextView(MainActivity.this);
        view.setText("Item " + position);
        view.setGravity(Gravity.CENTER);
        view.setBackgroundColor(Color.argb(255, position * 50,
                position * 10, position * 50));

        container.addView(view);
        return view;
                                 }

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


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

    @Override
    public boolean isViewFromObject(View view, Object object)
         {
        return (view == object);
               }}}

PagerContainer:

  public class PagerContainer extends FrameLayout implements
            ViewPager.OnPageChangeListener {

  private ViewPager mPager;
  boolean mNeedsRedraw = false;

  public PagerContainer(Context context) {
    super(context);
    init();
                      }

public PagerContainer(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
        }

public PagerContainer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
                 }

private void init() {
    setClipChildren(false);
                   }

@Override
protected void onFinishInflate() {
    try {
        mPager = (ViewPager) getChildAt(0);
        mPager.setOnPageChangeListener(this);
    } catch (Exception e) {
     throw new IllegalStateException("The root child of PagerContainer must be a
                  ViewPager");
                  }
                }

public ViewPager getViewPager() {
    return mPager;
               }

private Point mCenter = new Point();
private Point mInitialTouch = new Point();

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mCenter.x = w / 2;
    mCenter.y = h / 2;
            }

@Override
public boolean onTouchEvent(MotionEvent ev) {
    //We capture any touches not already handled by the ViewPager
    // to implement scrolling from a touch outside the pager bounds.
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mInitialTouch.x = (int)ev.getX();
            mInitialTouch.y = (int)ev.getY();
        default:
            ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y -
               mInitialTouch.y);
            break;
                }

    return mPager.dispatchTouchEvent(ev);
                  }

public void onPageScrolled(int position, float positionOffset, int
           positionOffsetPixels) {
    //Force the container to redraw on scrolling.
    //Without this the outer pages render initially and then stay static
    if (mNeedsRedraw) invalidate();
                 }

public void onPageSelected(int position) { }

public void onPageScrollStateChanged(int state) {
    mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);
           }
            }

activity_main.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >

<com.example.testviewpager.PagerContainer
    android:id="@+id/pager_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#CCC" >

    <android.support.v4.view.ViewPager
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal" />
</com.example.testviewpager.PagerContainer>

 </RelativeLayout>

hope help you.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
frozenhill
  • 124
  • 1
  • 9
  • No, my team decided not to use gallery view because it is deprecated is there other solution for this – koti Nov 14 '12 at 06:24
  • @koti As u shown above image Did got the output in same way.. How dis u use it can u give some idea about it... here is my question http://stackoverflow.com/questions/13757287/insert-3-images-dynamically-to-horizontal-scrollview-or-viewpager – Rao's Dec 07 '12 at 05:51
  • the above accepted answer is worked for me and it will work for you also – koti Dec 07 '12 at 06:06