其他分享
首页 > 其他分享> > android – 通过单击侧面的按钮滚动Horizo​​ntalScrollView

android – 通过单击侧面的按钮滚动Horizo​​ntalScrollView

作者:互联网

我在片段中使用Horizo​​ntalScrollView.当我滚动此视图而不是滚动Horizo​​ntalScrollView中的项目时,整个片段将向左或向右滚动.现在我想到了在布局两侧使用按钮.但无法获得如何在任何一侧滚动我的滚动视图.

在这方面的任何帮助将受到高度赞赏.

编辑

以下是我的xml文件.

<?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.
            }
        });
    }
}

}

我需要的

当我按下任何左或右按钮时,滚动视图必须分别向左或向右滚动.

解决方法:

如果将以下代码行添加到现有处理程序,则每次按下按钮时,视图将向右滚动:

rightBtn.setOnClickListener(new View.OnClickListener() {

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

如果您希望它更顺畅地滚动,您可以使用onTouchListener:

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);
        }
    };
});

改变您喜欢的延迟,以获得您想要的平稳性和响应性.

标签:horizontal-scrolling,android,horizontalscrollview
来源: https://codeday.me/bug/20191007/1869549.html