如何进行水平滚动视图,图像在两个方向上滚动

时间:2012-11-14 05:33:54

标签: android

我正在尝试显示带有图像的水平滚动视图以向两个方向滚动。以前为此我使用了Gallery View但是galleryview被删除了,我使用水平滚动视图而不是画廊视图但水平滚动视图与画廊视图不同

现在我必须做两次实施

1)连续两个方向滚动。

2)中心锁定功能与图库相同。

我的屏幕看起来像

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用 viewpager

尝试此代码并根据需要进行修改。

注意:这不是我的代码已经在网上发现它属于:

( 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>
希望能帮到你。