通过点击在其侧面的按钮滚动一个Horizo​​ntalScrollView [英] Scrolling a HorizontalScrollView by clicking buttons on its sides

查看:163
本文介绍了通过点击在其侧面的按钮滚动一个Horizo​​ntalScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Horizo​​ntalScrollView A 片段中。当我在滚动 Horizo​​ntalScrollView 滚动项目的这一观点,而不是整个片段滚动到左或右。现在,我已经想到了使用上的布局两边按钮。但不能得到如何滚动两边我的滚动视图。

I am using a HorizontalScrollView within a Fragment. When I scroll this view instead of scrolling the items within HorizontalScrollView the whole fragment is scrolled to either left or right. Now I have thought of using buttons on both sides of the layout. But cannot get how to scroll my scroll view on either side.

任何帮助在这方面将是非常美联社preciated。

Any help in this regard will be highly appreciated.

修改

以下是我的XML文件。

Following is my xml file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageButton
    android:id="@+id/btn_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/left_arrow" />

<ImageButton
    android:id="@+id/btn_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/right_arrow" />

<HorizontalScrollView
    android:id="@+id/horizontal_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/btn_right"
    android:layout_toRightOf="@id/btn_left"
    android:fadeScrollbars="false"
    android:padding="5dp"
    android:scrollbarAlwaysDrawHorizontalTrack="true"
    android:scrollbars="horizontal" >

    <LinearLayout
        android:id="@+id/dynamic_generation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</HorizontalScrollView>

和我的Java文件

public class MyGallery extends Activity {

private LinearLayout linearLayout;

private ImageButton leftBtn;
private ImageButton rightBtn;
private HorizontalScrollView hsv;
int currentScrollX = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.horizontalscrolling);

    leftBtn = (ImageButton) findViewById(R.id.btn_left);
    rightBtn = (ImageButton) findViewById(R.id.btn_right);
    hsv = (HorizontalScrollView) findViewById(R.id.horizontal_scrollview);
    linearLayout = (LinearLayout) findViewById(R.id.dynamic_generation);

    leftBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });

    rightBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        }
    });

    String[] users = new String[20];
    for (int i = 0; i < 20; i++) {
        users[i] = "user " + (i + 1);
    }

    for (int i = 0; i < users.length; i++) {
        final TextView userId = new TextView(MyGallery.this);
        userId.setText(users[i]);
        ImageView imageView = new ImageView(MyGallery.this);
        linearLayout.addView(userId);
        linearLayout.addView(imageView);
        userId.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MyGallery.this,
                        userId.getText() + " has been clicked!",
                        Toast.LENGTH_LONG).show();
                // hsv.
            }
        });
    }
}

}

我需要什么

当我preSS任何向左或向右按​​钮滚动视图必须滚动向左或向右分别。

When I press any of the left or right button scroll view must scroll to left or right respectively.

推荐答案

如果您添加的code以下行现有的处理程序,您的观点将与每一个按钮的点击向右滚动:

If you add the following line of code to your existing handler, your view will scroll right with every button click:

rightBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        hsv.scrollTo((int)hsv.getScrollX() + 10, (int)hsv.getScrollY());
    }
});

如果你想它更平滑滚动,你可以使用onTouchListener来代替:

If you'd like it to scroll more smoothly you can use an onTouchListener instead:

rightBtn.setOnTouchListener(new View.OnTouchListener() {

    private Handler mHandler;
    private long mInitialDelay = 300;
    private long mRepeatDelay = 100;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mHandler != null)
                    return true;
                mHandler = new Handler();
                mHandler.postDelayed(mAction, mInitialDelay);
                break;
            case MotionEvent.ACTION_UP:
                if (mHandler == null)
                    return true;
                mHandler.removeCallbacks(mAction);
                mHandler = null;
                break;
        }
        return false;
    }

    Runnable mAction = new Runnable() {
        @Override
        public void run() {
            hsv.scrollTo((int) hsv.getScrollX() + 10, (int) hsv.getScrollY());
            mHandler.postDelayed(mAction, mRepeatDelay);
        }
    };
});

因人而异的拖延根据自己的喜好来得到你想要的平滑度和响应能力。

Vary the delays to your liking to get the smoothness and responsiveness you want.

这篇关于通过点击在其侧面的按钮滚动一个Horizo​​ntalScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆